Don't have one? Create one ↗
Tagging in the wild — when there's no clean dataLayer

Lesson 6 of 9

Stitching Values Across Events & The Timing Traps

The wild is full of timing problems. The merged model is forgiving, but it is not magic: a value can be missing because it does not exist yet, or wrong because it was overwritten, or lost because the page navigated away before your tag could send. These are the traps that make a tag "work in Preview but not in reports."

The value isn't there yet

If a tag reads plan on gtm.js but the page pushes plan a beat later, you read undefined. The fix is to fire on the event that comes after the value exists. Order is everything: trigger on the push that proves the data is ready, not on page load out of habit.

The value got overwritten

Merging cuts both ways. If two steps both push plan, the later one wins, and by submit time you may be reading the wrong one. When a value is volatile, capture it the moment it is correct. A Custom JS variable can latch the latest good value:

function () {
  // Remember the last non-empty plan we saw, across events.
  window.__lastPlan = window.__lastPlan || undefined;
  var current = {{DLV - plan}};
  if (current) window.__lastPlan = current;   // update only when present
  return window.__lastPlan;                    // return the remembered value
}

The navigation gotcha

The classic. A real form submit or outbound click navigates the page away. The browser may tear down the page before your tag's network request finishes, so the event vanishes. GA4 dodges this by sending with navigator.sendBeacon, which the browser delivers even as the page unloads. Older or hand-rolled tags that use a normal request can lose the hit.

Symptoms

  • Fires perfectly in Preview, missing in reports
  • Outbound clicks and real submits under-count
  • Worse on fast connections (page leaves sooner)

Defences

  • Prefer events sent via beacon transport
  • Trigger on a reliable pre-navigation signal
  • If needed, briefly delay nav until the tag fires

Key takeaway

Most "works in Preview, missing in reports" bugs are timing: the value isn't ready, it was overwritten, or the page navigated before the tag sent. Fire after the data exists, latch volatile values, and make sure submit/outbound hits use a transport that survives unload.