Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(google-tag-manager): refactor, dev option and $gtm.pushEvent #274

Merged
merged 14 commits into from
Apr 15, 2019
Merged

feat(google-tag-manager): refactor, dev option and $gtm.pushEvent #274

merged 14 commits into from
Apr 15, 2019

Conversation

syffs
Copy link
Contributor

@syffs syffs commented Apr 8, 2019

@pi0 @manniL extracted module logic to a dedicated class for better readability and extensibility

Features:

  • pushing event from $gtm.pushEvent()

  • enabled option to allow dev debugging

pi0
pi0 previously requested changes Apr 8, 2019
packages/google-tag-manager/README.md Outdated Show resolved Hide resolved
@@ -44,7 +44,8 @@ id: () => {
{
id: 'GTM-XXXXXXX',
layer: 'dataLayer',
pageTracking: false
pageTracking: false,
enabled: ({ isDev, isClient }) => (!isDev && isClient), // or `false` when in dev/debug mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be enough to add dev option and not adding such complexity. Anyway, this plugin only works in client mode. Setting dev: true can cause plugin to also added in nuxt dev for debugging.

packages/google-tag-manager/gtm.js Outdated Show resolved Hide resolved
packages/google-tag-manager/gtm.js Outdated Show resolved Hide resolved
packages/google-tag-manager/gtm.js Outdated Show resolved Hide resolved
packages/google-tag-manager/gtm.js Outdated Show resolved Hide resolved
packages/google-tag-manager/gtm.js Outdated Show resolved Hide resolved
packages/google-tag-manager/index.js Outdated Show resolved Hide resolved
@syffs
Copy link
Contributor Author

syffs commented Apr 8, 2019

@pi0 Thanks for the quick feedback ! Updated with requested changes.

@pi0 pi0 dismissed their stale review April 8, 2019 17:29

fixed

@pi0 pi0 changed the title [Google Tag Manager] refactoring with enabled option and $gtm.pushEvent feat(google-tag-manager): refactor, dev option and $gtm.pushEvent Apr 8, 2019
@pi0
Copy link
Member

pi0 commented Apr 8, 2019

@syffs Looks good so far. Other the little change if you can confirm current changes works fine in your project i can merge and release it.

@syffs
Copy link
Contributor Author

syffs commented Apr 8, 2019

@pi0 agreed and updated !

@syffs
Copy link
Contributor Author

syffs commented Apr 9, 2019

@pi0 I can confirm that it works great in my project:
image

@pi0 pi0 mentioned this pull request Apr 15, 2019
@pi0 pi0 merged commit d0ba40f into nuxt-community:master Apr 15, 2019
@syffs syffs deleted the gtm-refactor branch April 15, 2019 11:52
@shikinen
Copy link

shikinen commented Jul 19, 2019

It seems after adding the gtm module in nuxt.config I have no access to this.$gtm.pushEvent() from my layout component <- should I create $gtm object myself or is it 'shipped' with the gtm module?

@syffs
Copy link
Contributor Author

syffs commented Jul 19, 2019

It seems after adding the gtm module in nuxt.config I have no access to this.$gtm.pushEvent() from my layout component <- should I create $gtm object myself or is it 'shipped' with the gtm module?

@shikinen how do you configure it exactly ? here's how I set it up in nuxt.config.js

...
export default {
...
  modules: [
    ['@nuxtjs/google-tag-manager', {
      id: process.env.GTM_ID,
      pageTracking: true, // disabled by default to prevent double events when using alongside with GA
      dev: true // enable dev
    }]
  ]
...
}

then from your nuxt/vue pages/components:

<script>
export default {
  methods: {
    pushSpecificEvent() {
      this.$gtm.pushEvent({...})
    }
  }
}
</script>

@gablittledevshop
Copy link

@syffs I'm currently having the same problem as shikinen. Currently have it setup as your examples, but console logging this.$gtm outputs undefined so I can't push custom events. Is there something I'm missing?

@woutercouvaras
Copy link

woutercouvaras commented Feb 19, 2020

@syffs I'm currently having the same problem as shikinen. Currently have it setup as your examples, but console logging this.$gtm outputs undefined so I can't push custom events. Is there something I'm missing?

I've also battled with this module a bit, but I think that's largely because of my own assumptions. I still have pageTracking to figure out - which I'm working on right now, seeing as I've deployed a pretty massive site and it's using this module. I'll put in a pull request for some additional info in the README, to help those that might face similar things.

Having said all that, I managed to push events. My config is setup as described in the readme and by @syffs. Then, in my component, I have something like this (altered to be shared here):

await this.$axios
  .$post(process.env.baseUrl + '/api/send-mail', this.form)
  .then(async () => {
    await this.$gtm.pushEvent({
      event: 'contact',
      formName: 'Contact Enquiry'
    })
    this.$router.push({ name: 'thank-you' })
  })

I'm not saying this is the case for you, but I certainly made HUGE assumptions with how GTM works (I thought it was plug & play like Google Analytics). What I didn't know was that I had to setup the Tags on GTM, and then setup actions on GA for it all to work. I'm only mentioning this in case it helps someone else:)

@pi0
Copy link
Member

pi0 commented Feb 24, 2020

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

Successfully merging this pull request may close these issues.

5 participants