Google Analytics Opt-Out
Home
Info

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 opt-out cookie for each domain/website as well as each subdomains and subfolders. As an additional feature you find a Do Not Track detection PHP and JavaScript at the bottom of this webpage.

This Google Analytics Opt-Out cookie javascript is independently from Google Analytics.

This Google Analytics Opt-Out cookie prevented the loading of the analytics.js javascript file wich are loaded by the Google Analytics script code snippet and also the saving of the Google Analytics cookies. Already saved cookies will not be deleted at the store of the Google Analytics Opt-Out cookie. But they are ineffective because the tracking script was not loaded.

This Google Analytics Opt-Out cookie javascript differentiates between www and non-www Internet Domain Name URLs and differentiates Subdomains.

Live Example:

Javascript and the storage of cookies must be allowed in your browser.
Please activate JavaScript.
To check the Google Analytics opt-out cookie, please reload these webpage.
The Google Analytics Opt-Out Cookie is only valid for this domain and this browser.

You can control and test it with these three buttons.
    Reload Webpage
Please note: In addition, Matomo (Piwik) Tracking Tool is active on this webpage (cookies with "_pk"). Already saved Matomo (Piwik) cookies will not be deleted.

There are two different options with two different original Google Analytics code versions. The first is for Google Analytics only and the second is for Google Tag Manager with Googel Analytics. A third version with the no longer recommended old ga.js is not displayed here, but would work just as well.

You must insert the marked javascript code into your Google Analytics javascript code snippet.
<!-- Google Analytics -->

<script>
var ganalyticsdomainname = window.location.hostname;
var ganalyticscookiename = "ganalyticsoptout" + ganalyticsdomainname.replace(/\./g, "");
if (document.cookie.indexOf(ganalyticscookiename) < 0) {

window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '
UA-XXXXX-Y
', 'auto');
ga('send', 'pageview');
var gascript = document.createElement("script");
gascript.async = true;
gascript.src = "https://www.google-analytics.com/analytics.js";
document.getElementsByTagName("head")[0].appendChild(gascript, document.getElementsByTagName("head")[0]);

</script>
<!-- End Google Analytics -->
Do not forget the closing curly bracket at the bottom of the javascript code.

This line is not needed. So that no data is loaded by Google with opt-out cookie, please delete this line in the original Google Analytics code version.
<script async src='https://www.google-analytics.com/analytics.js'></script>


Google Tag Manager with Google Analytics
<!-- Global site tag (gtag.js) - Google Analytics -->

<script>
var ganalyticsdomainname = window.location.hostname;
var ganalyticscookiename = "ganalyticsoptout" + ganalyticsdomainname.replace(/\./g, "");
if (document.cookie.indexOf(ganalyticscookiename) < 0) {

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '
UA-XXXXX-Y
');
var gtmscript = document.createElement("script");
gtmscript.type = "text/javascript";
gtmscript.async = true;
gtmscript.src = "https://www.googletagmanager.com/gtag/js?id=
UA-XXXXX-Y
";
document.getElementsByTagName("script")[0].parentNode.insertBefore(gtmscript, document.getElementsByTagName("script")[0]);

</script>
Do not forget the closing curly bracket at the bottom of the javascript code.

This line is not needed. So that no data is loaded by Google with opt-out cookie, please delete this line in the original Google Analytics code version.
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-Y"></script>


And that is the HTML code snippet with javascript to save the opt-out cookie with click a button for your privacy policy. Use it instead of the Google Analytics Opt-Out from Google. There is an opt-out cookie check built-in.
Javascript and the storage of cookies must be allowed in your browser.
<div id="ganalyticsoptoutdivid" class="ganalyticsoptoutdivclass"><b>Please activate JavaScript.</b></div>
<script>
var ganalyticsdomainname = window.location.hostname;
var ganalyticscookiename = "ganalyticsoptout" + ganalyticsdomainname.replace(/\./g, "");
if (document.cookie.indexOf(ganalyticscookiename) < 0) {
var createganalyticsoptoutbutton = document.createElement("BUTTON");
var createganalyticsoptoutbuttontext = document.createTextNode("Save Google Analytics Opt-Out Cookie");
createganalyticsoptoutbutton.appendChild(createganalyticsoptoutbuttontext);
createganalyticsoptoutbutton.setAttribute("id", "ganalyticsoptoutbuttonid");
createganalyticsoptoutbutton.setAttribute("class", "ganalyticsoptoutbuttonclass");
document.getElementById("ganalyticsoptoutdivid").innerHTML = "";
document.getElementById("ganalyticsoptoutdivid").appendChild(createganalyticsoptoutbutton);
createganalyticsoptoutbutton.onclick = function ganalyticsoptout() {
var ganalyticscookienamevalue = ganalyticscookiename + "=" + "optedout;expires=Wed, 1 Jan 2100 00:00:00 UTC;path=/";
document.cookie = ganalyticscookienamevalue;
}
} else {
document.getElementById("ganalyticsoptoutdivid").innerHTML = "<b>INFO:</b> A Google Analytics opt-out cookie for this domain and this browser is already exists.";
}
</script>
To check the Google Analytics opt-out cookie, please reload these webpage.
<br>
The Google Analytics Opt-Out Cookie is only valid for this domain and this browser.
If you want the opt-out cookie to work separately for individual subfolders, you must change the specification of "path = /".

The opt-out cookie button HTML code snippet with javascript is PHP valid. You can put it in a variable like $ganalyticsoptoutbuttoncode = '...';

You can style the HTML code via CSS. There is a div box and a button with id and class inside.
div id: #ganalyticsoptoutdivid
div class: .ganalyticsoptoutdivclass
button id: #ganalyticsoptoutbuttonid
button class: .ganalyticsoptoutbuttonclass



DO-NOT-TRACK DETECTION

Do Not Track detection PHP and/or JavaScript for your privacy policy.


1.) DoNotTrack detection - JavaScript only

Live Example:

DoNotTrack detection
Your Browser Do Not Track Status: - (JavaScript must be activated)

<div id="donottrackdivid" class="donottrackdivclass">DoNotTrack detection<br><div id="donottrackdivdivid" class="donottrackdivdivclass">Your Browser <b>Do Not Track</b> Status: - (JavaScript must be activated)</div></div>
<script>
if (navigator.doNotTrack == 1 || navigator.doNotTrack == "yes" || window.doNotTrack == 1 || navigator.msDoNotTrack == 1) {
donottrackstatustxt = "Your Browser <b>Do Not Track</b> Status: <b>On</b> (assumed)";
} else {
donottrackstatustxt = "Your Browser <b>Do Not Track</b> Status: <b>Off</b> (or unknown)";
}
document.getElementById("donottrackdivdivid").innerHTML = donottrackstatustxt;
</script>



2.) DoNotTrack detection - PHP only

