Controlling Google Analytics Universal with Cookiebot

Please note that this is no longer being updated. Kindly visit the new Cookiebot support area (https://support.cookiebot.com/hc/en-us) for updated guides, articles and FAQs. If you do not find the answer to your question there, you are welcome to post it in the Cookiebot community (https://support.cookiebot.com/hc/en-us/community/topics).

This article shows you how to use Cookiebot to control Google Analytics Universal.

The default method for implementing prior consent on GA Universal


To enable prior consent on GA Universal, apply the attribute "data-cookieconsent" to the GA Universal script tag on your website. Set the comma-separated value of the attribute to "statistics" in accordance with the types of cookies being set by the script. Finally change the attribute "type" from "text/javascript" to "text/plain". Make sure to use your own GA Universal code instead of 'UA-0000000-0'

<script type="text/plain" data-cookieconsent="statistics">
    (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-00000000-0', 'auto');
    ga('send', 'pageview');
</script>

An alternative method if you want to load GA Universal dynamically


Insert the following JavaScript callback function 'CookiebotCallback_OnAccept' in your website's HTML header. Make sure to use your own GA Universal code instead of 'UA-0000000-0':

function CookiebotCallback_OnAccept()
{
    if(Cookiebot.consent.statistics)
    {
        enableGoogleAnalyticsUniversal();
    }
}

function enableGoogleAnalyticsUniversal() {
    (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-00000000-0', 'auto');
    ga('send', 'pageview');
}

Read more: Is my use of Google Analytics GDPR and ePR compliant?

Feedback and Knowledge Base