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

Add an Opt-out option for Google Analytics #417

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions