首页 > 解决方案 > 如何修复 Google Analytics 的选择加入解决方案

问题描述

我正在建立一个新网站,并希望使用 Google Analytics 跟踪网页浏览量。由于我住在欧盟,我需要遵守 GDPR 并实施一个选择加入解决方案,允许用户在开始跟踪之前允许或拒绝 cookie。我偶然发现了 Cookie Consent 并尝试使用其选择加入解决方案,但它似乎不起作用。即使出现横幅,Google Analytics(分析)也不会在您点击“接受”后开始跟踪。非常感谢您的帮助。

我已经尝试过这个解决方案,但不幸的是它没有帮助:https ://github.com/osano/cookieconsent/issues/489

我也没有成功地尝试过这段代码:https ://www.dair-media.net/blog/google-analytics-mit-optin-implementieren/

<!-- Instead of 'UA-XXXXXXXXX-X' I will be using my individual tag -->
<script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"></script>
    <script>
    window.addEventListener("load", function(){
    window.cookieconsent.initialise({
      "palette": {
        "popup": {
          "background": "#dcdcdc",
          "text": "#000000"
        },
        "button": {
          "background": "#7fb2e5",
          "text": "#000000"
        }
      },
      "theme": "classic",
      "position": "bottom-left",
      "type": "opt-in",
      "content": {
        "message": "Some text",
        "dismiss": "Deny",
        "deny": "Deny",
        "allow": "Allow Cookies",
        "link": "Learn more",
        "href": "somelink"
      },
        onStatusChange: function(status, chosenBefore) {
         var type = this.options.type;
         var didConsent = this.hasConsented();
         if (type == 'opt-in' && didConsent) {
            window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
            ga('create', 'UA-XXXXXXXXX-X', 'auto');
            ga('set', 'anonymizeIp', true);
            ga('send', 'pageview');
            var gascript = document.createElement("script");
            gascript.async = true;
            gascript.src = "https://www.google-analytics.com/analytics.js";
            document.getElementsByTagName("head")[0].appendChild(gascript, document.getElementsByTagName("head")[0]);  
         }
         if (type == 'opt-out' && !didConsent) {
          window['ga-disable-UA-XXXXXXXXX-X'] = true;
         }
        },
        onRevokeChoice: function() {
         var type = this.options.type;
         if (type == 'opt-in') {
         window['ga-disable-UA-XXXXXXXXX-X'] = true;
         }
         if (type == 'opt-out') {
            window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
            ga('create', 'UA-XXXXXXXXX-X', 'auto');
            ga('set', 'anonymizeIp', true);
            ga('send', 'pageview');
            var gascript = document.createElement("script");
            gascript.async = true;
            gascript.src = "https://www.google-analytics.com/analytics.js";
            document.getElementsByTagName("head")[0].appendChild(gascript, document.getElementsByTagName("head")[0]);  

         }
        }
    })
});
</script>
    <script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXXXXXXX-X';

// 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;
  alert('Some alert');
}
</script>
</html>

如果您打开该页面,则没有 cookie,这当然是我想要的。但是,即使您单击“允许 cookie”,它们仍然不会出现。

标签: javascripthtmlcookiesgoogle-analyticscookieconsent

解决方案


选择加入 Google Analytics 的 cookie 同意,请参见此处


推荐阅读