Skip to content
FunnelGuard

Fix guide by FunnelsGuard

dataLayer event pushed before GTM loaded: how to fix it

TRK-130Medium 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, such as generate_lead or purchase, and Google Tag Manager (GTM) reads them to decide which tags to fire.

GTM does read events that were pushed before it loaded. But problems still happen. The usual causes: the code calls dataLayer.push before dataLayer exists, so it throws an error. Or a script replaces the array with dataLayer = [] after GTM started. Or the event fires before the page has its other data, and the trigger or variable that needs that data comes up empty. In each case the event is never processed by a tag.

Where the event gets lost
  1. Page starts loadingTheme or app script runs early
  2. dataLayer.push(event)Runs before the array exists or is later overwritten
  3. GTM container loadsFinds no matching event
  4. Conversion tagNever fires

The push runs before the dataLayer is ready, so nothing downstream sees it.

Why it costs you leads or sales

The lead or sale still happens, but GA4, Google Ads, Meta and other platforms never hear about it. Reports undercount conversions, and ad platforms learn from less data. Smart bidding can then spend on the wrong audiences. It often hits only some visits, such as slow connections or cached pages, so it is easy to miss.

Recorded vs real conversionsExample numbers
Recorded vs real conversions (example)Real leads: 100 conversions; Recorded in GA4: 72 conversions0387710072Real leadsRecorded in GA4conversions

Example: 100 real form leads in a week, but the early push is missed on some page loads.

How to check if your site has it

Check the order of events
  1. Open GTM PreviewGoogle Tag Manager

    In Tag Manager, click Preview, enter your page URL and run the journey that should send the event.

  2. Look at the event list

    In Tag Assistant, check the left column. Your custom event should appear after Container Loaded, or be shown in the Data Layer tab of an earlier message.

  3. Check the browser console

    Open DevTools, then Console. An error such as 'Cannot read properties of undefined (reading push)' means the push ran before the dataLayer existed.

  4. Inspect the array

    Type dataLayer in the console after the action. If your event is missing, it was overwritten or never pushed.

  5. Compare trigger and tagGoogle Tag Manager

    If the event is present but the tag shows as Not Fired, check the Custom Event trigger name. It is case sensitive.

How to fix it

The goal is that the array always exists first, is never replaced, and that events carry their data when pushed.

Safe order in the headhtml
<script>
  window.dataLayer = window.dataLayer || [];
</script>
<!-- Google Tag Manager snippet goes here, right after -->
Safe push anywhere in your codejavascript
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'generate_lead',
  form_id: 'contact'
});
Where to apply it
  1. Find where the push comes from

    Check your theme's header.php, a code snippets plugin, or the form plugin's custom JavaScript.

  2. Declare the array first

    Add the one-line declaration before the GTM snippet, or use window.dataLayer = window.dataLayer || [] in every push.

How to confirm it's fixed

After the change
  • No console errors about dataLayer or push
  • The custom event appears in Tag Assistant after the action
  • The tag shows under Tags Fired for that event
  • The hit appears in GA4 DebugView or the platform's own test tool
  • Test on a hard refresh and with the cache cleared

How to prevent it happening again

Habits that stop a repeat
  • Keep one agreed snippet that declares the dataLayer above GTM
  • Ask developers to document event names, and match them in triggers
  • Re-run GTM Preview after theme, plugin or form changes
  • Publish a GTM version note describing each new event
  • Test the full journey on a schedule, not only at launch

How FunnelGuard handles this

FunnelGuard detects this only partly. Its robot visitor runs your real journey, such as a lead form, and checks that the tracking hits for GA4, Google Ads, Meta and the other platforms actually go out. If an early or missed push means a conversion hit never fires, a failed hit check can reveal the symptom, and you get one alert with a screenshot and a likely cause after a second-region re-check.

Questions people ask

Does GTM process dataLayer events pushed before it loads?

Yes, if the dataLayer array exists. GTM reads messages already in the array when the container loads. The problem starts when the array is missing, overwritten, or the event lacks data the trigger needs.

Why is my dataLayer push not triggering a tag in GTM?

Common causes are a mismatch in the event name (it is case sensitive), a push that ran before the array existed, or a trigger with extra conditions that are not met. Check Tag Assistant to see whether the event appears at all.

How do I make sure the dataLayer exists before GTM loads?

Add window.dataLayer = window.dataLayer || []; in the head, above the GTM snippet. Use the same form in any script that pushes events.

Why do I get 'Cannot read properties of undefined (reading push)'?

Your code calls dataLayer.push before the array has been created. Declare it first with window.dataLayer = window.dataLayer || [].

How can I see events that GTM missed?

Type dataLayer in the browser console to view the array, and use GTM Preview to see which events it processed. If the event is in the array but not in Preview, check the container loading order and the trigger.

Can overwriting dataLayer break tracking?

Yes. Setting dataLayer = [] after GTM has started replaces the array and cuts the link GTM uses. Always use dataLayer = dataLayer || [].

Can FunnelGuard detect a dataLayer push that ran too early?

Only partly. It checks that the tracking hits go out on your real journeys, so a missing conversion hit can be flagged. It does not inspect your dataLayer code or timing, so confirm that by hand in GTM Preview.

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.