【问题标题】:How to set the google analytics opt-out cookie for sub-domains如何为子域设置谷歌分析选择退出 cookie
【发布时间】:2018-04-05 11:55:17
【问题描述】:

我正在设置 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>


非常感谢!

【问题讨论】:

    标签: javascript cookies google-analytics


    【解决方案1】:

    域名需要设置为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;
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多