MatteoGabriele / vue-gtag

Global Site Tag plugin for Vue (gtag.js)
https://matteo-gabriele.gitbook.io/vue-gtag/
MIT License
864 stars 64 forks source link

How to prevent sending hit on init or bootstrap? #139

Closed vincent-lu closed 4 years ago

vincent-lu commented 4 years ago

Hi first thanks for the plugin!

I'm trying to send pageview hit on only specific pages with code below:

this.$gtag.pageview({
    page_path: '/invoice-loaded',
});

And this is the only tracking hit I want to send.

However no matter what I do (opt out initially, not enable initially etc.), whenever I optIn or bootstrap $gtag (so I can run this.$gtag.pageview()), it sends its own tracking hit.

This leads to sending tracking hit twice.

The methods I tried are either:

// opt out initially
Vue.use(VueGtag, {
  config: { id: googleId },
  enabled: false,
});
// later before sending pageview
this.$gtag.optIn(); // this sends GA hit
// then send my pageview tracking
this.$gtag.pageview({ // this also sends GA hit
    page_path: '/invoice-loaded',
});

or

// not load script initially
Vue.use(VueGtag, {
  config: { id: googleId },
  bootstrap: false,
});
// then run bootstrap before sending my pageview tracking
bootstrap().then(gtag => { // this sends GA hit
  this.$gtag.pageview({ // this also sends GA hit
    page_path: '/invoice-loaded',
  });
});

Can someone please point me to the right direction so I don't send tracking hit twice? Thanks.

MatteoGabriele commented 4 years ago

what are you tracking twice? the /invoice-loaded?

MatteoGabriele commented 4 years ago

I think I found the bug which is also related to another one. I will try to push a fix asap

vincent-lu commented 4 years ago

what are you tracking twice? the /invoice-loaded?

No, this.$gtag.optIn(); or bootstrap() triggers a different tracking hit. But a tracking hit nonetheless.

My guess is that they trigger a gtag config command without path specified.

MatteoGabriele commented 4 years ago

yeah on bootstrap a hit is sent by default because I wrongly set it up like analytics. but there's no need to set trackers on gtag

MatteoGabriele commented 4 years ago

possibly fixed by this 😆 https://github.com/MatteoGabriele/vue-gtag/pull/142