Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active May 27, 2018 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/23e183cdec2e1c9bdbc212d1e6ea13d5 to your computer and use it in GitHub Desktop.
Save BananaAcid/23e183cdec2e1c9bdbc212d1e6ea13d5 to your computer and use it in GitHub Desktop.
cookie msg (php) + analytics opt-out (js) - no message in html if it was seen allready - tracking by opt out - no jquery
<script type="text/javascript">
// Use the same ID as the property on the site
var gaProperty = 'UA-XXXXXXXX-1';
// Disable tracking if the opt-out cookie exists.
var disableStr='ga-disable-'+gaProperty;if(document.cookie.indexOf(disableStr + '=true') > -1) { window[disableStr] = true; }
// Opt-out function
function gaOptout() { document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2100 23:59:59 UTC; path=/; domain=' + document.location.hostname; window[disableStr] = true; }
// check opt-out
if (window[disableStr] !== true) {
// Change the UA-XXXXX-X to your site's ID
(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','//www.google-analytics.com/analytics.js','ga');
// cookie-less tracking
var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
if (window.localStorage) {
ga('create', gaProperty, {
'storage': 'none',
'cookieDomain': 'auto',
'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function(tracker) {
localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});
ga('set','anonymizeIp',true);
ga('set','forceSSL',true)
ga('send','pageview');
}
// old: tracking by cookie
//ga('create',gaProperty,'auto');
//
//ga('set','anonymizeIp',true);
//ga('set','forceSSL',true)
//ga('send','pageview');
}
else {
console.info('Opted out from google analytics tracking. No google tracking script loaded. Sad to see.');
}
</script>
<?php
// test: ?showcookie
// cookie message will show itself only once
if (isset($_REQUEST['showcookie']) || empty($_COOKIE["cookieMsgSeen"])) {
setcookie( "cookieMsgSeen", "yes", strtotime( "+1 year" ), "/", '.' . $_SERVER["HTTP_HOST"] );
?>
<style>
#cookieMsg + section {transition: .5s ease-out; z-index: 2000;}
#cookieMsg:not(:checked) + section {transform: translateY(100%);}
#cookieMsg + section .msgButtons > * { padding: 10px; background: #08c; color: white; max-width: 100px; display: inline-block; margin: 0 10px; text-decoration: none; }
</style>
<input type="checkbox" checked id="cookieMsg" style="display: none" />
<section style="position: fixed; bottom: 0; left: 0; background: black; text-align: left; color: white; width: 100vw; padding: 25px;">
<article style="max-width: 600px; margin: auto;">
<p><strong>Diese Seite verwendet keine Cookies</strong></p>
<p>
... um den Traffic auf dieser Website zu analysieren.
<br />
Informationen zu Ihrer Nutzung meiner Website werden aber dennoch anonym an Google übermittelt.
<br />
<a href="/datenschutz.html#cookies">Details ansehen</a>
</p>
<p style="margin: auto; text-align: center" class="msgButtons">
<label for="cookieMsg">Akzeptieren</label>
<a href="" onclick="gaOptout(); document.location.reload(); return false;" style="background: gray">Ablehnen</a>
</p>
</article>
</section>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment