Toggle google analytics - opt out / opt in

2019-08-12 13:49发布

I've implemented Google analytics Opt out feature, but what I actually need is an option for a user to undo his action.

Lats say a user clicks the button to opt out, but then changes his mind. In any case we all want to have that tracking, so would be a good thing for a user to have an option to enable tracking again.

The problem I'm having is not being sure how to handle this. At the end of this post is the jsFiddle example. What I'm doing is initially including the tracking code, and then optionally creating the tracker.

if (document.cookie.indexOf(disableGa + '=true') > -1) {
    window[disableGa] = true;

    // Remove the tracker
    ga(function () {
        ga('remove', gaProperty);
    });

} else {

    // Create the tracker
    ga('create', gaProperty, 'auto', {
        anonymizeIp: true
    });
    ga('send', 'pageview');
}

So, when the user reaches the page I either instantiate the create tracker, or not. What I'm not clear on is the remove property.

After this, on the page there is the button which allows the user to toggle GA state.

The functionality is pretty much the same as the above described one. But I also want to allow an option for the Google analytics to be dynamically enabled.

I'm handling the cookie as per the example:

// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXX-Y';

// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
    window[disableStr] = true;
}

// Opt-out function
function gaOptout() {
    document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59     UTC; path=/';
    window[disableStr] = true;
}

And then my idea was to register/unregister the tracker per user request, but I'm not sure if I'm doing it correctly.

// Remove the tracker
ga(function () {
    ga('remove', gaProperty);
});

Not sure if it's worth mentioning, but I'm implementing this in an Angular app.

Also, the reason why I'm doing the create and remove conditionally is because I remember reading that the global property window.ga-disable-UA-XXXX-Y = true has to be set before issuing the ga('create').

For debugging I'm using Google's Tag Assistant, and it is reporting duplicate use of the tracking ID when toggling the GA. https://jsfiddle.net/vLyeszfg/18/

As you might be able to see, I'm successfully programmatically enabling google analytics, but the removal is the problem.

1条回答
一夜七次
2楼-- · 2019-08-12 14:39

I have never used remove but according to the docs it does not take a parameter - it removes a tracker instance by name, not the tracker for a given property (so ga('remove') deletes the default tracker, ga('myCustomTracker.remove') removes a named tracker instance "myCustomTracker" etc.

I'm not sure however why you would bother both to enable the opt-out and remove the tracker (the opt-out is pointless when there is no tracker instance).

查看更多
登录 后发表回答