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 enable after user consent? #115

Closed codeofsumit closed 4 years ago

codeofsumit commented 4 years ago

Matteo, first of all thanks a lot for this awesome plugin!

I'm building a cookie banner and as per the documentation I can see how I can load the plugin with enabled: false. What is missing from the docs is how to actually enable it again after user consent - including sending the first pageview.

Can you point me into the right direction?

Thanks in advance - all the best, Sumit

codeofsumit commented 4 years ago

I tried to call bootstrap() but no pageview is sent when calling it 🤔

hennzen commented 4 years ago

Stumbled across same issue. Looked at the API source and verified it with MatteoGabriele's reply to How to opt out after opt in
Simply calling this.$gtag.optIn() within my component method just worked.

codeofsumit commented 4 years ago

@hennzen just tried it again. When I call this.$gtag.optIn(); I don't see any "collect" event sent in my network tab that is normally sent when I navigate/load the page. These events: image

Is there one firing for you when you call optIn()?

hennzen commented 4 years ago

For me, the collect request only fires on first event (custom, pageview...) after calling optIn() (vs. upon calling it)
If that still doesn't work, try getting more insight with Google Analytics Debugger Chrome Extension

codeofsumit commented 4 years ago

@hennzen that's what I mean.

User enters page. No event. User clicks button -> optIn() First Event fired User navigates page Subsequent events are fired

The bold marked event is not happening for me.

codeofsumit commented 4 years ago

Just installed the analytics debugger extension.

This is what happens on page load when loading vue-gtag with enabled: false. image

When I call this.$gtag.optIn(), nothing happens. No logs are added.

hennzen commented 4 years ago

Cannot reproduce. I get same output using { enabled: false } on load, i.e. all events are being labeled "User has opted out of tracking. Aborting hit." Then calling optIn() (no output on this one - but what about console.log()'ing something, so you know it was called). Subsequent events being tracked and shown by the extension as expected.

codeofsumit commented 4 years ago

Well ok then we have the same behaviour. The problem is that optIn() should send the first pageview immediately(!) IMO. Without navigating anywhere. Subsequent navigation is tracked fine. I'm only concerned about the first event after calling optIn().

hennzen commented 4 years ago

I would not expect such behaviour. But you could nevertheless manually/programmatically trigger a pageview event after calling optIn() to make the current pageview count

codeofsumit commented 4 years ago

Hm ok. Then how do you track referal pages or time spent on a landing page if tracking won't start until the user navigates again? Especially Referal pages seem to be completely useless then - or am I wrong here?

MatteoGabriele commented 4 years ago

sorry for the late response. The optIn method it's just changing a global variable inside the gtag global object: there's no need to fire any events, it's not something that needs to be registered. after the global variable it's re-enabled again, then you need to do something to start triggering events again: it's a simple on/off switch built-in inside google analytics or google gtag, same pattern

window['ga-disable-MY_DOMAIN_ID'] = true/false;

you can read more here https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out

the library will basically fire events and abort them at the same time, as soon as you enable it again, those events will not be aborted anymore and you will register events in your dashboard

codeofsumit commented 4 years ago

@MatteoGabriele thanks for chiming in and taking the time to explain this. I understand now. Seems like my expectations are wrong. If this is as designed, it's more like that this is correct and my assumption that e.g. referrals aren't properly tracked is wrong.

Thanks also @hennzen ❤️

MatteoGabriele commented 4 years ago

@codeofsumit you're welcome. sorry you had to wait this long for my response. yeah this is by design from Google, not from me. I can't really predict what you want to do after re-enabling the property so you might need to trigger whatever you need by yourself manually. after that, whatever happens will be picked up again by the library