mib200 / vue-gtm

Simple implementation of Google Tag Manager in Vue.js 2.0
Apache License 2.0
242 stars 85 forks source link

spa router change not working #88

Closed novaknole closed 4 years ago

novaknole commented 4 years ago

I have the following:

import VueGtm from 'vue-gtm';

Vue.use(VueGtm, {
  id: config.services.googleTag.id,
  queryParams: config.services.googleTag.gtm_auth ? { // Add url query string when load gtm.js with GTM ID (optional)
    gtm_auth: config.services.googleTag.gtm_auth,
    gtm_preview: config.services.googleTag.gtm_preview,
    gtm_cookies_win: config.services.googleTag.gtm_cookies_win
  } : {},
  defer: false, // defaults to false. Script can be set to `defer` to increase page-load-time at the cost of less accurate results (in case visitor leaves before script is loaded, which is unlikely but possible)
  enabled: true, // defaults to true. Plugin can be disabled by setting this to false for Ex: enabled: !!GDPR_Cookie (optional)
  debug: true, // Whether or not display console logs debugs (optional)
  vueRouter: router, // Pass the router instance to automatically sync with router (optional)
});

I added history change event and variables in GTM. If I refresh the page, google analytics can see the url in reports->realtime->overview. but if I change the route by router-link, it doesn't show anything at all(no changes in google analytics).

I even tried this in router watcher:

this.$gtm.trackView('MyScreenName-'+Math.random(), 'currentpath-' + Math.random());

It comes here, logs everything, in the debug, it shows this event is thrown, but in google analytics, still nothing until i refresh the page.

Any idea what I might be missing ? should I also have vue-analytics plugin installed ? but why ? this plugin should be doing that..

NOTE: I am using custom environments..

pehleme commented 4 years ago

Need add name in router object to work, like: { path: '/', component: Home, name: 'home' }