Skip to content

Instantly share code, notes, and snippets.

@hh-com
Created February 23, 2018 09:22
Show Gist options
  • Save hh-com/da9ff7f51695a41a7216f3f07f42323f to your computer and use it in GitHub Desktop.
Save hh-com/da9ff7f51695a41a7216f3f07f42323f to your computer and use it in GitHub Desktop.
Activate / Deactivate BUTTON for Contao Google Analytics Template
<?php
/**
* To use this script, please fill in your Google Analytics ID below
*/
$GoogleAnalyticsId = 'UA-XXXXX-X';
/**
* DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
*/
if ($GoogleAnalyticsId != 'UA-XXXXX-X' && !BE_USER_LOGGED_IN && sha1(session_id() . (!Config::get('disableIpCheck') ? Environment::get('ip') : '') . 'BE_USER_AUTH') != Input::cookie('BE_USER_AUTH')): ?>
<script>
/**
* ################################################################################
* INSERT THIS BUTTON ON YOUR PAGE
* <a id="gaOptout" href="javascript:gaOptout();">Google Analytics deaktivieren</a>
* ################################################################################
**/
var gaProperty = '<?php echo $GoogleAnalyticsId; ?>';
var disableStr = 'ga-disable-' + gaProperty;
var gadeactbtn = document.getElementById("gaOptout");
if (gadeactbtn)
{
if (document.cookie.indexOf(disableStr + '=true') > -1) {
document.getElementById("gaOptout").text = "Google Analytics aktivieren";
window[disableStr] = true;
}
else {
document.getElementById("gaOptout").text = "Google Analytics deaktivieren";
window[disableStr] = false;
}
}
function gaOptout() {
if (document.cookie.indexOf(disableStr + '=true') > -1) {
document.cookie = disableStr + '=; expires=Thu, 01 Jan 1970 00:00:01 UTC; path=/';
window[disableStr] = true;
document.getElementById("gaOptout").text = "Google Analytics deaktivieren";
}
else {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = false;
document.getElementById("gaOptout").text = "Google Analytics aktivieren";
}
}
(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', '<?= $GoogleAnalyticsId ?>', 'auto');
<?php if (Config::get('privacyAnonymizeGA')): ?>
ga('set', 'anonymizeIp', true);
<?php endif; ?>
ga('send', 'pageview');
</script>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment