Skip to content
FunnelGuard

Fix guide by FunnelsGuard

dataLayer declared after GTM snippet: how to fix overwritten pushes

TRK-128High impactPartlyBy FunnelGuardPublished Updated
On this page

What this issue is

The dataLayer is a JavaScript array on your page. Your site pushes information into it (page type, product, form submitted, order value) and GTM reads it to decide which tags to fire.

The GTM snippet creates the array if it does not exist, then pushes a gtm.start event into it. If a later script on the page runs window.dataLayer = [{...}] with a plain assignment, it replaces that array with a new one. GTM was watching the old object, so gtm.start and any earlier pushes are gone. Pushes made after the overwrite may also behave oddly, because GTM has wrapped the original array's push method.

Broken: declaration after the snippethtml
<head>
  <!-- Google Tag Manager -->
  <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
  <!-- End Google Tag Manager -->

  <script>
    // This replaces the array GTM already started using
    window.dataLayer = [{ pageType: 'lead-form', country: 'US' }];
  </script>
</head>

Why it costs you leads or sales

Tags that depend on lost data stop working, and nothing looks broken on the page. The site loads, forms submit and orders go through. Only your reporting and ad platforms suffer.

What typically goes wrong
  • Triggers that wait for a custom event pushed early in the page load never fire, so conversions are not recorded.
  • Variables that read page or user data (page type, product ID, order value) return undefined.
  • Ad platforms such as Google Ads and Meta get fewer conversions, so their bidding optimises on weak data.
  • The problem can be intermittent: it depends on whether the overwrite runs before or after your pushes.
How the overwrite loses your data
  1. GTM snippet loadsCreates dataLayer and pushes gtm.start
  2. Page script declares dataLayerwindow.dataLayer = [...] replaces the array
  3. Triggers wait for eventsEarly events and data are gone
  4. Tags fireGA4, Ads and Meta get nothing or wrong values

The break happens silently on page load, before any visitor action.

A tag waits for data that was overwritten

The trigger depends on a dataLayer event that no longer exists, so the tag never fires.

How to check if your site has it

Check the order of the dataLayer and GTM
  1. View the page source

    Open the page, right-click and choose View Page Source. Search for dataLayer. Note whether any window.dataLayer = assignment appears below the GTM snippet.

  2. Inspect the array in the console

    Open DevTools, go to Console and type dataLayer. Expand the array. If the first item is not {gtm.start: ..., event: 'gtm.js'}, the array was probably replaced.

  3. Use GTM Preview mode

    In GTM click Preview and enter your URL. In Tag Assistant, check the Summary for a Container Loaded event and any early page events you expect. Missing events are a warning sign.

  4. Search your theme and plugins

    Search the code for dataLayer = and dataLayer=. Look in the theme header, tag plugins, consent tools and any custom scripts.

How to fix it

There are two safe fixes. Use both if you can. First, put any dataLayer declaration above the GTM snippet. Second, never use a bare assignment. Use the safe form so existing content is kept.

Fixed: declare first, using the safe formhtml
<head>
  <script>
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({ pageType: 'lead-form', country: 'US' });
  </script>

  <!-- Google Tag Manager -->
  <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
  <!-- End Google Tag Manager -->
</head>
Overwrite vs safe push

Before

  • window.dataLayer = [{...}] after the GTM snippet
  • gtm.start and early pushes lost
  • Triggers and variables fail silently

After

  • Declaration uses window.dataLayer = window.dataLayer || []
  • Data pushed with dataLayer.push(...)
  • gtm.start and your events both present
Where to make the change
  1. Find the declaration

    Search your theme (header.php, functions.php) and plugins for dataLayer =. Check any plugin that adds a dataLayer or GTM snippet, such as a GTM or consent plugin.

  2. Reorder or rewrite

    Move your dataLayer code above the GTM snippet in header.php, or change it to window.dataLayer = window.dataLayer || []; followed by push. If a plugin outputs it, check its settings for a dataLayer or load-order option before editing code. Use a child theme so updates do not undo your edit.

How to confirm it's fixed

Verify after the change
  1. Clear caches

    Purge page, plugin and CDN caches so the old header is not served.

  2. Check the array

    Reload and run dataLayer in the console. Your own pushes and the gtm.start event should both be present.

  3. Run GTM Preview

    Confirm the events you expect appear in Tag Assistant and that the tags attached to them show as Fired.

  4. Check the platforms

    Look at GA4 DebugView and your ad platform's diagnostics to see the events arrive with correct values.

How to prevent it happening again

Habits that keep the dataLayer safe
  • Agree one rule: always window.dataLayer = window.dataLayer || [];, never a bare assignment.
  • Keep dataLayer setup in one place, above the GTM snippet.
  • Review theme updates, new plugins and agency code changes for new dataLayer declarations.
  • Run GTM Preview and check dataLayer[0] after every release that touches the header.
  • Keep a short note of which events your site pushes and when, so gaps are easy to spot.

How FunnelGuard handles this

FunnelGuard detects this issue only partly. It does not read your page source or flag a dataLayer assignment that sits after the GTM snippet. What it does check is the symptom: when it runs your real customer journeys on a schedule, it looks for the expected dataLayer events and tracking hits, and it reports when dataLayer events are missing.

A missing-events alert tells you something is wrong and where in the journey it happens, with a screenshot and a likely cause. It does not prove the overwrite is the cause. Failures are re-checked from a second region before one alert goes out.

Questions people ask

Why are my dataLayer events missing in Google Tag Manager?

A common cause is a script that runs window.dataLayer = [...] after the GTM snippet, which replaces the array and discards earlier pushes. Other causes include a renamed event or a trigger that no longer matches. Check dataLayer[0] in the console and look for the assignment in your page source.

Should I declare the dataLayer before or after the GTM snippet?

Before. Put window.dataLayer = window.dataLayer || []; above the snippet, then push your data. GTM can read anything pushed before it loads, as long as the array is not replaced later.

What is the difference between window.dataLayer = [] and window.dataLayer = window.dataLayer || []?

The first always creates a new empty array and discards any existing one. The second reuses the existing array if there is one and creates it only if missing. Use the second form everywhere.

How do I check if something is overwriting my dataLayer?

Reload the page and type dataLayer in the DevTools console. The first item should be the gtm.start object. If it is missing, search your theme, plugins and Custom HTML tags for dataLayer = and test with GTM Preview.

Can overwriting the dataLayer break Google Ads and Meta conversions?

Yes. If a trigger or variable depends on data that was lost, the conversion tag does not fire or sends wrong values. Ad platforms then see fewer conversions and optimise on weaker data.

Is it okay to push to the dataLayer after the GTM snippet?

Yes. dataLayer.push(...) after the snippet is the normal way to send events. The problem is only reassigning the array with =. Pushing adds to the array GTM is already watching.

Can FunnelGuard detect a dataLayer declared after the GTM snippet?

Only partly. It checks for missing dataLayer events and tracking hits during scheduled journeys, but it does not inspect your source for the bad declaration. You confirm the cause by hand with the console and GTM Preview.

Does FunnelGuard catch this?

Partly

FunnelGuard checks part of this.

How: dataLayer events missing.

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.