Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

feat: add external handler through module options #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SasanFarrokh
Copy link

No description provided.

@pi0
Copy link
Member

pi0 commented Jun 12, 2019

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

@farzadso
Copy link
Collaborator

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.

@pi0
Copy link
Member

pi0 commented Jun 12, 2019

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

@farzadso
Copy link
Collaborator

@SasanFarrokh

@SasanFarrokh
Copy link
Author

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.

@pi0
Copy link
Member

pi0 commented Jun 12, 2019

Can you please share an example custom handler?

@SasanFarrokh
Copy link
Author

SasanFarrokh commented Jun 13, 2019

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('-')
       })
   }
}

@pi0
Copy link
Member

pi0 commented Jun 14, 2019

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.

@farzadso
Copy link
Collaborator

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

@armonb
Copy link

armonb commented Aug 5, 2019

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

@farzadso
Copy link
Collaborator

farzadso commented Aug 6, 2019

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

@pi0
Copy link
Member

pi0 commented Aug 6, 2019

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
Copy link
Member

pi0 commented Aug 6, 2019

Example for options (handler) file with < 2.9 backward support: nuxt-community/vuetify-module#86

@ghost
Copy link

ghost commented Aug 22, 2019

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

@farzadso
Copy link
Collaborator

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

@japboy
Copy link

japboy commented Mar 18, 2020

any updates here?

@leandromatos-hotmart
Copy link

Any updates?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants