A GTM tag template to set the standard GTM consent types using CookieConsent user preferences.
This template is designed to make it very easy to honour user consent preferences set via CookieConsent by Orest Bida in a Google Tag Manager container.
Note
Before using this template, you need to have CookieConsent up and running on your website first, with all of the consent categories already configured - this template doesn't load any of the JS or CSS for the consent manager!
Either:
- Click the <> Code button above, select Download ZIP, or
- Go to the latest release in Releases on the right, then download the latest Source code (zip)
Extract the ZIP file, then find the GTM Consent for CookieConsent.tpl file within.
- Go to your GTM container
- Navigate to the Templates menu, then click the New button in the Tag Templates box
- Click the ... menu at the top-right, then select Import
- Choose the
GTM Consent for CookieConsent.tplfile from before, then wait for it to import - Click the Save button at the top-right, then click X at the top-left to return to the main GTM interface
If you haven't already done so, you need to configure your GTM container for consent mode:
- Navigate to the Admin tab
- Select Container Settings on the right
- Ensure Enable consent overview is checked
- Click the Save button at the top-right if you needed to check this box
The default Consent Initialisation trigger already exists, but you also need to be able to react to consent events:
- Navigate to the Workspace tab
- Select the Triggers menu, then click the New button
- Give the trigger a name - e.g.,
Consent Events - Click into Trigger Configuration, then select Custom Event from the Other section
- If you're using my Wordpress plugin with the default configuration:
- Enter
onFirstConsent|onChangeinto the Event name box, then check the Use regex matching checkbox.
- Enter
- If you're not using my plugin, or if you've customised things:
- Please check your CookieConsent configuration and ensure you're triggering dataLayer events for the onFirstConsent and onChange CookieConsent events - see below for what needs to be included in the configuration.
- If you've changed the name of either of the dataLayer events, you will need to update the event name regex in the trigger configuration accordingly.
- Click the Save button at the top-right
Reference: the following needs to appear in the CookieConsent configuration to trigger the dataLayer events mentioned above:
// Trigger GTM dataLayer events when updating consent
// These are optional, but required if using the "GTM Consent for CookieConsent" GTM template
onFirstConsent: function(detail) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'onFirstConsent',
consentCategories: detail.cookie.categories,
consentServices: detail.cookie.services
});
},
onChange: function(detail) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'onChange',
consentCategories: detail.cookie.categories,
consentServices: detail.cookie.services
});
}When adding the tag, you will need to refer to your CookieConsent configuration for the tag settings:
- Select the Tags menu, then click the New button
- Give the tag a name - e.g.,
CookieConsent - Click into Tag Configuration, then select GTM Consent for CookieConsent from the Custom section
- Review the tag configuration and make sure everything matches your CookieConsent configuration - at the very least, you need to update the Consent Type Mappings section
Each GTM category can only be mapped to one CookieConsent category, however you can have one CookieConsent category mapped to multiple GTM categories - as shown below.
You likely want to have mappings similar to the following (but check your CookieConsent configuration to be sure!):
| Google Tag Manager | CookieConsent |
|---|---|
| Ad Storage | marketing |
| Ad User Data | marketing |
| Ad Personalization | marketing |
| Analytics Storage | analytics |
| Strictly Necessary | necessary |
Warning
If there are mismatches between the tag configuration and CookieConsent configuration, weird behaviour is almost guaranteed..
- Click into Triggering, then select BOTH
Consent EventsandConsent Initialisation - All Pages - Click the Add button at the top-right
- Click the Save button at the top-right
If you have consent logging requirements, or tags that need to respond to consent changes on the page:
- Navigate to the Triggers menu, click the New button
- Give the trigger a name - e.g.,
Consent Update - Click into Trigger Configuration, then select Custom Event from the Other section
- Enter
consent_updateinto the Event name box - Click the Save button at the top-right
If you have tags that need to behave differently on page load for default consent vs. consent updates (typically needed if the default consent is granted, but you only want to fire a tag if the user actually saves that preference as well) - you'll need to create a Custom JavaScript variable and trigger. Sadly, this doesn't work in a template, as it reads the google_tag_data global object.
- Navigate to the Variables menu
- In the User-Defined Variables section, click the New button
- Give the variable a name - e.g.,
Consent Type - Click into Variable Configuration, then select Custom JavaScript
- Paste the code below, then click the Save button at the top-right
function() {
var tagData = window.google_tag_data;
if (!tagData.ics) return;
if (tagData.ics.usedUpdate) return 'explicit';
if (tagData.ics.usedDefault) return 'implicit';
}- Navigate to the Triggers menu, click the New button
- Give the trigger a name - e.g.,
All Pages - Explicit Consent - Click into Trigger Configuration, then select Page View from the Page View section
- Choose Some Page Views
- In the first drop down, choose Consent Type
- In the second drop down, choose equals
- In the text box, enter
explicit - Click the Save button at the top-right
Before publishing, test your configuration carefully to make sure everything works:
- Check each of your tags for Advanced Settings > Consent Settings, to make sure that consent is configured appropriately
- If you created the Consent Update trigger above, consider which tags need this added to their Triggering section, if any
- If you created the All Pages - Explicit Consent trigger above, consider which tags need this added to their Triggering section, if any
- Use GTM's Preview Mode to ensure that tags are being blocked / updated according to the user consent preferences and tag settings
- Test as many different consent flows as possible: no consent set, consent choices set, consent choices updated
When Preview Mode is active, there are a number of comments in the log that you will see - here's what they mean:
| Comment | Explanation |
|---|---|
| Event triggering consent evaluation: ( event ) | The event name from whatever triggered the CookieConsent tag, commonly:
|
| Valid consent object found, contents: { object } | A valid consent object has been found in either the consent cookie or localStorage - the contents of the object are shown in the log. |
| Invalid consent object found, contents: { object } | A consent object was found in either the consent cookie or localStorage, but it wasn't valid. Default consent will be applied instead. This is very unlikely.. |
| Consent object cannot be parsed! We received: { whatever we can parse } | A consent object was found, but it couldn't be JSON.parse()'d. Default consent will be applied instead. This is very, very unlikely..! |
| Setting default consent using ( CookieConsent | tag ) configuration, mode is ( opt-in | opt-out ): { object } | Default consent has been applied from either the CookieConsent configuration if available, or from the tag configuration. The mode is either opt-in (GDPR), or opt-out (CPRA). The object at the end is the consent state. |
| Setting updated consent using data from the consent object: { object } | Updated consent has been applied from the consent object. The object at the end is the consent state. |
| First consent decision - triggering consent_update event! | The onFirstConsent event was processed, and the consent_update event has been triggered. |
| Consent changed (n changes) - triggering consent_update event! | The onChange event was processed with more than 0 changes, and the consent_update event has been triggered. |