Live Example:

DoNotTrack detection
Your Browser Do Not Track Status: Off (or unknown)

<div id="donottrackdivid" class="donottrackdivclass">DoNotTrack detection<br><div id="donottrackdivdivid" class="donottrackdivdivclass">Your Browser <b>Do Not Track</b> Status:
<?php
if ((isset($_SERVER['HTTP_DNT']) && substr($_SERVER['HTTP_DNT'], 0, 1) === '1') || (isset($_SERVER['HTTP_X_DO_NOT_TRACK']) && $_SERVER['HTTP_X_DO_NOT_TRACK'] === '1')) {
echo '<b>On</b> (assumed)';
} else {
echo '<b>Off</b> (or unknown)';
} ?>
</div>
</div>



3.) DoNotTrack detection - PHP and JavaScript Fallback

Live Example:

DoNotTrack detection
Your Browser Do Not Track Status: Off (or unknown, Please activate JavaScript)

<?php
if ((isset($_SERVER['HTTP_DNT']) && substr($_SERVER['HTTP_DNT'], 0, 1) === '1') || (isset($_SERVER['HTTP_X_DO_NOT_TRACK']) && $_SERVER['HTTP_X_DO_NOT_TRACK'] === '1')) {
$serverhttpdonottracktxtphp = "<b>On</b> (assumed)";
} else {
$serverhttpdonottracktxtphp = "<b>Off</b> (or unknown, Please activate JavaScript)";
} ?>
<div id="donottrackdivid" class="donottrackdivclass">DoNotTrack detection<br><div id="donottrackdivdivid" class="donottrackdivdivclass">Your Browser <b>Do Not Track</b> Status: <?php echo $serverhttpdonottracktxtphp; ?></div></div>
<script>
var serverhttpdonottrackphpjs = "<?php if (isset($_SERVER['HTTP_DNT'])) { echo $_SERVER['HTTP_DNT']; } ?>";
if (serverhttpdonottrackphpjs != 1) {
if (navigator.doNotTrack == 1 || navigator.doNotTrack == "yes" || window.doNotTrack == 1 || navigator.msDoNotTrack == 1) {
donottrackstatustxtjs = "Your Browser <b>Do Not Track</b> Status: <b>On</b> (assumed)";
} else {
donottrackstatustxtjs = "Your Browser <b>Do Not Track</b> Status: <b>Off</b> (or unknown)";
}
document.getElementById("donottrackdivdivid").innerHTML = donottrackstatustxtjs;
}
</script>


The Do Not Track detection PHP and/or JavaScript will be work on mostly desktop browser.
Firefox 9.0 and upwards, Chrome/Chromium 23 and upwards, Opera 12 and upwards, Safari 5.1 and upwards, Internet Explorer 9 and upwards.

You can style the HTML code via CSS. There is a div box and a div box in the div box with separate id and class inside.
div id: #donottrackdivid
div class: .donottrackdivclass
div div id: #donottrackdivdivid
div div class: .donottrackdivdivclass

Support Penultima.de
PayPal Gefällt Mir Button
© Penultima.de - All rights reserved. Data protection statement & Imprint & Contact
Cookies in use.
More Infos