javascript - 如何为子域设置谷歌分析选择退出 cookie

标签 javascript cookies google-analytics

我按照此处所述设置 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/

相关文章:

google-analytics - 实时数据显示所选域不存在的页面的访问量

javascript - 使用身份验证时 Angularjs 路由断断续续

javascript - Node 应用: Http to Https redirection does not work for localhost

c++ - QNetworkCookieJar 中的 setCookiesFromUrl() 和 cookiesForUrl

javascript - 刷新时维护表单元素状态

google-analytics - 直接流量 Google Analytics 100% 页面跳出率不到一秒

javascript - 即使 javascript 函数返回 false,表单也会提交

javascript - Angular View 未加载

ruby-on-rails - 如何使用 Ruby 获取网站 (url) cookie 列表

google-analytics - Universal Analytics生成了两个收集请求