Skip to content
Search
  • ServicesPage
  • Our workPage
  • Case studiesPage
  • BlogPage
  • AI solutionsPage
  • Contact usPage
All articles

Google Analytics recorded nothing, because of one line

Moinul Hossain Miraj · · 3 min read · SEO and PPC

Google Analytics recorded nothing, because of one line

After a site launch, Google Analytics showed one or two sessions a day, and nothing in Realtime. The tag was on the page. The cookie banner worked. Visitors clicked "Accept". Still nothing arrived.

The cause turned out to be one line.

The setup

Like many sites with a cookie banner, we loaded Google Analytics only after the visitor agreed. Our code defined the gtag function itself, then loaded Google's script:

js
window.dataLayer = window.dataLayer || [];
window.gtag = (...args) => window.dataLayer.push(args);
gtag("js", new Date());
gtag("config", "G-XXXXXXX");

It reads well. The script loaded, and dataLayer filled up with entries. But not one hit was sent.

The bug

Google's script does not read plain arrays from dataLayer. It expects each entry to be the special arguments object that a normal function receives. An arrow function with ...args creates an ordinary array, and gtag.js quietly ignores it. No error, no warning: the js and config commands simply never run.

The standard snippet from Google is written the old way for exactly this reason:

js
window.gtag = function () { window.dataLayer.push(arguments); };

Changing that one line fixed it. In the browser's Network tab, a collect request to Google appeared right after "Accept", and visits showed up in Realtime within a minute.

If your linter complains (prefer-rest-params), add an exception on that line. This is one of the few places where arguments is required.

How to check your own site

  1. Open the site, accept analytics cookies, and open DevTools.
  2. In the Console, run dataLayer.map(e => Object.prototype.toString.call(e)).
  3. Entries from gtag should show [object Arguments]. If they show [object Array], you have this bug.
  4. In the Network tab, filter for collect. After accepting, you should see requests with your measurement ID.

While we were there: Consent Mode v2

Loading the tag only after consent is good, but Google also expects Consent Mode signals, and some tags (for example from a Tag Manager container) can load independently of your banner. So we added two pieces.

First, defaults in the page head, before any Google tag can run: everything denied.

html
<script>
  window.dataLayer = window.dataLayer || [];
  window.gtag = function () { dataLayer.push(arguments); };
  gtag("consent", "default", {
    ad_storage: "denied",
    analytics_storage: "denied",
    ad_user_data: "denied",
    ad_personalization: "denied",
    wait_for_update: 500,
  });
</script>

Second, an update whenever the visitor makes a choice in the banner:

code
gtag("consent", "update", {
  analytics_storage: choice.analytics ? "granted" : "denied",
  ad_storage: choice.ads ? "granted" : "denied",
  ad_user_data: choice.ads ? "granted" : "denied",
  ad_personalization: choice.ads ? "granted" : "denied",
});

Note that the head script defines gtag with arguments too. Since it is defined once there, the rest of the code can reuse it instead of defining its own.

What we learned

  • "The script loads" is not the same as "data is sent". Check the network requests, and compare with the numbers in Analytics itself.
  • Small rewrites of copied snippets can break them silently. Google's snippet looks old-fashioned for a reason.
  • Test the full path after launch: accept the banner, then watch Realtime.

We use AI coding tools in our work, and an engineer reviews every change. This bug is a good example of why: the code looked correct, and only checking the actual result showed it was not. If your analytics numbers look too low, we can take a look.

Have a project in mind?

Tell us what you need. We get back to you as soon as possible.

Start a project