Skip to content
FunnelGuard

Fix guide by FunnelsGuard

GA4 ecommerce object not cleared in dataLayer: how to fix it

TRK-091Medium impactCould addBy FunnelGuardPublished Updated
On this page

What this issue is

The dataLayer is a list of messages your site sends to Google Tag Manager (GTM). GTM merges every message into one running picture of the page, called the data model. When you push an ecommerce event such as view_item_list or add_to_cart, the ecommerce object stays in that model until something replaces it.

Why it costs you leads or sales

How old items leak into the next event
  1. view_item_listPushes 12 items
  2. add_to_cartPushes 1 item, old object not cleared
  3. GA4 receives eventItems or fields from the list are merged in
  4. ReportsWrong products and counts

The ecommerce object from the product list is never cleared, so it is merged into the add_to_cart event.

How to check if your site has it

Check for a missing clear in about 10 minutes
  1. Open GTM Preview modeGoogle Tag Manager

    In Tag Manager click Preview, enter your site URL and connect Tag Assistant to the site.

  2. Walk through two ecommerce events in a row

    Open a category page, then a product page, then add the product to the cart. Each step should send a different ecommerce event.

  3. Inspect the dataLayer for each event

    In Tag Assistant, select each ecommerce event and open the Data Layer tab. Look for a push of { ecommerce: null } just before each ecommerce push. If it is missing, you have the issue.

  4. Compare the payload GA4 receives

    Open the GA4 tag fired by the later event and check its fields. If it lists items from the earlier event, or a coupon or value that was not in the new push, the old object leaked.

  5. Test in the browser console

    On the page, run google_tag_manager['GTM-XXXX'].dataLayer.get('ecommerce') after the later event. Replace GTM-XXXX with your container ID. If it returns old fields, the object was not reset.

How to fix it

Clear, then push the new eventjavascript
window.dataLayer = window.dataLayer || [];

// 1. Clear the previous ecommerce object
dataLayer.push({ ecommerce: null });

// 2. Push the new event
dataLayer.push({
  event: 'add_to_cart',
  ecommerce: {
    currency: 'USD',
    value: 29.99,
    items: [{
      item_id: 'SKU_123',
      item_name: 'Blue T-shirt',
      price: 29.99,
      quantity: 1
    }]
  }
});
Where to add the clear, by platform
  1. Find every ecommerce push

    Search your code for dataLayer.push calls that contain an ecommerce key.

  2. Add the clear before each one

    Put dataLayer.push({ ecommerce: null }); directly above each push, or wrap both in one helper function and call that everywhere.

How to confirm it's fixed

Confirm the fix
  1. Repeat the walkthrough in GTM PreviewGoogle Tag Manager

    Go through the list, product and cart events again. Each ecommerce push should now be preceded by an { ecommerce: null } push.

  2. Check the GA4 tag payload

    Each event should list only its own items, with no leftover coupon, value or items from the previous event.

  3. Check GA4 DebugView

    In GA4 go to Admin > DebugView and confirm each event shows the right items and parameters.

  4. Compare reports after a few days

    Check Reports > Monetization > Ecommerce purchases. Item counts per event should look realistic compared with your orders.

add_to_cart before and after the fix

Not cleared

  • add_to_cart carries items from the earlier list
  • Item counts and values look inflated
  • Product reports show products nobody added

Cleared first

  • add_to_cart contains only the added product
  • Each event has its own items and value
  • Product and funnel reports match real behaviour

How to prevent it happening again

Prevention checklist
  • Use one shared function that clears and pushes every ecommerce event
  • Add the clear step to your tracking spec so new events include it
  • Re-test in GTM Preview after theme, plugin or checkout changes
  • Test two ecommerce events in a row, not only one in isolation
  • Review GA4 DebugView after any release that touches tracking

How FunnelGuard handles this

Questions people ask

Why are items from a previous event showing in my GA4 add_to_cart?

GTM merges each new push into its data model, so an ecommerce object that was not cleared can carry old items or fields into the next event. Push dataLayer.push({ ecommerce: null }) immediately before every ecommerce push to stop it.

How do I clear the ecommerce object in the dataLayer?

Run dataLayer.push({ ecommerce: null }); right before each ecommerce event push. This resets the object so the new event starts clean. It should sit in the code that creates the event, not in a GTM trigger.

Do I need to clear ecommerce before every GA4 event?

Yes, before every push that contains an ecommerce object. Google's GA4 ecommerce documentation recommends this because the object otherwise persists in the GTM data model. Events without an ecommerce key do not need it.

Can I fix a dataLayer leak inside Google Tag Manager only?

Not reliably. GTM reads the dataLayer but cannot reset it after the fact. The clear push has to be added in the code or plugin that creates the events. A Custom HTML tag that you control is the one exception, because you can edit its code.

How can I tell if my ecommerce object is leaking?

Open GTM Preview, trigger two ecommerce events in a row and inspect the Data Layer tab for each. If there is no { ecommerce: null } push before the second event, or the GA4 tag shows old items, the object is leaking.

Does a leaking ecommerce object affect purchase revenue?

It can. Purchase value may still be correct, but extra items, a leftover coupon or wrong item data can distort product reports and item revenue. Check the purchase event payload against a real test order.

Can FunnelGuard detect an ecommerce object that is not cleared?

No. FunnelGuard does not check for this yet. It captures the dataLayer but has no clear check, so use GTM Preview and GA4 DebugView for now.

Does FunnelGuard catch this?

Could add

Not checked yet: could be added to FunnelGuard.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.