切换谷歌分析 - 退出/选入 [英] Toggle google analytics - opt out / opt in

查看:102
本文介绍了切换谷歌分析 - 退出/选入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现 Google analytics选择退出功能,但实际需要的是用户选择撤销他的行为。

拉特说,用户点击按钮退出,但随后改变了他的想法。在任何情况下,我们都希望进行该跟踪,因此对于用户有一个选项来重新启用跟踪是一件好事。



我遇到的问题不确定如何处理这个问题。在这篇文章的最后是jsFiddle的例子。
我在做的是最初包括跟踪代码,然后可选地创建跟踪器



<$如果(document.cookie.indexOf(disableGa +'= true')> -1){
window [disableGa] = true;
$ b $ //删除跟踪器
ga(function(){
ga('remove',gaProperty);
});
$ b} else {

//创建追踪器
ga('create',gaProperty,'auto',{
anonymizeIp:true
});
ga('send','pageview');





$ b因此,当用户到达页面时,我要么实例化创建跟踪器,要么不。
我不清楚的是 remove 属性。



在此之后,在页面上有允许用户切换GA状态的按钮。

功能与上述相同。但我也希望允许动态启用Google分析的选项。



我按照以下示例处理Cookie:

  //设置为与站点上使用的web属性相同的值
var gaProperty ='UA-XXXX-Y';

//禁用跟踪,如果选择退出cookie存在。
var disableStr ='ga-disable-'+ gaProperty;
if(document.cookie.indexOf(disableStr +'= true')> -1){
window [disableStr] = true;


//退出函数
函数gaOptout(){
document.cookie = disableStr +'= true;到期= 2099年12月31日星期四23:59:59 UTC;路径= /;
窗口[disableStr] = true;
}

然后我的想法是注册/取消注册跟踪器每个用户的请求,但

  //删除跟踪器
ga(function( ){
ga('remove',gaProperty);
});

不知道它是否值得一提,但我正在使用Angular应用程序来实现它。



另外,我为什么有条件地创建和删除是因为我记得读到全局属性 window.ga-disable-UA-XXXX -Y = true 必须在发出 ga('create')之前设置。



对于调试,我使用 Google的标签助理,并且在切换GA时报告跟踪ID的重复使用。
https://jsfiddle.net/vLyeszfg/18/



正如您可能看到的,我已成功通过编程启用谷歌分析,但移除是问题所在。 方案

我从来没有使用删除,但根据文档,它不接受参数 - 它通过名称删除跟踪器实例,而不是给定属性的跟踪器(所以 ga('remove')删除默认的跟踪器, ga('myCustomTracker.remove')删除一个指定的跟踪器实例myCustomTracker等。



我不确定为什么你会打扰选择退出和移除跟踪器(当没有跟踪器实例时选择退出是毫无意义的)。


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.

解决方案

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).

这篇关于切换谷歌分析 - 退出/选入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