我已经实现了Google analytics Opt out
功能,但实际上我需要的是用户执行undo
其操作的选项。
老兄说,用户单击按钮选择退出,但随后改变了主意。无论如何,我们所有人都希望拥有该跟踪功能,因此对于用户而言,可以选择再次启用跟踪功能将是一件好事。
我遇到的问题是不确定如何处理。这篇文章的结尾是jsFiddle示例。我正在做的工作最初是包含跟踪代码,然后是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');
}
因此,当用户到达页面时,我要么实例化创建跟踪器,要么不实例化。我不清楚的是remove
财产。
此后,页面上有一个按钮,允许用户切换GA状态。
该功能与上述功能几乎相同。但我也想允许动态启用Google Analytics(分析)的选项。
我按照示例处理cookie:
// 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;
}
然后,我的想法是根据每个用户请求注册/注销跟踪器,但是我不确定自己是否做得正确。
// Remove the tracker
ga(function () {
ga('remove', gaProperty);
});
不知道是否值得一提,但是我正在Angular应用程序中实现它。
另外,之所以要有条件地进行创建和删除,是因为我记得阅读window.ga-disable-UA-XXXX-Y = true
过在发出之前必须先设置global属性的原因ga('create')
。
为了进行调试,我使用了Google的Tag Assistant,它在切换GA时报告了重复使用跟踪ID的情况。https://jsfiddle.net/vLyeszfg/18/
如您所见,我已经成功地以编程方式启用了Google Analytics(分析),但是删除却是问题所在。
我从未使用过remove,但是根据文档,它不带参数-它通过名称删除跟踪器实例,而不是给定属性的跟踪器(因此ga('remove')
删除默认跟踪器,ga('myCustomTracker.remove')
删除命名的跟踪器实例“ myCustomTracker”等)。
但是,我不确定为什么要同时启用启用退出和删除跟踪器(没有跟踪器实例时,退出是没有意义的)的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句