This repository was archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Open
Labels
@ghost
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=2099年12月31日 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.