Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Add an Opt-out option for Google Analytics #417

Open
ghost opened this issue Jan 18, 2019 · 1 comment
Open

Add an Opt-out option for Google Analytics #417

ghost opened this issue Jan 18, 2019 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 18, 2019

This can be done by saving a cookie that, if present, will prevent the GA script from execution:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script type="text/javascript">
    var ga_property = 'UA-XXXX-Y'; // Insert GA-property
    var ga_disableStr = 'ga-disable-' + ga_property;
    if (document.cookie.indexOf(ga_disableStr + '=true') > -1) {
        window[ga_disableStr] = true;
    } else {
      // Execute Google Analytics Code
      (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', ga_property, 'auto');
      ga('set', 'anonymizeIp', true);
      ga('send', 'pageview');
    }
    function gaOptOut() {
      document.cookie = ga_disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
      window[ga_disableStr] = true;
      location.reload();
    }
  </script>
</head>
<body>
  <a href="javascript:gaOptOut()">Deactivate Google Analytics</a>
</body>

The link in the example above would be added to a new privacy page in the section for Google Analytics.

@ghost
Copy link
Author

ghost commented Jul 27, 2019

Nobody is interested in such things so closing as not helpful.

@ghost ghost closed this as completed Jul 27, 2019
@abcdefg30 abcdefg30 reopened this Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants