【问题标题】:Google Analytics opt-in keeps opt-out谷歌分析选择加入保持选择退出
【发布时间】:2017-06-28 12:42:25
【问题描述】:

我的代码不允许我重新选择加入 Google Analytics。重新设置 Google Analytics cookie 以选择加入或如何删除选择退出状态以再次选择加入的方法是什么?

Google Analytics 调试器

Initializing Google Analytics.
Running command: ga("create", "UA-XXXXXXX-2", "electronics.semaf.at")
Creating new tracker: t0
Aborting cookie write: User has opted out of tracking.
Aborting cookie write: User has opted out of tracking.
Running command: ga("set", "anonymizeIp", true)
Running command: ga("set", "forceSSL", true)
Running command: ga("send", "pageview")
User has opted out of tracking. Aborting hit.
Registered new plugin: ga(provide, "render", Function)

我的 Javascript 代码选择加入和退出:

<script>
      var gaProperty = 'UA-XXXXXXX-2';
      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;
        location.reload(true);
      }
      // Opt-in function
      function gaOptin() {
        document.cookie = disableStr + '=false; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
        window[disableStr] = false;
        location.reload(true);
      }
</script>
<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-XXXXXXX-2', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('set', 'forceSSL', true);
    ga('send', 'pageview');
</script>

这是选择加入或退出的部分

<script type="text/javascript">
  if(window[disableStr] == true) {
    document.getElementById("google_analytics").innerHTML = 'You are opt-out from Google Analytics. <a href="javascript:gaOptin()">Opt-in</a>';}
  else {
    document.getElementById("google_analytics").innerHTML = 'This page is using <a href="http://analytics.google.com/">Google Analytics</a>. <a href="javascript:gaOptout()">Opt-out</a>';
  }
</script>

【问题讨论】:

    标签: javascript angularjs google-analytics


    【解决方案1】:

    试试这个:

    // Opt-in function
      function gaOptin() {
        document.cookie = disableStr + '=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
        window[disableStr] = false;
        location.reload(true);
      }
    

    如果您需要有关删除 cookie 的更多信息,请阅读以下内容: w3schools Cookies

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多