Google Tag Manager
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 set it up so that the scripts are controlled by the consent preferences choices selected by visitors.
This can be more efficient than using the standard helper methods described in the Client-Side Cookie Management section of this guide. However, there are some differences in behavior because of how GTM works.
This section of the guide explains how to set up GTM to take advantage of these changes.
This is not intended to be a complete guide to Google Tag Manager. Consult official Google Tag Manager documentation for more detailed information about setting up and using GTM.
How it Works
To pass data to Google Tag Manager, the Cookie Compliance tool uses Variables. It supports JavaScript Variables and Data Layer Variables. We recommend using the GTM Data Layer and Data Layer Variables.
Cookie Compliance uses the existing dataLayer object or creates a new one if it doesn’t already exist. If your site creates a dataLayer object, ensure that this does not overwrite the one Cookie Compliance creates.
It adds a key named OnetrustActiveGroups with a value of a comma delimited string of the current active category ids as selected by the visitor (or the default setup). This key is re-populated on every page load once the script is executed.
For example, the data contained in the value might be ,C0002,C0003,C0004.
When a user updates their consent a dataLayer event OneTrustGroupsUpdated will be triggered. You will use this event when creating triggers to apply to your tags.
By creating a GTM Custom Variable and Triggers, you can make GTM tags only trigger when specific consent groups within OntrustActiveGroups 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.
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 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:
j.setAttributeNode(d.createAttribute('data-ot-ignore'));
j.setAttribute('class','optanon-category-C0001');
To create a new variable
We suggest naming the variable OnetrustActiveGroups so you know what it refers to.
- Open your container in Google Tag Manager.
- Select the Variables tab from the main menu. The Variable screen appears.
- Create a new User-Defined Variable.
- Name the variable.
- Set the Variable Type to Data Layer Variable under the Page Variables section.
- Set the Data Layer Variable Name to OnetrustActiveGroups.
- Press Save Button
The Data Layer Variable Name must be set to OnetrustActiveGroups for the code to work as expected
Deploy the banner script in a tag
If using a Custom HTML-type tag in Google Tag Manager, the tag content can be either of the following:
- HTML / JavaScript
- Making use of the Optanon helper methods to insert the HTML/JavaScript (i.e. Optanon.InsertScript or Optanon.InsertHtml)
-
Select the Tags tab from the main menu. The Tags screen appears
-
Press New. The Tag modal appears
-
Name the tag Cookie Banner Script
-
Under the Tag Configuration, press the edit button. The Choose tag type modal appears.
-
Select Custom HTML
-
Paste the published script into the HTML editor and check the Support document.write box.
<!-- OneTrust Cookies Consent Notice start --> <script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript" charset="UTF-8" data-domain-script="YOUR DATA DOMAIN SCRIPT ID"></script> <script type="text/javascript"> function OptanonWrapper() { } </script> <!-- OneTrust Cookies Consent Notice end -->
-
Click the Triggering icon. The Choose a trigger modal appears.
-
Select All Pages
-
Click Save.
Triggers
In GTM, triggers prompt tags to fire or not fire on certain criteria. You may already have a variety of different triggers applied to your tags.
As part of the Cookie Compliance 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
- Using Firing Triggers on Existing Tags and Creating a Trigger Group
- Creating Exception Triggers
To create a firing trigger
-
Open your container in Google Tag Manager
-
Select the Triggers tab from the main menu. The Triggers screen appears
-
Press New. The Trigger Configuration screen appears
-
Name it accordingly, e.g. Performance Cookies Active
-
Press the Trigger Configuration and set the Trigger type to Custom Event.
-
Set the Event name to OneTrustGroupsUpdated. This event is embedded in the script
-
Select Some Page Views and set it to fire when the following is true:
-
[OnetrustActiveGroups] [matches RegEx] [,C0002,]
-
-
Save the Trigger.
-
Repeat this process for the remaining Cookie Categories.
-
Apply the Triggers to Tags as a firing trigger.
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.
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 OneTrust Active Groups does not contain C0002 (where C0002 is the id for performance cookies). Apply this blocking trigger as an exception to tags in this group.
-
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. Block Performance Cookies.
-
Press the Trigger Configuration and set the Trigger type to Custom Event.
-
Set the Event name to .*. This event applies to all events and will allow the exception trigger to override the event that is in the firing trigger.
-
Set the Trigger to fire on Some Custom Events.
-
Select Some Page Views and set the Trigger to fire when the following is true:
-
[OnetrustActiveGroups] [does not match RegEx][,C0002,]
-
-
Save the Trigger.
-
Repeat this process for the remaining Cookie Categories.
-
Apply the Triggers to Tags as a firing trigger.
Using firing triggers on existing tags and creating a trigger group
When using a firing trigger, you want to make the OneTrust trigger is the limiting factor to the tag firing.
You will only want to use a trigger group if you already have a firing trigger set up on your tag.
Trigger groups utilize “AND” conditions, where as directly applying multiple triggers to a tag to be firing 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.
Using a container to integrate with Google Tag Manager
To import a container
- Open your container in Google Tag Manager
- Go to the Admin tab
- Download the container from this link.
- Click Import Container.
- Select the downloaded container file.
- Select Merge and then Overwrite conflicting tags, triggers and variables as the import option.
- Click the Confirm button.
To add the triggers to tags
- On the main navigation menu in Google Tag Manager, select Tags.
- Select a tag to apply the appropriate OneTrust Cookie Category Trigger to.
- Apply the appropriate Firing Trigger, Trigger Group or Exception Trigger that you have created
- Click the Save button
Blocking Tags Using the OptanonConsent Cookie
In addition to the OneTrustActiveGroups data layer variable, the OTSDKstub.js file also sets the first party cookie, OptanonConsent, to capture user consent. By decoding the value of the OptanonConsent cookie, the current consent can be read. For more information, see OneTrust Cookies
To create a new variable
- Open your container in Google Tag Manager.
- Select the Variables tab from the main menu. The Variable screen appears
- Create a new User-Defined Variable
- Name the variable
-
We suggest naming the variable OptanonConsent so you know what it refers to.
-
- Set the Variable Type to 1st Party Cookie under the Page Variables section
- Set the Data Layer Variable Name to OptanonConsent
- Press the save button
The Data Layer Variable Name must be set to OptanonConsent for the code to work as expected. Additionally, the URI-decode cookie option must be enabled.
Triggers
Similar to the OnetrustActiveGroups data layer variable setup, active and blocking triggers will be created for each cookie category within the account. As the OptanonConsent cookie is set on page load, this allows you to trigger client tags before OneTrustGroupsUpdated event normally used by the data layer variable to block Cookie Categories.
For example, if you are using an e-commerce event that you have defined in the data layer, you can use the OptanonConsent cookie over the OneTrustActiveGroups data layer variable to trigger your analytics tags on the e-commerce event rather than on OneTrustGroupsUpdated.
To create a firing trigger
- Open your container in Google Tag Manager.
- Select the Triggers tab from the main menu. The Triggers screen appears.
- Press New. The Trigger Configuration screen appears.
- Name it accordingly, e.g. First Party Cookie - Active Performance - OT.
- Press the Trigger Configuration and set the Trigger type to the relevant use case
-
Adding Page View or DOM Ready triggers will likely the cause the triggers to not fire on the first page load.
-
- Set the Event name to the event the on which affected tag will fire. This differs from the Data Layer variable OneTrustActiveGroups that must trigger on the event OneTrustGroupsUpdated
- Set the Event name to the event the on which affected tag will fire. This differs from the Data Layer variable OneTrustActiveGroups that must trigger on the event OneTrustGroupsUpdated.Select the relevant trigger and set it to fire when the following is true:
-
[OptanonConsent][contains][C0002:1]
-
- Save the Trigger.
- Repeat this process for the remaining Cookie Categories.
- Apply the Triggers to Tags as a firing trigger.
To create an exception trigger
- Open your container in Google Tag Manager.
- Select the Triggers tab from the main menu. The Triggers screen appears.
- Press New. The Trigger Configuration screen appears.
- Name it accordingly, e.g. First Party Cookie - Block Performance - OT.
- Press the Trigger Configuration and set the Trigger type to the relevant use case
-
Adding Page View or DOM Ready triggers will likely the cause the triggers to not fire on the first page load.
-
- Set the Event name to the event the on which affected tag will fire. This differs from the Data Layer variable OneTrustActiveGroups that must trigger on the event OneTrustGroupsUpdated.
- Select the relevant trigger and set it to fire when the following is true:
-
[OptanonConsent][does not contain][C0002:1]
-
- Save the Trigger.
- Repeat this process for the remaining Cookie Categories.
- Apply the Triggers to Tags as a firing trigger.
You will need to use >=6.29 script version for the following
To leverage dataLayer attributes
Using the following dataLayer attributes, you can prevent the OneTrust CDN from pushing to window.dataLayer or create a new dataLayer so that you have control over how messages are sent to GTM.
To disable CDN pushed to window.dataLayer
Insert the 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
Insert the 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>
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.
Updated 10 months ago