我按照此处所述设置 google opt-out cookie,并设置 ga-disable cookie。 https://developers.google.com/analytics/devguides/collection/gajs/#disable
不幸的是,该 cookie 仅针对/example.com 设置,并且在 xy.example.com 等任何子域上,无法识别 ga-disable cookie。
我希望选择退出 cookie 在所有子域上都有效。
我该如何做到这一点?
正在运行并设置 Google 选择退出 cookie 的代码:
// Set to the same value as the web property used on the site
var gaProperty = 'UA-yyyyyy';
// 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>
非常感谢!
最佳答案
域名需要设置为domain=.example.com;
重要的是使选择退出 cookie 在所有子域上工作的前导点。
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; domain=.example.com; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
关于javascript - 如何为子域设置谷歌分析选择退出 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914688/