Google Tag Manager implementation
In order to enable Google consent mode v2 on your website, you need to have a cookie banner solution in place. If you already have a Cookiebot CMP subscription, the Cookiebot script will automatically send the appropriate signals to Google on consent submission. You will manually need to ensure that a default state is set though.
Ensure that you have enabled Consent Mode (see image below) and have the template updated to the newest version. If you want to implement Cookiebot via the Cookiebot CMP Template Gallery Tag inside Google Tag Manager (GTM), please refer to our GTM deployment guide instead.
Wordpress implementation
Install the Cookiebot Wordpress plugin and ensure that you have enabled consent mode (see image below) and have the plugin updated to the newest version.
Inline script implementation
The Google consent mode is an API which Cookiebot CMP integrates with.
When you have installed the Cookiebot CMP tag directly on your site, you can set default (prior consent) settings via the Google consent API-integration by adding the following code snippet to your website template.
<script data-cookieconsent="ignore"> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('consent', 'default', { 'ad_personalization': 'denied', 'ad_storage': 'denied', 'ad_user_data': 'denied', 'analytics_storage': 'denied', 'functionality_storage': 'denied', 'personalization_storage': 'denied', 'security_storage': 'granted', 'wait_for_update': 500, }); gtag("set", "ads_data_redaction", true); gtag("set", "url_passthrough", false); </script>
Region specific settings
To change the default behavior of your tags for users from certain regions, specify a region in your consent command. By providing a region value, you can fine-tune defaults based on your users' geographic locations.
If two default consent commands occur on the same page with values for a region and subregion, the one with a more specific region will take effect. For example, if you have ad_storage
set to granted
for the region US and ad_storage
set to denied
for the region US-CA, a visitor from California will have the more specific US-CA setting take effect.
These regional settings must be set in order from most to least specific.
For example:
<script data-cookieconsent="ignore"> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } // EU-specific settings
gtag('consent', 'default', {
'ad_personalization': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'analytics_storage': 'denied',
'functionality_storage': 'denied',
'personalization_storage': 'denied',
'region': [
'GB','AT','BE','BG','CH','CY','DE','DK','EE','ES','FI',
'FR','GG','GR','HR','HU','IE','IM','IT','JE','LT','LU',
'LV','MT','NL','PL','PT','RO','SE','SI','SK'
],
'wait_for_update': 500,
}); // Default settings
gtag('consent', 'default', { 'ad_personalization': 'granted', 'ad_storage': 'granted', 'ad_user_data': 'granted', 'analytics_storage': 'granted', 'functionality_storage': 'granted', 'personalization_storage': 'granted', 'security_storage': 'granted', 'wait_for_update': 500, }); gtag('set', 'ads_data_redaction', true); gtag('set', 'url_passthrough', false); </script>
URL passthrough
When a user rejects cookies, there is a fallback solution to pass the data that would normally be stored in the "_ga" cookie by adding it to the URL as a query parameter.
To enable this, set URL passthrough to "true" with gtag("set",
"url_passthrough", true);
Renaming the dataLayer
If you've chosen to rename the dataLayer, please ensure that you modify the script to use the new name of the dataLayer.
For example, if you renamed the dataLayer to "gtmDataLayer", the first line needs to be amended to resemble this: window.gtmDataLayer = window.gtmDataLayer || [];
Default values
Default consent can be set per region. You may change the values of the default consent types to "granted" or "denied" if you want to default to an opt-in before the end user has submitted consent, for example allowing for a default opt-in under CCPA.
Trigger configuration
If you previously applied cookie consent triggers to tags with "built-in consent" (such as Google Ads, Analytics, Floodlight and Conversion Linker), you can once again use default triggers like "All pages".
Consent mode and TCF
If you use TCF 2.2 in addition to Google consent mode; A TCData update (TCData.enableAdvertiserConsentMode
) allows Google to infer ad_storage, ad_personalization, and ad_user_data settings from the TC string. This will incorporate consent mode v2 updates directly into the TC string.
However, to mitigate potential issues related to network loading times, we recommend additionally implementing the consent mode script with the default consent states manually to ensure that no Google tags can fire without consent.
Comments
5 comments
@Edward from the email that Google sent out if you are an Ads user:
I have been searching this code from few days, and it has helped me very much. It is very much clear and beneficial for this type of websites
Thank you that was helpful
good information
Please sign in to leave a comment.