Skip to content
FunnelGuard

Fix guide by FunnelsGuard

dataLayer = [] reassigned mid-page wiping GTM events: fix

TRK-129High impactPartlyBy FunnelGuardPublished Updated
On this page

What this issue is

The dataLayer is a JavaScript array on your page. Your site pushes messages into it (for example a purchase or a form submit) and Google Tag Manager (GTM) reads them to decide which tags to fire.

When the GTM snippet loads, it wraps the dataLayer array and watches its push method. If another script later runs dataLayer = [] or window.dataLayer = [], the page variable now points to a brand new array. GTM keeps reading the old one, so anything pushed to the new array is never seen. Values already in the old array can also disappear from what your tags use.

The bug: reassigning the arrayjavascript
// Runs AFTER the GTM snippet
window.dataLayer = [];
window.dataLayer.push({ event: 'form_submit' }); // GTM never sees this

The usual sources are a theme or plugin, a copied tracking snippet, a cookie banner script, a single page app that resets state on route change, or a developer who wanted to "clear" the layer.

Why it costs you leads or sales

Your ad platforms learn from the conversions your tags report. If purchase or lead events go to a dead array, GA4, Google Ads and Meta see fewer conversions than really happened. Bidding then optimises on weak data, reports undercount, and you may cut campaigns that were working.

The failure is also quiet. The page looks fine, the form still submits and the customer still pays. Only the tracking breaks, and it can be intermittent because it depends on which script runs first.

Where the events get lost
  1. GTM loadsWatches the original dataLayer
  2. Script runs dataLayer = []Page now holds a new array
  3. Site pushes purchase eventGoes to the new array
  4. Tags fireGA4, Ads, Meta never triggered

The reset cuts GTM off from later pushes.

A push that GTM never sees

The event is pushed, but the tag stays silent.

How to check if your site has it

Find a dataLayer reset
  1. Search your code

    Search theme files, plugins, header and footer scripts and tag templates for dataLayer = and dataLayer=. Anything that is not window.dataLayer = window.dataLayer || []; is a suspect.

  2. Use the browser console

    Open your page, then DevTools, then Console. Type google_tag_manager and check your container ID exists. Then type dataLayer and look at the entries. If your early events (such as gtm.js) are missing, the array was replaced.

  3. Run a quick test

    Before reloading, run window.__dl = window.dataLayer; after the page loads. Trigger an action, then run window.__dl === window.dataLayer. If it returns false, something replaced the array.

  4. Use GTM Preview

    Open Tag Assistant from GTM, connect to your page and walk through the journey. Compare the Data Layer tab with what you expected. Missing events or a Summary without gtm.js and gtm.dom hints at a reset.

Console check for a replaced arrayjavascript
// Paste in the console after the page has loaded
window.__dl = window.dataLayer;
// ...click or submit something, then:
console.log(window.__dl === window.dataLayer ? 'Same array' : 'Array was replaced');

How to fix it

The rule is simple: create the array once, then only push to it. Never assign a new array after the page starts.

Safe declaration and pushjavascript
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ event: 'form_submit', form_id: 'contact' });
Fix the reset
  1. Locate the line

    Use the search above to find every dataLayer = [] or window.dataLayer = [] and note which file or tool adds it.

  2. Replace it

    Change it to window.dataLayer = window.dataLayer || [];. If it was meant to clear data, remove it, or reset specific keys by pushing them as null or undefined.

  3. Declare before GTM

    If you set up values before the GTM snippet, put the safe declaration above the snippet, and never below it.

  4. Check third party code

    If a plugin or app adds the reset, update it, change its setting, or contact its support. Do not edit files that updates will overwrite without a child theme or a custom code option.

Where to look on your platform
  1. Search theme and plugins

    Search wp-content/themes and wp-content/plugins for dataLayer =. Also check header and footer injection plugins and Appearance, Theme File Editor.

  2. Edit safely

    Change the line in a child theme or the plugin setting, not in a file that updates overwrite.

