dataLayer variables bleeding old product data forward

Google Tag Manager's persistent data layer uses recursive merging. How un-cleared arrays bleed old product data into checkout, and how to fix it.

A customer adds a £200 power tool to their basket on your eCommerce store. Three minutes later, they remove the tool, add a £40 battery pack instead, and proceed through checkout. Your bank account receives £40.00. Your Google Analytics 4 monetisation report records a completed order for £240.00, crediting your campaigns with revenue for both the tool and the battery.

The data layer is not a blank slate that automatically clears between events. When an application pushes information into Google Tag Manager via window.dataLayer.push(), GTM processes the payload and merges it into an internal, persistent data model.

Without an explicit reset between steps, properties pushed during earlier interactions persist in memory. In eCommerce and B2B quotation flows, this recursive merge behaviour causes previous product arrays, outdated currency codes, or stale enquiry tiers to bleed directly into subsequent conversion tags.

Understanding how GTM merges nested objects, why array elements are overwritten by index rather than appended, and how to enforce clean data layer resets protects your trading reports from cumulative distortion.

How the GTM recursive merge operates

Google Tag Manager does not simply read the latest object pushed to window.dataLayer. It maintains an ongoing internal state. When a new event is pushed, GTM updates its data model using specific merge rules:

  1. Primitive values (strings, numbers, booleans): If a new event contains a primitive property with the same key, the new value overwrites the old one (e.g. currency: 'GBP' overwrites currency: 'USD').
  2. Nested objects: GTM recursively merges the new object into the existing one. Properties in the old object that are not mentioned in the new push remain active in memory.
  3. Arrays (such as ecommerce.items): GTM updates array elements by index. If the previous push contained an array of three items ([Item A, Item B, Item C]), and the new push contains only one item ([Item X]), GTM overwrites index 0 with Item X, while leaving index 1 (Item B) and index 2 (Item C) untouched in memory unless explicitly wiped.
User Interaction StepFront-End Data Layer PushGTM Internal Data Model StatePayload Sent to Analytics Tag
1. View Product A (£180)items: [{ name: 'A', price: 180 }]ecommerce.items: [A (£180)]Correct: Item A (£180)
2. Add Product B (£45)items: [{ name: 'B', price: 45 }]ecommerce.items: [B (£45)]Correct: Item B (£45)
3. Add Product C (£25)items: [{ name: 'B', price: 45 }, { name: 'C', price: 25 }]ecommerce.items: [B (£45), C (£25)]Correct: 2 items (£70 total)
4. Remove Product B, Buy C (£25)items: [{ name: 'C', price: 25 }] (no null clear)ecommerce.items: [C (£25), C (£25)] (index 1 preserved)Distorted: 2 items (£50 recorded)
Combined Impact across CheckoutUn-cleared arrays overwrite by indexStale items persist in memoryRevenue and SKU counts disconnect from bank

The table above demonstrates how an array with fewer elements fails to remove surplus items from GTM's internal state, leading to duplicate items or inflated basket values.

The explicit null-clear command

To guarantee that an event payload contains only the data intended for that specific interaction, the application must explicitly command Google Tag Manager to wipe the object from memory before pushing new data.

For Google Analytics 4 eCommerce tracking, this requires a two-step sequence:

```javascript // Step 1: Wipe the persistent eCommerce object from GTM memory window.dataLayer.push({ ecommerce: null });

// Step 2: Push the fresh, verified event payload window.dataLayer.push({ event: 'purchase', ecommerce: { transaction_id: 'ORD-98421', value: 25.00, currency: 'GBP', items: [ { item_id: 'SKU-003', item_name: 'Product C', price: 25.00, quantity: 1 } ] } }); ```

By pushing { ecommerce: null }, GTM completely empties its internal ecommerce object. When the subsequent purchase event arrives, GTM constructs a fresh data model containing only Product C, ensuring zero historical items bleed into the conversion tag.

Diagnostic symptomUnderlying technical causeRequired engineering fix
Purchase revenue exceeds invoice totalsStale items persisted in GTM internal arrayPush { ecommerce: null } immediately before purchase
Multiple product views on single item pagePrevious item array merged into new viewEnforce null-clear on every product page load
Wrong currency code sent on checkoutStale currency parameter inherited from earlier pushExplicitly declare currency on every ecommerce event
Malformed items array in GA4 DebugViewArray overwrite left partial orphaned propertiesValidate complete item schemas on every push

Defensive architecture for Single-Page Applications

On classic websites with full page reloads, browser memory clears naturally when navigating between pages. On modern Single-Page Applications, mobile cart drawers, and AJAX checkouts, the window never reloads.

To protect your reporting:

  1. Enforce null-clears on every virtual route change: Ensure that your front-end framework dispatches { ecommerce: null } whenever the user navigates between catalogue, cart, and checkout views.
  2. Never clear the whole array: Do not execute window.dataLayer = []. That wipes GTM's internal event listener methods and breaks tracking completely. Only nullify specific objects like ecommerce.
  3. Audit variables in GTM Preview mode: Use Tag Assistant to inspect the 'Data Layer' tab on the exact event firing your tag. Check that the values match your expected payload, not a hybrid mixture of earlier interactions.

Learn how to audit your Google Tag Manager container, review our guide on eCommerce tracking reconciliation, or explore our fixed-scope packages to resolve tracking data discrepancies.

Frequently asked questions

Does Google Tag Manager 'Version 2' variable selection solve this?

Version 2 variables allow GTM to access nested object properties using dot notation (e.g. ecommerce.items), whereas Version 1 variables only read top-level keys. However, Version 2 variables still read from GTM's merged internal data model. They do not prevent stale array elements from persisting unless you explicitly push null.

What happens if we do not push { ecommerce: null } on Shopify?

If your store uses Shopify Checkout Extensibility, checkout events run in an isolated Web Pixels sandbox that manages its own clean payloads. However, on your main storefront theme (collection pages, product pages, and slide-out cart drawers), failing to clear the data layer will cause product view and add-to-cart events to merge.

Can we implement the null-clear inside a GTM tag instead of site code?

Attempting to push { ecommerce: null } from inside a GTM Custom HTML tag introduces dangerous race conditions. If the null-clear tag fires after your analytics tag has already read the data layer, the fix will fail. The null clear must always precede the event push in the application source code.