Learning
Learning

Reputation: 20011

Opt-In opt-Out for Google analytics not working

I am using piece of code from other source to give user option to Opt-In & Opt-Out of Google Analytics. below code is not triggering any option, it just loads the page and nothing else.

What is the best way to do it and is th GDRP complient

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

Upvotes: 1

Views: 1516

Answers (1)

daratz
daratz

Reputation: 21

I reworked your code, you didn't have gaOptout function. It' working now:

    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);
    }

Upvotes: 2

Related Questions