gtm-support / vue-gtm

Simple implementation of Google Tag Manager for Vue
https://www.npmjs.com/package/@gtm-support/vue-gtm
MIT License
198 stars 23 forks source link

Google Tag Manager tag is not firing when viewing the page; we have to reload the pageBug: #358

Closed ngocvinh208 closed 1 year ago

ngocvinh208 commented 1 year ago

Info

Tool Version
Plugin v2
Vue v2.6.12
Node v14
OS mac

Input

Configure it in main.js

  import VueGtm from '@gtm-support/vue2-gtm';
  export default function(Vue, { router, head, isClient }) {

      if( process.isClient){
          Vue.use(VueGtm, {
              id: 'GTM-PR7VTQG',
              vueRouter: router,
              enabled: true,
              debug: true,
          });
      }
    }

Configure it in my component

export default {
   name: "WpPage",
   components: {
     BlockBase: () => import('../components/BlockBase.vue'),
     MetaInfo: () => import('../components/MetaInfo.vue'),
     EventsCollective: () => import('../components/EventsCollective.vue'),
     NewsCollective: () => import('../components/NewsCollective.vue'),
     ArchivedEvents: () => import('../components/ArchivedEvents.vue'),
   },
   mounted() {
     var mainTitle = this.$page.wpPage.title;
     this.$gtm.trackView(mainTitle, window.location.pathname)
   },
 };  

Output or Error

GTM tag is not firing the website

Expected Output

I am expecting GTM to fire the website.

Additional Context

I'm using Gridsome to build my website. When trying to build with Vue GTM module Google Tag Manager tag is not firing when viewing the page so we have to reload the page. I think because this is a third-party service, it only renders itself once per page load.?

Shinigami92 commented 1 year ago

You wrote you using Vue 2.6 :thinking: But the Changelog explicitly mention that the min version for v2 is v2.7 https://github.com/gtm-support/vue-gtm/blob/vue2-gtm/CHANGELOG.md#breaking-change

So could you downgrade to v1.3 and test if that is the problem?

ngocvinh208 commented 1 year ago

I've already used v1.3, but I wrote v2.0 in my question haha. I found it by using "watch" to fire content_view. Thank you anyway.

watch: {
    $route(to, from) {
      if (this.$gtm) {
        this.$gtm.trackView(this.$page.wpPage.title, this.$page.wpPage.path);
        window.dataLayer.push({event: 'pageview'});
      }
    }
  },
Shinigami92 commented 1 year ago

I found it by using "watch" to fire content_view.

Does this mean, it is solved? I will close this then Feel free to reopen if I misunderstood, but please add then a reproducible example repo so I can debug, or try to open a PR on your own