How to sequence datalayer.push?

416 Views Asked by At

I have a question regarding GA4 Enhanced Ecommerce Tracking with GTM - although the problem isn't specific to GA4 or Ecommerce Tracking.

The Situation

  • the shop does use usercentrics as cookie management solution
  • all gtm tags work only, when the user accepts necessary cookies: after the user interacts with the cookie notice, usercentrics does a datalayer.push event called "consent_status" and depending on the value gtm tags shoo, or not
  • on the checkout there is a datalayer.push event called "begin_checkout"

Problem In GTM Debug Mode i can see, that begin_checkout shoots before consent_status and therefore gtm does not process the datalayer value of begin_checkout.

Question Is there a possibility to sequence begin_checkout to shoot after consent_status? What does the sequence of datalayers depend on?

Further thoughts

  • Basic tracking functionality (e.g. pageview) works without problems
  • datalayer.push like "add_to_cart" works also just fine, but theres a differnt approach to the checkout: when adding an item to the card, the add_to_card datalayer becomes visible in page source code, but only after the user interacts with the website and clicks the button with text "add to cart" - before that there is no datalayer.push called "add_to_card. Therefore of course the consent_status event is already processed and the sequencing of datalayers is correct
  • on the checkout page instead the datalayer.push event shoots directly and without further user interaction. here the sequencing of datalayer.push is important, but I can't understand how to prioritize the usercentrics event "consent_status" before the ecommerce event "begin_checkout"

What i tried

  • I tried removing async, dns-prefetch and preconnect tags regarding usercentrics and gtm
  • I tried playing around with the order of scripts in without results
1

There are 1 best solutions below

0
BNazaruk On

Looks like your approach is not quite the optimal one. Playing with event sequencing is quite a great thing to avoid.

The good solution here would be finding out where the consent information is stored (either cookies or local storage in most cases) and then just read directly from there instead of waiting for a third party callback to do it for you.

Moreover, this is not a problem just for your begin_checkout. It's likely happening in other cases too, where you're not seeing it due to either you not paying attention or due to not so obvious race condition in place. So you might want to revisit how you block your other rules from firing.

The only place where you want to wait for it is on pageviews. And only when the consent hasn't yet been given. So you just set two triggers on your pageviews: one normal pageview trigger with the cookie blocker/exception, and one trigger listening for the consent management system callback. This way people who gave consent would be tracked with no delays and people who haven't yet given consent won't be tracked until they give it.