google-optimize-module icon indicating copy to clipboard operation
google-optimize-module copied to clipboard

feat: add external handler through module options

Open SasanFarrokh opened this issue 6 years ago • 17 comments

SasanFarrokh avatar Jun 11 '19 08:06 SasanFarrokh

Serialized function has many limits. What's the usecase of replacing handler?

pi0 avatar Jun 12 '19 04:06 pi0

The handler is for handling manual GTM implementations using app.html instead of the existing modules to use dataLayer instead of ga. I talked with Sasan and we will handle Google Optimize's features manually.

farzadso avatar Jun 12 '19 07:06 farzadso

In this case, I think this line can be configurable to support dataLayer. Providing a sample use case (with dataLayer) can be useful.

pi0 avatar Jun 12 '19 07:06 pi0

@SasanFarrokh

farzadso avatar Jun 12 '19 08:06 farzadso

We can add a feature to support dataLayer But the problem is that we don’t know what needs to be inserted inside dataLayer, So there has to be a function that defines what needs to be inserted.

With that in mind, how can we achieve this? Share your ideas.

SasanFarrokh avatar Jun 12 '19 09:06 SasanFarrokh

Can you please share an example custom handler?

pi0 avatar Jun 12 '19 10:06 pi0

like this:

{
    handler(ctx, experiment) {
        ctx.$gtm.execute({
              expId: experiment.experimentId,
              expVar: experiment.$activeVariants.join('-')
       })
   }
}

or directly pushing to dataLayer (not working, cause can't access global scope window):

{
    handler(ctx, experiment) {
        window.dataLayer.push({
              expId: experiment.experimentId,
              expVar: experiment.$activeVariants.join('-')
       })
   }
}

SasanFarrokh avatar Jun 13 '19 05:06 SasanFarrokh

We can add a new module option eventHandler which can be either ga, dataLayer or gtm and in the template: (below code is untested)

function googleOptimize(ctx, { experiment }) {
  if (process.server || !experiment || !experiment.experimentID) {
    return
  }

  <% if (options.eventHandler === 'ga') { %>
  // https://developers.google.com/optimize/devguides/experiments
  if (!window.ga) return
  const exp = experiment.experimentID + '.' + experiment.$variantIndexes.join('-')
  window.ga('set', 'exp', exp)
  <% } else if (options.eventHandler === 'gtm') { %>
  // TODO: Link to gtm docs  
  if (!ctx.$gtm) return
  ctx.$gtm.execute({
    expId: experiment.experimentId,
    expVar: experiment.$activeVariants.join('-')
  })
  <% } else if (options.eventHandler === 'dataLayer') { %>
  // TODO: Link to gtm docs  
  if (!window.dataLayer) return
  window.dataLayer.push({
    expId: experiment.experimentId,
    expVar: experiment.$activeVariants.join('-')
  })
  <% } %>
}

This is safer as we don't serialize function and also removes boilerplate from projects that need different event handling.

pi0 avatar Jun 14 '19 21:06 pi0

@SasanFarrokh Would you please check Pooya's proposal and inform us? Thanks mate.

farzadso avatar Jun 15 '19 06:06 farzadso

Any update on this? We currently have Optimize set up via tag manager and this looks like it would fix our issues.

armonb avatar Aug 05 '19 07:08 armonb

@pi0 Do you think Sasan's work needs any further refinements or can we merge this?

farzadso avatar Aug 06 '19 07:08 farzadso

Serializing functions is not a nuxt recommended way. Other than suggestion above, modules for nuxt 2.9 are supporting app/ dir for custom options like this which we can leverage here as well. (app/google-optimize/handler.js)

pi0 avatar Aug 06 '19 07:08 pi0

Example for options (handler) file with < 2.9 backward support: https://github.com/nuxt-community/vuetify-module/pull/86

pi0 avatar Aug 06 '19 07:08 pi0

Any update on this please? It would be really helpful :-)

ghost avatar Aug 22 '19 09:08 ghost

@SasanFarrokh Can you take a look at he vuetify example and see if we can implement it in our module?

farzadso avatar Aug 24 '19 09:08 farzadso

any updates here?

japboy avatar Mar 18 '20 06:03 japboy

Any updates?

leandromatos-hotmart avatar May 26 '20 14:05 leandromatos-hotmart