我想在一个新项目中使用Google Analytics 4。
直到今天,我一直在使用普通的Google Analytics,在我的隐私政策页面上添加了一个Google Opt Out Cookie。该Cookie是通过简单的JavaScript代码实现的。代码如下:
<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>
通过这段代码,我可以在想要设置的页面上设置Google Opt Out Cookie。
<a href="javascript:gaOptout()">点击此处选择退出Google Analytics</a>
现在我遇到了一个问题,就是在新的GA4版本中,这段代码不起作用了。在GA4的开发者页面上有一段禁用GA4的代码。
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
window['ga-disable-MEASUREMENT_ID'] = true;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'MEASUREMENT_ID');
</script>
我该如何设置按钮链接以禁用GA4?有谁能在这里帮助我吗?谢谢,保持健康,享受生活 :)
window['ga-disable-XXXXX']=true
,但似乎无法阻止对Google Analytics API的调用。 - AlexandreS