Sonntag, 31. Dezember 2017

Google Analytics Opt-out cookie javascript

For alternative browsers and mobile devices for a Google Analytics Opt-Out you can use this simple javascript with a function and set a cookie.

This Google Analytics Opt-out cookie javascript use the function wich is inside the Google Analytics Opt-out browser AddOn.

This Google Analytics Opt-out cookie javascript works with older and newer Google Analytic script code snippets.

This Google Analytics Opt-out cookie javascript works with the ga.js and the analytics.js javascript files wich are loaded by the Google Analytics javascript code snippet.

You must insert this javascript code directly before your Google Analytics javascript code snippet. Here both shown together.

<!-- Google Analytics Opt-out -->
<script type="text/javascript">
var domainname = window.location.hostname;
var gaoptoutcookiename = "gaoptoutcookie" + domainname.replace(/\./g, "");
if (document.cookie.indexOf(gaoptoutcookiename) >= 0) {
var a = document.createElement("script");
a.type = "text/javascript";
a.innerText = 'window["_gaUserPrefs"] = { ioo : function() { return true; } }';
document.documentElement.insertBefore(a, document.documentElement.firstChild);
function googleanalyticsoptout() {
var gaoptoutcookienamevalue = gaoptoutcookiename + "=" + "accepted;" + "expires=Wed, 1 Jan 2100 00:00:00 UTC;" + "path=/";
document.cookie = gaoptoutcookienamevalue;
<!-- End Google Analytics Opt-out -->

<!-- Google Analytics -->
<script>||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
<script async src=''></script>
<!-- End Google Analytics -->

And that is the HTML code snippet for your privacy policy.

<a href="javascript:googleanalyticsoptout();">Google Analytics Opt-out cookie to save</a>

It is possible that Google will change the function in her script, then it will not work anymore. You can then extract the function yourself from the Google Analytics Opt-out Browser AddOn and replace it with the old one.

No guarantee. No warranty. No support.
Use at your own risk.