4 min read

Product

My Analytics Showed Zero Conversions for 90 Days. The Tracking Was Dead.

For 90 days my GA4 showed page views but not one custom event. An Astro directive had quietly hidden gtag from every caller, and nothing looked broken.

ga4 custom events not firingastro define:varsgtag is not defined

My Analytics Showed Zero Conversions for 90 Days. The Tracking Was Dead. · Products Decoded
By Debashis Nayak · Products Decoded31 Oct 2026

Until 22 October, my own site had never sent a single custom event to Google Analytics. Not one in the 90 days before I found it. The dashboard still looked alive, with page views climbing and scroll events ticking, so I had no reason to open it up and look.

The numbers that mattered were all zero, including the leads and purchases on my Ship in 7 page. I had been reading those zeros as “nobody is converting yet”. They were zeros by construction.

Why GA4 showed page views but no custom events

This is the part that made it so hard to see. GA4 collects some events on its own: page_view, scroll, outbound click. Those kept arriving, so the property looked healthy.

My custom events were a different path. The site calls window.gtag(...) (through a small tracking helper) whenever someone clicks something I care about. If window.gtag does not exist, that call does nothing. No error in the console that I ever noticed, no failed request, no warning in GA4. A silent no-op.

So the question was why window.gtag was undefined on a site that was clearly sending page views.

What define:vars does to your script in Astro

My base layout bootstraps Google Analytics in a script tag that looks roughly like this (simplified):

<script is:inline define:vars={{ ga }}>
  window.dataLayer = window.dataLayer || [];
  function gtag(){ dataLayer.push(arguments); }
  gtag('js', new Date());
  gtag('config', ga);
</script>

I used define:vars to pass my measurement ID from the server side into the script. That directive does its job, and it does one more thing that I had never read about: it wraps your script body in an immediately invoked function, (function(){ ... })().

Inside that wrapper, function gtag(){} is a local function declaration. It is not a global. Nothing outside the wrapper can see it, and window.gtag stays undefined.

And here is why the page views survived. The gtag('config', ...) call runs inside the same wrapper, where the local gtag is perfectly visible. So the config call worked, the page view fired, and the setup looked correct from every angle I checked.

Every other caller, living in separate scripts on the page, reached for window.gtag and found nothing. That covered:

  • the 57 elements with a data-evt attribute that my click tracker listens to
  • the ask_ai buttons
  • generate_lead and purchase on Ship in 7
  • assessment_complete on the AI-readiness quiz
  • the Holt Teams page

I’m fairly sure I wrote the analytics setup once, saw page views in the realtime report, and never tested a custom event again. That’s the lazy check, and it passed for 90 days.

The fix: assign gtag to window

One line in the layout:

// before: local to the IIFE that define:vars creates
function gtag(){ dataLayer.push(arguments); }

// after: also exposed globally
const gtag = window.gtag = function(){ dataLayer.push(arguments); };

Same function, now reachable from every script on the page. Anything defined inside a define:vars script that other scripts need has to be put on window explicitly. There is no other way out of that wrapper.

If you would rather not touch the bootstrap, the other route is to drop define:vars and read the measurement ID some other way. I took the one-line route.

How to verify a GA4 event actually reaches /g/collect

I would not trust the GA4 reports for this check, because they lag and because a missing event looks the same as a quiet day. Look at the network request instead.

  1. Open the page in Chrome with DevTools open on the Network tab.
  2. Filter by collect.
  3. Click the thing that should fire your event.
  4. Find the request to /g/collect and check its query string for en=<your event name>.

No request means the event never left the browser. A request with the wrong name means your tracker is sending something other than what you think.

I ran this in headless Chrome after the fix, and ask_ai:chatgpt and ask_ai:claude both showed up as requests to /g/collect. That was the first time in 90 days I had seen a custom event leave the page. One more quick check before you start: type window.gtag in the console. If it prints undefined, you have my bug.

I also got the checking wrong at first. Right after pushing a deploy to my host, the old container can keep answering for a minute. My first check read the stale version and told me the fix had not worked. If a fix looks dead right after a deploy, wait and test again before you start second-guessing the code.

What I can and cannot trust from those 90 days

Anything custom in GA4 before 22 October is zero by construction. It does not tell me nobody converted. For that stretch, the truth lives in my payment provider’s records and my own database, and I use those for Ship in 7.

I also added an event-scoped custom dimension called item on 22 October, so I can break ask_ai clicks down by which AI they went to. It only applies to data collected from that day forward, so even the dimension has no past to look at. Next to the server bill that kept coming after I deleted the server, this is the second time a green surface hid a dead one.

Checklist before you trust your GA4 setup

  • Type window.gtag in the browser console. It should print a function.
  • Fire one custom event and find it in the Network tab as a /g/collect request.
  • If you use define:vars in Astro, grep for any global your other scripts depend on and put it on window.
  • Treat page_view as proof of nothing except page views.
  • Write down the date your tracking actually started working, so future you does not read old zeros as results.