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.
<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.
- 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.
- GTM snippet loadsCreates dataLayer and pushes gtm.start
- Page script declares dataLayerwindow.dataLayer = [...] replaces the array
- Triggers wait for eventsEarly events and data are gone
- Tags fireGA4, Ads and Meta get nothing or wrong values
The break happens silently on page load, before any visitor action.
The trigger depends on a dataLayer event that no longer exists, so the tag never fires.
How to check if your site has it
View the page source
Open the page, right-click and choose View Page Source. Search for
dataLayer. Note whether anywindow.dataLayer =assignment appears below the GTM snippet.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.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.
Search your theme and plugins
Search the code for
dataLayer =anddataLayer=. 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.
<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>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
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.Reorder or rewrite
Move your dataLayer code above the GTM snippet in header.php, or change it to
window.dataLayer = window.dataLayer || [];followed bypush. 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.
Open theme code
Go to Online Store > Themes > Edit code and open theme.liquid. Look for
dataLayer =and for the GTM snippet.Fix the order
Place your dataLayer lines above the GTM snippet and use the safe form. Check any installed tracking app too, since apps can inject their own declaration. Checkout pages use separate settings (customer events or checkout extensibility), so test them separately.
Check Custom HTML tags
Open Tags and look for Custom HTML tags that contain
dataLayer =. A tag that runs inside GTM and reassigns the array can wipe pushes too.Switch to push
Replace the assignment with
window.dataLayer = window.dataLayer || []; dataLayer.push({...});or justdataLayer.push({...});. Preview, then Submit and publish.
Open custom code
Go to Site settings > Custom code. Head code is where the GTM snippet and any dataLayer code usually live.
Order the code
Put the dataLayer declaration above the GTM snippet in the Head code box, using the safe form. Also check page-level custom code in Page settings. Publish the site.
Check tracking code fields
Open the funnel or website Settings and look at Head Tracking Code and the page-level custom code. Search for
dataLayer =.Reorder
Put the declaration above the GTM snippet in the same Head Tracking Code field, using the safe form, then save and republish.
Review custom code
Go to Settings > Custom code (Advanced). Find the GTM snippet and any script that sets dataLayer. If you used the built-in Google Tag Manager integration, check any extra custom code against it.
Fix the order
Place the dataLayer script before GTM, or switch to the safe form. Set it to load in the Head and on the right pages, then apply.
Open code injection
Go to Settings > Advanced > Code Injection. Check the Header field for the GTM snippet and any dataLayer assignment.
Reorder
Put the declaration before the GTM snippet in the Header field and use the safe form. Check page-level injection too. Save.
Search the codebase
Search templates, bundles and tag-injection scripts for
dataLayer =. Single-page app frameworks sometimes reset it on route change.Standardise
Create one small include above the GTM snippet that runs
window.dataLayer = window.dataLayer || [];. Replace every other assignment withdataLayer.push(...).
How to confirm it's fixed
Clear caches
Purge page, plugin and CDN caches so the old header is not served.
Check the array
Reload and run
dataLayerin the console. Your own pushes and the gtm.start event should both be present.Run GTM Preview
Confirm the events you expect appear in Tag Assistant and that the tags attached to them show as Fired.
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
- 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.
Related issues
- TRK-099GTM container snippet missingFix guide
- TRK-101Two GTM containers fighting (old agency + new)Fix guide
- TRK-105GTM snippet placed in footer, firing lateFix guide
- TRK-113Variables returning undefined (dataLayer key renamed)Fix guide
- TRK-106Trigger set to All Pages instead of thank-you pageFix guide
- TRK-115Custom HTML tag throwing JavaScript errorFix guide