Info
Content

Working with Google Analytics

In order to block Google Analytics codes unless consent is given, you can simply change your Google Analytics code.

Version with Google TagManager-Code

Your orginal code will look like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-1111111-01"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1111111-01');
</script>

In order to block the code, all you need to do is to add a few characters in the first line (read more about blocking codes here). The new code will look like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async class="cmplazyload" data-cmp-vendor="s26" data-cmp-src="https://www.googletagmanager.com/gtag/js?id=UA-1111111-01"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1111111-01');
</script>

Version with Google Analytics-Code

Your original code will look like this:

<script type="text/javascript">		
(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');

ga('create', 'UA-11111111-1', 'mywebsite.de');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>

In order to block the code, all you need to do is to add a few lines. The new code will look like this:

<script type="text/javascript">
(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.setAttribute('class','cmplazyload');
a.setAttribute('data-cmp-src',g);
a.setAttribute('data-cmp-vendor',"s26");                         
m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-11111111-1', 'mywebsite.de');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
Back to top