Integrating cookie management with tag management platforms
A few people have spoken to me recently about issues with race conditions when trying to integrate their cookie management (CMP) and tag management (TMS) platforms.
For example - you want to trigger GA4, but it's only allowed to fire once the user has consented to "performance" tags.
So you do the obvious thing - you set GA4 to fire on page load in your TMS, and you reference data from your CMP to limit when it can fire so it only triggers if the user has consented to "performance" tags.
Result - GA4 doesn't fire even after consent is granted, OR WORSE (if you're relying blocking triggers in GTM), GA4 fires before the user has granted consent / after they have declined.
There's a solid chance that the CMP data doesn't exist yet.
What's going on? - assuming the rest of the implementation is sound, it's likely you're seeing a race condition. The CMP script has to make a network request, retrieve the CMP JavaScript library, and then this JS has to be processed by the browser.
Often - by the time that's happened - the page load event in your TMS (which is completely unrestricted by anything the CMP is doing) has already fired.
Put simply: if you're firing a tag using a standard TMS page load trigger, and you're referencing data from a CMP to conditionally decide whether the tag should run, there's a solid chance that the CMP data doesn't exist yet. Depending how you've set the TMS up, this will either mean the tag doesn't fire when it should, or the tag fires when should not. I've seen both.
How to fix - use events from your CMP to trigger your tags
Most CMPs will / can be made to return events and associated data to tell you what consents the user has given.
Get your CMP to push data onto the dataLayer array.
The CMP should load on every page, meaning consent-level events will fire on every page. If you replace all your page-load level triggers with CMP page-load level events instead, you can guarantee that user consent data will be available when you run tags that rely on this data to ensure compliance with users' consent choices.
Here's an example of how you might leverage that:
First - ensure that there's an array called dataLayer on your site. If you're using GTM - happy days, it'll do it for you.
If you're using another TMS like Adobe Launch; as early as you can in the page load order (e.g. Library Loaded (Page Top) in Adobe Launch), run something like this to create it:
window.dataLayer = window.dataLayer || [];
Then you just get your CMP to push data onto the dataLayer array. Here're a few CMP examples:
领英推荐
OneTrust
OneTrust is super easy, and is the CMP we use at Loop Horizon. When you publish your cookie banner, select the "Google Analytics Tracking for the Banner and Preference Center" option:
And when OneTrust loads, you'll get data like this pushed onto the dataLayer array:
TrustArc
There's a GitHub repository that tackles getting TrustArc to integrate with the dataLayer.
NOTE: As with any external script, it'll need approval before it can be used
Once implemented, it will return a separate event to the dataLayer for each consent choice. If you're integrating with GTM, this is actually a much easier approach than the OneTrust method, as you can simply trigger off the specific event for the consent choice relating to the tag you want to run without any additional variables / conditions needed:
Evidon
In examples we've seen using Evidon, it looks to push an "evidonConsentGiven" event object onto the dataLayer without any specific configuration required. This contains a "consentCategories" object that list out the consent categories that the user has accepted in human-readable format:
Summary
TMSs have the ability to easily integrate with dataLayer.push events such as the ones emitted by the CMPs. In GTM, it's "Custom Events", in Adobe Launch there're multiple agnostic and vendor specific data layer integration extensions.
Leverage this functionality to lean into your CMP, defer your page-load level tags to the events emitted by your CMP, and ensure you never suffer CMP-related race conditions again.
Great post! Onetrust also has a JS function you can use, OptanonWrapper- https://my.onetrust.com/s/article/UUID-29158b4e-22f6-0067-aa36-94f3b8cf3561?language=en_US
Beautiful writing Matt Bentley, clear and simple enough for me to understand ??