Javascript 选择加入或退出谷歌分析不起作用

Javascript 选择加入或退出谷歌分析不起作用,javascript,google-analytics,Javascript,Google Analytics,我正在使用来自其他来源的一段代码,为用户提供选择加入和退出谷歌分析的选项。下面的代码没有触发任何选项,它只是加载页面,没有其他内容 做这件事的最佳方式是什么?GDRP是否符合要求 <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title></title> <h1> Hello This is Test</h1&

我正在使用来自其他来源的一段代码,为用户提供选择加入和退出谷歌分析的选项。下面的代码没有触发任何选项,它只是加载页面,没有其他内容

做这件事的最佳方式是什么?GDRP是否符合要求

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title></title>
  <h1> Hello This is Test</h1>
</head>
<body>

  <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>
<script>
      var gaProperty = 'UA-XXXXXXX-2';
      var disableStr = 'ga-disable-' + gaProperty;
      if (document.cookie.indexOf(disableStr + '=true') > -1) {
        window[disableStr] = true;
      }
        // 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);
          }
      // 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>
</body>
</html>

你好,这是考试
如果(窗口[disableStr]==true){
document.getElementById(“google_analytics”).innerHTML=“您选择退出google analytics。”;}
否则{
document.getElementById(“google_analytics”).innerHTML='此页面正在使用';
}
var gaProperty='UA-XXXXXXX-2';
var disableStr='ga disable-'+gaProperty;
if(document.cookie.indexOf(disableStr+'=true')>-1){
window[disableStr]=true;
}
//选择加入功能
函数gaOptin(){
document.cookie=disableStr+'=false;expires=Thu,1970年1月1日00:00:00 UTC;path=/;';
window[disableStr]=false;
位置。重新加载(true);
}
//选择加入功能
函数gaOptin(){
document.cookie=disableStr+'=false;expires=Thu,2009年12月31日23:59:59 UTC;path=/';
window[disableStr]=false;
位置。重新加载(true);
}
(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口、文档、“脚本”和https://www.google-analytics.com/analytics.js","ga",;
ga(“创建”、“UA-XXXXXXX-2”、“自动”);
ga('set','anonymizeIp',true);
//ga('set','forceSSL',true);
ga(‘发送’、‘页面浏览’);

我重新编写了您的代码,您没有gaOptout功能。它现在正在工作:

    var gaProperty = 'UA-XXXXXXX-2';
    var disableStr = 'ga-disable-' + gaProperty;
    if(document.cookie.indexOf(disableStr + '=true') > -1) {
            $("#ga").html('You are opt-out from Google Analytics. <a href="javascript:gaOptin()">Opt-in.</a>');
    }
    else {
            $("#ga").html('This page is using <a href="http://analytics.google.com/" target="_blank">Google Analytics</a>. <a href="javascript:gaOptout()">Opt-out.</a>');
    }
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
            window[disableStr] = true;
    }
    // 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);
    }
    // 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);
    }
var-gaProperty='UA-XXXXXXX-2';
var disableStr='ga disable-'+gaProperty;
if(document.cookie.indexOf(disableStr+'=true')>-1){
$(“#ga”).html('您选择退出谷歌分析');
}
否则{
$(“#ga”).html('此页面正在使用');
}
if(document.cookie.indexOf(disableStr+'=true')>-1){
window[disableStr]=true;
}
//选择加入功能
函数gaOptin(){
document.cookie=disableStr+'=false;expires=Thu,1970年1月1日00:00:00 UTC;path=/;';
window[disableStr]=false;
位置。重新加载(true);
}
//选择退出功能
函数gaOptout(){
document.cookie=disableStr+'=true;expires=Thu,2009年12月31日23:59:59 UTC;path=/';
window[disableStr]=true;
位置。重新加载(true);
}
gaOptin()
定义了两次,我想应该是
gaOptin()
gaOptOut()