【问题标题】:User opt-out with gtag.js用户选择退出 gtag.js
【发布时间】:2020-09-29 17:35:15
【问题描述】:

我们正在寻求为我们的网站访问者实施禁用跟踪的选项。

我已经阅读了 Google Analytics opt-out dev guide,但我仍然不完全清楚如何做到这一点。

点击按钮时触发window['ga-disable-GA_TRACKING_ID'] = true;就足够了吗?

Google Analytics 会记住该用户的设置,还是每次访问都会记住?

如果我有第二个按钮将其设置为false,用户能否重新启用跟踪?

【问题讨论】:

  • 谁能分享一些对此的见解?
  • 你在这方面有什么收获吗?我一直在尝试自己做这件事,但没有成功。
  • 我做到了!昨天一个朋友的朋友帮了我。在网上找到这个解决方案真是太难了。我刚刚用解决方案更新了我的问题。
  • 很高兴你把它整理好了!我已经从您的标题中删除了“已解决的标签”,与论坛不同,StackOverflow 不需要指示存在有效的解决方案 - 相反,单击答案旁边的“勾号”图标将其标记为解决方案。

标签: javascript google-analytics gtag.js


【解决方案1】:

将以下内容粘贴到 Analytics 代码之前的 head 标记中(将 UA 替换为您自己的 UA)

<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>

并使用它来触发它:

<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>

【讨论】:

    【解决方案2】:

    感谢您的回复!

    自从我回复了您的帖子后,我也想出了一个解决方案,但如果用户选择退出,我还没有找到删除 cookie 的方法。虽然数据不会发送回 Google,但最好删除 cookie 以完成。这是我想出的。

    <script async type="text/javascript">
    
    function optIn() {
        sessionStorage.setItem('aValue', false);
    }
    
    function optOut() {
        sessionStorage.setItem('aValue', true);
        setCookie('_gat_gtag_TRACKING-ID', 'value', 0); 
    
    }
    
    var getCookieValue = sessionStorage.getItem('aValue');
    
    var b = (getCookieValue == 'true');
    
    window['ga-disable-TRACKING-ID'] = b;
    
    </script>
    

    这是由

    触发的
    <form class="form-inline">
    <button value="optin" onClick="optIn();" class="btn btn-primary mb-2">Opt In</button>
    <br />
    <button value="optout" onClick="optOut();" class="btn btn-primary mb-2">Opt Out</button>
    </form>
    

    因此,这样做是将 window['ga-disable-TRACKING-ID'] 设置为 true 或 false,具体取决于访问者的响应,该响应存储在 SessionStorage 中。希望这对任何人以及上述解决方案都有帮助。

    【讨论】:

      【解决方案3】:

      GTag Opt In 是一个为您执行此操作的工具,此外还允许您在默认情况下禁用 cookie 并稍后启用它们,再次禁用,再次启用等等。

      禁用 cookie

         <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-Y"></script>
      -- <script>
      -- window.dataLayer = window.dataLayer || [];
      -- function gtag(){dataLayer.push(arguments);}
      -- gtag('js', new Date());
      -- gtag('config', 'UA-XXXXXX-Y');
      -- </script>
      

      在实施 GA 时,只需导入 gtag.js 脚本并删除 GA 初始化即可对其进行更新。

      当用户接受 cookie 时启用 GTag

      GTag Opt In 是一个在用户接受/拒绝 cookie 时启用和禁用 GA 的工具。

      <script src="https://www.npmcdn.com/gtag-opt-in@2.0.0/dist/index.js"></script>
      
      <script>
        GTagOptIn.register('UA-XXXXXX-Y');
      </script>
      
      <a href="javascript:GTagOptIn.optIn()">ACCEPT</a>
      <a href="javascript:GTagOptIn.optOut()">REJECT</a>
      

      首先,加载库。
      二、注册GA tracking ID。
      第三,optInoptOut 函数绑定到用户操作的接受/拒绝。

      您可以在How To Implement Google Analytics With Opt In 上阅读更多相关信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-08
        • 2021-08-19
        相关资源
        最近更新 更多