Search code examples
google-analyticsgtag.js

40% drop in number of page views (and users and sessions) after updating ga.js to gtag.js


I manage a website which has a corresponding Google Analytics property created many years ago whose ID is like "UA-XXXXXX-X" (where the X's are digits).

Up to now, the tracking code at the top of every html page used to be this:

<script>
window.google_analytics_uacct = 'UA-XXXXXX-X'; var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXX-X'], ['_trackPageview'], ['_trackPageLoadTime']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();
</script>

which is what Google had told us to use many years ago.

Yesterday I went to Admin -> Tracking Info -> Tracking code (for the very same property) and realised that the tracking code that they give now is:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  
  gtag('config', 'UA-XXXXXX-X');
</script>

So I replaced the old code with the new code in the HTML.

After that, today, the visits (both in terms of page views, users, and sessions) have dropped between 40 and 50%. It's not a real drop in traffic, there has been no significant decrease in throughput in terms of requests received by the server (and we're talking about ~20k users per day).

If I had done something wrong I could have expected to see exactly zero visits, but around 50% less visits makes no sense.

What could be the issue?

Actually, the exact same thing has happened with two other smaller websites, which have about 1/10th the traffic of the big one (I updated all three at the same time, each with its own tracking ID); for the smallest, the drop has been of about 80%.

enter image description here


Solution

  • Well apparently gtag.js simply doesn't work with UA-XXX properties.

    They claim it does, they tell you to use it (meaning, the tracking code they give you in the admin panel of your UA property to copy and paste is the one that uses gtag.js), but it won't work.

    Source: none except for my own experience. Nothing in the docs tells you this; in fact, they tell you the contrary.

    I have done this for half a dozen of UA-XXXX properties, replacing the old ga.js, supposedly obsolete (twice obsolete, as it had already been supposedly replaced with analytics.js), which was working, with the new gtag.js, and in all cases, the amount of data has dropped by anywhere from 50% to almost 100% (i.e. practically no data).

    Then I just created new GA4 properties for the same websites, I started tracking them, and I'm getting the data again just fine. Well, I'm getting the data with a lag of several hours, whereas with the old UA properties and the old JS code I was getting all the data instantly, but that's just Google: new versions offer a worse service than the older versions. However, apart from the lag, I now get correct and apparently complete data again.

    To summarize, there are three versions of js code for Google Analytics:

    • ga.js: super obsolete, you are not supposed to use this one (yet I was until yesterday and it was working perfectly with UA-XXX properties). Works with UA properties, not with GA4 properties (i.e. G-XXX)
    • analytics.js less obsolete. I haven't used this one so I can't offer any experimental data. It is supposed to replace ga.js, and if I'm not mistaken it's supposed to work with UA properties but not GA4 properties.
    • gtag.js the new one. It is supposed to replace everything else and support both UA and GA4 properties (UA-XXXXX and G-XXXXX) but in reality only works with GA4 properties despite all claims to the contrary in the docs. If you use this one with a UA property, you'll lose a random (huge) percentage of the data, with no warning that anything is wrong and no indication of how much downsampling you're getting.

    So, they recommend you to migrate to gtag.js, but if you do, you also have to switch to a new GA4 property.

    Which will break backwards compatibility with almost any preexisting tools that had anything to do with the API:

    • anything you had using the Google Analytics API that was compatible with the old UA properties needs to be completely rewritten using the new Google Analytics Data API (which btw is in beta, it's not stable, it's buggy and lacks features that were present in the old one). Oh, and this new stuff will not be compatible with old UA properties. It goes both ways.
    • the Embed API (a JS api that allowed you to easily embed front-end pre-made graphs with Analytics data with zero effort) is not compatible and there is no replacement (you need to do it from scratch with the new API and Google Charts).

    This is just how Google does business.