用户选择退出使用gtag.js [英] User opt-out with gtag.js
问题描述
我们正在寻求为网站访问者实施该选项以禁用跟踪.
We're looking to implement the option for our website visitors to disable tracking.
我已阅读Google Analytics(分析)选择退出开发人员指南,但是我仍然不清楚如何做到这一点.
I've read the Google Analytics opt-out dev guide but I'm still not totally clear on how to do this.
单击按钮时仅触发window['ga-disable-GA_TRACKING_ID'] = true;
就足够了吗?
Is it sufficient to just trigger window['ga-disable-GA_TRACKING_ID'] = true;
when a button is clicked?
Google Analytics(分析)会记住该用户的设置吗?还是基于每次访问的设置?
Will Google Analytics remember the setting for this user, or is it on a per-visit basis?
如果我还有另一个将其设置为false
的按钮,用户可以重新启用跟踪吗?
Can the user re-enable tracking if I have a second button that sets it to false
?
推荐答案
将以下内容粘贴到Google Analytics(分析)代码之前的head标签中(用您自己的UA替换UA)
Paste the following into the head tag before the Analytics code (replacing the UA with your own UA)
<script>
// 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;
}
</script>
并使用它来触发它:
<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
这篇关于用户选择退出使用gtag.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!