Cookie Consent Integration with Google Tag Manager
Google Tag Manager (GTM) is a tool that allows you to manage and deploy tags (snippets of code or tracking pixels) on your website without having to modify your site code.
If you use Google Tag Manager (GTM) to inject cookies on your website and manage site content, you can configure GTM so that the scripts are controlled by visitors' consent preferences.
This section of the guide explains how to set up GTM to take advantage of these changes.
This article is not a complete guide to Google Tag Manager. Consult official Google Tag Manager documentation for more detailed information about setting up and using GTM.
Key Terms
-
OptanonConsent: the OneTrust first party cookie that fires when implementing a Cookie Consent Banner script.For more information, see OneTrust Cookies.
-
OnetrustActiveGroups: the OneTrust data layer variable that stores users' consent preferences. -
OneTrustGroupsUpdated: the OneTrust event that fires when users' consent preferences are updated.
How it Works
To pass user consent data to Google Tag Manager, the Cookie Consent tool uses variables. Cookie Consent supports JavaScript variables and data layer variables. OneTrust recommends using the GTM data layer and data layer variables.
Cookie Consent uses the existing dataLayer object or creates a new one if it does not already exist. If your site creates a dataLayer object, ensure that this does not overwrite the one Cookie Consent creates.
Cookie Consent stores user consent data in two places. You can implement this integration using one of two methods outlined below:
- Implement with OnetrustActiveGroups
-
this stores the user consent data in a comma delimited string and repopulates on every page load once the script is executed and when the user updates their consent preferences.
For example, the data contained in the
datalayervariable value might be ,C0001,C0002,C0003,C0004, which means all the cookies associated with these IDs have been given consent to fire on the user's browser.For more information on the data layer variable, see OnetrustActiveGroups – Using the Data Layer Object.
- Implement with OptanonConsent
-
Similar to the
OnetrustActiveGroupsdata layer variable setup, firing and blocking triggers can be created using theOptanonConsentcookie. This is a OneTrust first-party cookie set on the browser when the banner CDN script is integrated that allows you to trigger client tags.Along with other values, the cookie includes a
groupsattribute. The value of thegroupsattribute includes all the category IDs with a binary value of 0, meaning the group is inactive, or 1, meaning the group is active. Because this attribute is updated every time a user makes updates to their consent preferences, you can leverage theOptanonConsentcookie to set up triggers in GTM.For more information, see OneTrust Cookies.
For both methods, it is required to set up OneTrust triggers and assign them to appropriate tags.
When a user updates their consent preferences or on page load, the dataLayer event OneTrustGroupsUpdated is triggered. You can use this event when creating triggers to apply to your tags.
By creating a GTM Custom Variable and triggers, you can configure GTM tags to trigger only when specific consent groups within OnetrustActiveGroups are present.
You can implement this integration in one of two ways: uploading and merging a provided container file or by manually establishing triggers for each tag.
If any technology, such as an ad blocker, prevents GTM from firing, the OT cookie script will not fire, which could lead to noncompliance.
Important Notes about Google Tag Manager Integration
-
The Google Tag Manager rule operator and value fields can be customized if a different behavior is required than contained in the examples in this section.
-
Auto-Blocking is capable of blocking Google Tag Manager. If this occurs, use one of the follow methods to unblock Google Tag Manager:
CODEj.setAttributeNode(d.createAttribute('data-ot-ignore'));CODEj.setAttribute('class','optanon-category-C0001');
Example:
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.setAttributeNode(d.createAttribute('data-ot-ignore'));
j.async = true;j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-XXXXXXX');
</script>
<!-- End Google Tag Manager -->
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.setAttributeNode(d.createAttribute('data-ot-ignore'));
j.async = true;j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-XXXXXXX');
</script>
<!-- End Google Tag Manager -->
To deploy OneTrust scripts from Google Tag Manager
The first step of this integration is to determine if you want to add OneTrust CDN scripts in the HTML header section of your site or deploy scripts from Google Tag Manager.
OneTrust recommends deploying the CDN scripts from one location only. If you deploy the CDN scripts from Google Tag Manager, please ensure you place the GTM header script as the first script in your source code to ensure any other scripts on your source code that fire cookies on the website are loaded after OneTrust.
After the OneTrust CDN script is added, you must create a data layer variable in GTM that saves user preferences if you did not import the variable into your GTM container with the OneTrust triggers.
Once you determine what type of variable you would want to create in your Google Tag Manager container, follow the corresponding steps:
To create a new variable for OnetrustActiveGroups
Custom data layer variables are only supported by OneTrust 6.29 and up.
To create a new variable for OptanonConsent
Triggers
In GTM, triggers prompt tags to fire or not fire based on certain criteria. You may already have a variety of different triggers applied to your tags.
As part of the Cookie Consent integration with GTM you are going to create a trigger associated with each cookie category.
When you apply these triggers to your tags, this will prompt your tags to fire or not fire based on the Cookie Category being active or the consent given by the user.
You will need a separate trigger for each of the Cookie Categories that you will be blocking cookies under. For example, you may have a group called 'Performance Cookies', which has a category id of C0002 and contains the cookies set by your Google Analytics tag.
Each trigger needs to be in line with the category ids that are set in your cookie consent application. You can find the cookie category IDs in the cookie consent application under Categorizations.
We want the OneTrust cookie category id triggers that you create or upload to be applied to your existing tags in such a way that the OneTrust trigger is the limiting factor to the tag firing. This can be done in several different ways. Three different ways are outlined below.
-
Creating a firing trigger (recommended)
-
Using firing triggers on existing tags and creating a trigger group
-
Creating exception triggers
To create a firing trigger
Because OneTrust fires on all pages, if your existing trigger for a tag is All pages, you can delete that trigger and replace it with the OneTrust category.
If a tag is blocked and then allowed, it'll fire without the need for page reload because of listening for OneTrustGroupsUpdated. If it had been previously allowed and then blocked, the tag would be blocked on subsequent page loads.
Using firing triggers on existing tags and creating a trigger group
OneTrust recommends using this method when you have an existing firing trigger on the tag that contains a condition that needs to be met before the tag can fire.
When using a firing trigger, make sure the OneTrust trigger is the limiting factor to the tag firing.
Only use a trigger group if you already have a firing trigger set up on your tag.
Follow the steps mentioned above to first create the OneTrust category firing trigger and then to add your existing condition trigger to create a trigger group.
Trigger groups utilize “AND” conditions, whereas directly applying multiple triggers to a tag to fire utilizes an “OR” condition.
Firing Triggers directly applied:
Within Trigger Group:
This is another method for setting up the OneTrust triggers and applying them to your tags such that the OneTrust triggers are the limiting factor to the tags firing.
-
Select the Triggers tab from the main menu. The Triggers screen appears.
-
Press New. The Trigger Configuration screen appears.
-
Create a new trigger and name it accordingly, e.g. Example Trigger Group.
-
Press the Trigger Configuration and set the Trigger type to Trigger Group.
-
Select the existing trigger for your tag.
-
Select the OneTrust trigger based on the category to which the tag should be mapped.
To create an exception trigger
You can also set up an exception trigger to fire the script if a category of cookies is not active.
You will only want to use an exception trigger if you already have a different firing trigger set up on your tag.
In Google Tag Manager, Blocking triggers must fire on the same event as the Active triggers.
For example, set a trigger to fire when OnetrustActiveGroups does not contain C0002 (where C0002 is the id for performance cookies). Apply this blocking trigger as an exception to tags in this group.
Using a container to integrate with Google Tag Manager
This adds the triggers mentioned above automatically to your container.
To import a container
To add the triggers to tags in a container
You can review all your tags in your Google Tag Manager containers to verify which tags should connect to OneTrust triggers.
The general recommendation is to review the categorizations in your OneTrust tenant to determine which cookies are added from Google Tag Manager. You can then add the respective categorization trigger to the tag that fires a cookie.
If there is an existing trigger on a tag that is firing a cookie, follow the steps above to create a trigger group. Add the trigger group with your existing condition and the OneTrust cookie category trigger to the tag firing the cookie.
If you have a tag that fires a cookie with the All Pages trigger added to it, delete the trigger and replace it with the OneTrust cookie category trigger as the Firing trigger.
Before OneTrust is integrated, the triggers appear as follows:
After replacement, the trigger displays as follows:
- On the main navigation menu in Google Tag Manager, select Tags.
- Select a tag to which you want to apply the appropriate OneTrust Cookie Category Trigger.
- Apply the appropriate Firing Trigger, Trigger Group, or Exception Trigger that you have created.
- Click the Save button.
To test your Google Tag Manager configuration
After you have reviewed all your tags, created onetrust category triggers and added the triggers to all the tags firing cookies by following the steps listed above you should be ready to test your website
To test your setup, you will need preview access on Google Tag Manager. If you Preview is not present in your GTM container, please reach out to your GTM admin.
To leverage dataLayer attributes
To disable CDN pushes to window.dataLayer
data-dLayer-ignore attribute into your script.
Example:
<script src="https:/cdn.cookielaw.org/scripttemplates/otSDKStub.js"
type="text/javascript" charset="UTF-8" data-domain-script="XXXXX"
data-dLayer-ignore="true"></script>
To enable CDN pushes to a new dataLayer
data-dLayer-name attribute into your script.
Example:
<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js"
type="text/javascript" charset="UTF-8" data-domain-script="XXXXXX"
data-dLayer-name="dataLayerOneTrust"></script>
This allows CDN pushes to your defined dataLayer only. Pushes to window.dataLayer are disabled.
This is supported for script versions OneTrust 6.28 and onwards. Please publish to a newer script version if you are using an older script version than OneTrust 6.28.
OneTrust cannot send events or updates to multiple datalayer variables
Troubleshooting
Support Document.Write
If you are using Google Tag Manager and have more than one tag or container with the ‘Support Document.Write’ checkbox checked, it may prevent the script from firing properly.
In order to confirm, block the request URL of the container that does not contain the CookiePro/OneTrust tag.
-
Does the tag that loads the CookiePro/OneTrust script fire successfully?
-
If so, try blocking the request URL for the exact tag causing the issue.
-
To resolve this issue, set the tag causing the conflict to fire after the CookiePro/OneTrust script has loaded completely.
This can be accomplished by setting the tag in question to use a firing trigger that is dependent on the OnetrustGroupsUpdated event that pushes within the dataLayer once the script completes. This fix is similar to the method used to ensure cookies are not dropped for users based on consent preferences.
OptanonLoaded Event is Not Triggered
If the cookie banner is deployed on a website via Google Tag Manager (GTM), but the event OptanonLoaded is not being triggered in GTM, then GTM may not be reading the default dataLayer. Instead, GTM is reading a custom layer such as googleDataLayer. Within the GTM container, no events are recorded even after a banner interaction on the website. However, on the website you can see that the events are indeed being pushed to the default data layer.
-
Right-click the web page and select Inspect Source, or use the browser's Developer Tools to open the console.
-
Check the data layer. If the opt-in consent model is being used, only category C0001 should appear under
OptanonLoadedandOneTrustGroupsUpdated. -
Interact with your banner by accepting all cookies.
-
Upon banner interaction, check if the events are being pushed to the data layer.
-
After accepting all cookies,
OptanonLoadedandOneTrustGroupsUpdatedshould show categories C0001, C0002, C0003, C0004, etc in the data layer. This means consent is being pushed to the default data layer.Google Tag Manager needs to read this data layer to reflect the events in the container.
-
On the website's Network tab, search for
datalayer. -
Review which data layer is being read by which GTM container. This will display in the header and payload of the GTM network calls.
-
If the header request URL and the payload show
googleDataLayerunder the GTM container name, this indicates that the GTM container is reading the Google data layer instead of the default data layer. This is why nodataLayerconsent events are being recorded in GTM.