How to configure Google Consent Mode V2

Modified on Mon, 18 Mar 2024 at 03:32 PM

illow is already a Google-certified CMP and you can implement Google Consent Mode V2


As a Google-certified CMP, you can integrate illow directly from Google Tag Manager and in this way integrate Google consent mode V2 as well. To know more about what is Google Consent mode V2, check out our latest blog.


Set up Google Consent Mode V2 using GTM (recommended)

When you implement illow through Google Tag Manager, it is already integrated automatically with Google Consent Mode V2. You can do it, and no technical experience is needed at all.


Learn here how to do it in less than 2 minutes!

Want a step-by-step setup? Check out the full guide here.




Manual setup (if you can not use GTM)

If you are integrating the Consent Banner by copying and pasting the script tag directly in the HTML code, you need to add a second code snippet to comply with Google's consent requirements. In other words, KEEP the banner script tag and ADD the following one.


1. Add the following <script> tag to the <head> of your pages:


REMEMBER TO REPLACE  <your-site-id>  with your actual Site ID shown in the platform


For Global/GDPR banners:

<script>
(function(siteId, win, doc) {
    var cookieName = 'illow-consent-' + siteId;
    var cookies = doc.cookie.split(';');
    var cookie = cookies.map((c) => c.split('=')).find(([name]) => name.trim() === cookieName);
    var consentStr = cookie ? cookie.slice(1).join('=').trim() : undefined;

    var consent = {};
    consentStr?.split('|').map((v) => v.split('=')).forEach(([k, v]) => {
      consent[k] = v === 'true';
    });

    // Initialize the data layer for Google Tag Manager (this should mandatorily be done before the Privacy Controls and Cookie Soluion is loaded)
    win.dataLayer = win.dataLayer || [];
    function gtag() {
        win.dataLayer.push(arguments);
    }
    win.gtag = win.gtag || gtag;
    
    win.gtag("consent", "default", {
        ad_storage: consent.marketing ? 'granted' : 'denied',
        analytics_storage: consent.preferences ? 'granted' : 'denied',
        functionality_storage: consent.preferences ? 'granted' : 'denied',
        personalization_storage: consent.preferences ? 'granted' : 'denied',
        security_storage: consent.preferences ? 'granted' : 'denied',
        ad_user_data: consent.marketing ? 'granted' : 'denied',
        ad_personalization: consent.marketing ? 'granted' : 'denied',
    });

    window.gtag('set', {
      'ads_data_redaction': !consent.marketing,
      'url_passthrough': !consent.marketing || !consent.preferences,
    });
})("your-site-id", window, document);
</script>


For US opt-out banners:

<script>
    (function(siteId, win, doc) {
    var cookieName = 'illow-consent-' + siteId;
    var cookies = doc.cookie.split(';');
    var cookie = cookies.map((c) => c.split('=')).find(([name]) => name.trim() === cookieName);
    var consentStr = cookie ? cookie.slice(1).join('=').trim() : undefined;

    var consent = {};
    consentStr?.split('|').map((v) => v.split('=')).forEach(([k, v]) => {
      consent[k] = v === 'true';
    });

    // Initialize the data layer for Google Tag Manager (this should mandatorily be done before the Privacy Controls and Cookie Soluion is loaded)
    win.dataLayer = win.dataLayer || [];
    function gtag() {
        win.dataLayer.push(arguments);
    }
    win.gtag = win.gtag || gtag;
    
    win.gtag("consent", "default", {
        ad_storage: consent.optedIn !== false ? 'granted' : 'denied',
        analytics_storage: consent.optedIn !== false ? 'granted' : 'denied',
        functionality_storage: consent.optedIn !== false ? 'granted' : 'denied',
        personalization_storage: consent.optedIn !== false ? 'granted' : 'denied',
        security_storage: consent.optedIn !== false ? 'granted' : 'denied',
        ad_user_data: consent.optedIn !== false ? 'granted' : 'denied',
        ad_personalization: consent.optedIn !== false ? 'granted' : 'denied',
    });

    window.gtag('set', {
      'ads_data_redaction': consent.optedIn === false,
      'url_passthrough': consent.optedIn === false,
    });
})("your-site-id", window, document);
</script>


Find attached the minified versions of the code for the different types of banners.


To learn more about Consent Mode, take a look at the official blog of the Google Marketing Platform, the Google Analytics Help or the gtag.js guide.


Having issues with the integration? Check out our Troubleshooting guide.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article