【问题标题】:How to set an Google Opt Out Cookie FOR GOOGLE ANALYTICS 4 - not universal one - the new ga4如何为 GOOGLE ANALYTICS 4 设置 Google Opt Out Cookie - 不是通用的 - 新的 ga4
【发布时间】:2020-12-13 11:10:34
【问题描述】:

对于一个新项目,我想使用 Google Analytics 4。

直到今天,我还在使用普通的 Google Analytics,在我的隐私政策页面上,我有一个 Google Opt Out 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

&lt;a href="javascript:gaOptout()"&gt;Click here to opt-out of Google Analytics&lt;/a&gt;

所以现在我遇到了问题,新的 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?
任何人都可以在这里帮助我???

已经谢谢了,保持健康,享受你的生活:)

【问题讨论】:

    标签: javascript cookies google-analytics google-analytics-4


    【解决方案1】:

    你实际上拥有大部分的作品。

    获取您的 GA4 测量 ID,类似于:249888888

    GA4 标签中禁用测量的代码如下:

    window['ga-disable-MEASUREMENT_ID'] = true;

    你需要用你自己的ID替换MEASURMENT_ID,在这个例子中:249888888

    将此与您自己的代码结合起来:

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=249888888"></script>
    <script>
    // Disable tracking if the opt-out cookie exists.
    var disableStr = 'ga-disable-249888888';
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
       window['ga-disable-249888888'] = true; //this is what disables the tracking, note the measurement ID
    }
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', '249888888');
    </script>
    
    // Opt-out function (this function is unchanged)
    function gaOptout() {
      document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
      window[disableStr] = true;
    }
    </script>
    

    【讨论】:

    • 嘿 XTOTHEL,感谢您的回复。我的 measure-Id 以字母开头,看起来像 G-M7562N73457 类似的东西。不幸的是,此代码不起作用...我没有跟踪
    • 您是否在自己的项目中为 Google Analytics 4 尝试过这段代码?
    • 嘿,是的,使用 G-M756..etc,我意识到没有声明“disableStr”。
    • 如果你能发布完整的代码会很棒;)
    • 我用window['ga-disable-XXXXX']=true 为 GA (UA-XXXXX-Y) 和 GA4 (29888888) 尝试了你的解决方案,但它似乎并没有阻止对谷歌分析 api 的调用
    【解决方案2】:

    对于 Google Analytics 4,我必须使用“G-”前缀来指明 Measurement-ID。

    window['ga-disable-G-XXXXXXXXXX']=true;

    如您所见:https://support.google.com/analytics/answer/9539598?hl=en

    在上面的例子中它必须是:

    ... // Disable tracking if the opt-out cookie exists. var disableStr = 'ga-disable-G-249888888'; if (document.cookie.indexOf(disableStr + '=true') > -1) { ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2022-08-24
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2014-08-11
      相关资源
      最近更新 更多