Declaration, before and after

Broken

  • dataLayer = [] runs after GTM loads
  • New pushes go to an array GTM ignores
  • Conversion tags stay silent

Fixed

  • window.dataLayer = window.dataLayer || [];
  • All pushes reach the array GTM watches
  • Tags fire on the right events

How to confirm it's fixed

Confirmation checks
  • Search shows no dataLayer = [] outside the safe declaration
  • The console test reports the same array after a page action
  • GTM Preview shows gtm.js, gtm.dom and gtm.load plus your custom events
  • Your conversion tags show as fired in Tag Assistant
  • Hits appear in GA4 DebugView or your ad platform's test tool
  • A real test lead or test order shows in reports
Toggle the dataLayer resetInteractive
dataLayer = [] runs mid-page

Reset present

Leads or data lost

Pushes go to a new array. GTM never sees them and conversion tags do not fire.

Switch the cause on to see how one line can silence tracking.

How to prevent it happening again

Prevention habits
  • Give developers and agencies the rule: declare with window.dataLayer = window.dataLayer || [];, then only push
  • Review any new plugin, app or pasted snippet for dataLayer = before it goes live
  • Test events in GTM Preview after every theme, plugin or consent tool change
  • Keep a short list of the events your funnel must send
  • Avoid copying old tracking code from other sites without reading it

How FunnelGuard handles this

FunnelGuard detects this issue only partly. It does not read your source code or look for a dataLayer = [] line. What it does is run your real journeys, such as a lead form or checkout, on a schedule and check the tracking hits sent to GA4, Google Ads, Meta, TikTok, LinkedIn, Bing, Pinterest, Snapchat, X and Reddit. If a reset wipes the events and a conversion hit does not arrive, the step fails, it is re-checked from a second region, and you get one alert with a screenshot and a likely cause.

What it does not do: it cannot tell you that a reset is the cause with certainty, it will not find resets that only affect events outside your monitored journeys, and it will not flag a reset that happens to cause no missing hit. To cover the rest by hand, search your code for dataLayer = as shown above, run the console check and review GTM Preview after changes. Plans start with a Free option (1 site, 2 journeys, daily checks, no card), and paid plans have a 3-day free trial.

Questions people ask

Why is my dataLayer.push not working in Google Tag Manager?

A common cause is that another script ran dataLayer = [] after the GTM snippet loaded. That replaces the array GTM watches, so later pushes go to an array GTM never reads. Search your code for the assignment and replace it with window.dataLayer = window.dataLayer || [];.

Is it OK to write dataLayer = [] before the GTM snippet?

It is safe only if nothing else has created the array earlier and no other script resets it later. The safer habit is to always use window.dataLayer = window.dataLayer || [];, which creates the array only if it does not exist.

How do I check if my dataLayer was overwritten?

Open the browser console and save window.__dl = window.dataLayer; after the page loads. After some activity, compare it with window.dataLayer using ===. If the result is false, the array was replaced. Missing gtm.js entries in the console are another sign.

How do I clear dataLayer values without resetting it?

Push the key with a null value, for example dataLayer.push({ ecommerce: null });. GTM merges pushes into its data model, so this clears the old value and keeps the same array.

Why do my GTM tags fire in Preview but conversions are missing in reports?

There are several possible causes, including consent settings, filters and wrong IDs. If events only go missing on some pages or after certain scripts run, check whether a script reassigns the dataLayer on those pages.

Can a single page app cause the dataLayer to reset?

Yes. Some single page app code or route change handlers set dataLayer = [] on every navigation. Replace that with pushes, and clear specific keys with null instead.

Can FunnelGuard detect a dataLayer reset?

Only partly. It does not scan your code, but it runs your journeys and checks whether the expected tracking hits arrive. A missing hit on a monitored journey raises an alert with a likely cause, and you then confirm the reset by hand.

Does FunnelGuard catch this?

Partly

FunnelGuard checks part of this.

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.