我们希望为我们的网站访问者实现禁用跟踪的选项。
我已经阅读了谷歌分析opt-out dev guide,但我仍然不完全清楚如何做到这一点。
当一个按钮被点击时仅仅触发window['ga-disable-GA_TRACKING_ID'] = true;就足够了吗?
Google Analytics会记住该用户的设置吗,还是每次访问时都会记住?
如果我有第二个按钮将其设置为false,用户是否可以重新启用跟踪
发布于 2018-05-10 22:01:33
将以下内容粘贴到分析代码之前的head标签中(将UA替换为您自己的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>发布于 2018-05-14 21:28:34
谢谢你的回复!
自从我回复了你的帖子,我也想出了一个解决方案,但还没有找到一种方法来删除cookie,如果用户选择退出。虽然数据不会被发送回Google,但删除cookie以完成操作会更好。这是我想出来的。
<script async type="text/javascript">
function optIn() {
sessionStorage.setItem('aValue', false);
}
function optOut() {
sessionStorage.setItem('aValue', true);
setCookie('_gat_gtag_TRACKING-ID', 'value', 0);
}
var getCookieValue = sessionStorage.getItem('aValue');
var b = (getCookieValue == 'true');
window['ga-disable-TRACKING-ID'] = b;
</script>这是由
<form class="form-inline">
<button value="optin" onClick="optIn();" class="btn btn-primary mb-2">Opt In</button>
<br />
<button value="optout" onClick="optOut();" class="btn btn-primary mb-2">Opt Out</button>
</form>这样做就是将窗口‘’ga disable-TRACKING-ID‘设置为true或false,这取决于访问者的响应,该响应存储在SessionStorage中。希望这对任何人都有帮助,以及上面的解决方案。
发布于 2020-09-29 17:35:15
GTag Opt In是一个工具,它可以为你做这件事,并允许你在默认情况下禁用cookies,并在以后启用它们,再次禁用,再次启用,等等。
禁用cookies
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-Y"></script>
-- <script>
-- window.dataLayer = window.dataLayer || [];
-- function gtag(){dataLayer.push(arguments);}
-- gtag('js', new Date());
-- gtag('config', 'UA-XXXXXX-Y');
-- </script>在实现GA时,只需导入gtag.js脚本并删除GA初始化即可对其进行更新。
在用户接受cookies时启用GTag
GTag Opt In是一个在用户接受/拒绝cookies时启用和禁用GA的工具。
<script src="https://www.npmcdn.com/gtag-opt-in@2.0.0/dist/index.js"></script>
<script>
GTagOptIn.register('UA-XXXXXX-Y');
</script>
<a href="javascript:GTagOptIn.optIn()">ACCEPT</a>
<a href="javascript:GTagOptIn.optOut()">REJECT</a>首先,加载库。
其次,注册GA跟踪ID。
第三,optIn和optOut函数被绑定到用户操作的接受/拒绝。
你可以在How To Implement Google Analytics With Opt In上阅读更多关于它的内容。
https://stackoverflow.com/questions/48468226
复制相似问题