A visitor clicks Accept on your OneTrust banner. Half a second later your tags in Google Tag Manager are supposed to know about it. Between those two moments there's a single string of text, and once you can read it, most OneTrust problems stop being mysterious.
The string that holds every answer
Open any site running OneTrust, press F12 and type this into the Console:
OnetrustActiveGroups
You'll get something like ,C0001,C0003,. That's the list of cookie categories the visitor has allowed, with commas at both ends. On a default OneTrust setup the IDs mean:
| ID | Category |
|---|---|
C0001 | Strictly necessary |
C0002 | Performance (analytics) |
C0003 | Functional |
C0004 | Targeting (advertising) |
C0005 | Social media |
Don't trust that table for your own site, though. Category IDs can be renamed or added in the OneTrust admin, so check yours under Cookie Categories before building anything on them. On an older account, analytics might sit under C0003 just because someone set it up that way years ago.
C0001 is always in the list. Strictly necessary cookies can't be refused, so a visitor who rejects everything still gives you ,C0001,.
The two events OneTrust sends to the dataLayer
OneTrust doesn't just set that variable. It also pushes it into the dataLayer, attached to one of two events:
OneTrustLoadedwhen the banner script has finished loading and knows the stored choiceOneTrustGroupsUpdatedwhenever the visitor saves a new choice
Each push carries OnetrustActiveGroups with it. You can watch both happen in GTM Preview mode: they show up in the event list on the left, and the Variables tab shows the groups at that moment.
Building the trigger
In GTM, create a Data Layer Variable called OnetrustActiveGroups. Then make a Custom Event trigger for analytics:
- Event name:
OneTrustLoaded|OneTrustGroupsUpdated, with regex matching on - Fires on:
OnetrustActiveGroupscontains,C0002,
Keep the commas in the condition. Without them, C0002 would also match a category called C00021 if anyone ever creates one, and that's a quiet bug you'd find months later.
Why I don't use the Page View trigger here
The obvious move is to keep your GA4 tag on All Pages and add a condition on OnetrustActiveGroups. It often fails, and the reason is timing. GTM's page view usually happens before OneTrust has loaded, so at that moment the variable is empty. The condition is false, the tag doesn't fire, and you lose the page view of every returning visitor who had already said yes.
Firing on OneTrustLoaded waits for the answer. Firing on OneTrustGroupsUpdated catches the visitor who accepts on their very first page.
Check that it fires once
There's a catch with listening to both events. On some setups OneTrust sends OneTrustGroupsUpdated on page load too, right after OneTrustLoaded. If both match, your page view goes out twice and your sessions look busier than they are.
Open Preview, reload a page as a visitor who has already accepted, and count how many times the tag fires. If it's two, add a trigger exception, or switch to a single event and handle the first-accept case separately.
This variable-and-trigger approach is the older way of doing things. Most sites now also pass consent to Google through Consent Mode, which works differently. Next: how to map OneTrust's categories onto it.
