Analytics.js + GA4. It’s possible!

Google announced that Universal Analytics is going away. There is little to do if you use it as your primary analytics solution. You should switch from Universal Analytics to Google Analytics 4 as soon as possible. 

There are at least a few ways how to do it. Therefore, I recommend you to look at my other post, “How to upgrade to Google Analytics 4”, where I mentioned all of them. 

In this article, I will guide you on upgrading to Google Analytics 4 using your existing analytics.js setup.

What’s analytics.js, and how to check if you are using it? 

Analytics.js is a Google Analytics javascript library that sends the behaviour information from your website into Google Analytics 3 (Universal Analytics). 

It was created a while ago and used before GTAG.js, the current primary Google tag solution.  If you set up Universal Analytics before the 2018 year and you didn’t use Google Tag Manager, most likely, you are using Analytics.js.

If you want to check if it’s true. You should visit your website and check the source code of the page and see if the page has the following code.

<!-- Google Analytics -->
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  ga('create', 'TAG_ID', 'auto');
  ga('send', 'pageview');
<!-- End Google Analytics -->

If it has, you are using Analytics.js library. If you don’t see it, I recommend you look at my other post, “How to upgrade to Google Analytics 4”, since most likely you are using another solution for Universal Analytics, and this article is not entirely relevant to you.

So, now, after we are on the same page that you are using it, let’s discuss how to use analytics.js library to migrate to Google Analytics 4.

How to migrate to GA4 using Analytics.js 

First of all, you should know there is no possibility to transit your analytics.js setup into Google Analytics 4 fully. GA4 allows you to track only the following UA hits:

  1. custom events
  2. timing
  3. exception calls.

Therefore, you will need anyway to change your pageview tag into a custom event, or you will need to add gtag.js library for the page view tag on your website.

So, for instance, if you have analytics.js code for pageview like that below,

<!-- Google Analytics -->
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  ga('create', ‘UA-ID’, 'auto');
  ga('send', 'pageview');
<!-- End Google Analytics —>

You can change it to GTAG.js

<!-- Google tag (gtag.js) -->
<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', ‘GA4_Measurement_ID');

However, all your Analytics.js tags in the following format will reach your GA4 property.

So, for instance, I have this code to track the UA event.

ga('send', 'event', event_category, event_action, event_label, 1);

Google Analytics 4 will automatically detect this code and send it to the right GA4 property. 

There is just one crucial step that we should do to activate Analytics.js event tracking in GA4. We should go to:

  1. GA4 -> Admin -> Data Streams -> Web Data Stream Details -> Configure Tag Settings -> click on “Show all”

  2. Activate “Collect Universal Analytics events”. 

Activate "Collect Universal Analytics events" in GA4
Activate “Collect Universal Analytics events” in GA4

After we activate this, all events that you send into Universal Analytics using Analytics.js library will be automatically sent to the right GA4 property. Yes, it’s so easy and so fast!

You can see my UA event in Universal Analytics and Google Analytics 4. GA4 reads the analytics.js library and uses event action as the name for the event, and event category, event label and event value as the event properties. 

Analytics.js Event in Universal Analytics
Analytics.js event in Universal Analytics
Analytics.js Event in GA4
The same Analytics.js event in Google Analytics 4

If you want to use these properties, you should set them up as a custom dimension or metric in Google Analytics 4. 

Final Word

If you use Analytics.js library for Universal Analytics, there is a way to transit 99% of your setup into Google Analytics 4 using one of the GA4 features. In this article, I walked you through each and every step. If you found this article helpful or have a question, please don’t hesitate to comment below.

Is it possible to migrate to GA4 using Analytics.js library

Yes, it’s possible to migrate almost 99% of all your Analytics.js setup to Google Analytics 4. You should activate “Collect Universal Analytics events” feature in GA4. 

2 Replies to “Analytics.js + GA4. It’s possible!”

  1. Hello,

    Great job on your Blog.

    I have the following question:

    If I use Analytics.js library and perform migration to GA4. I check that both UA and GA4 are collecting events.

    I have activated “Collect events from Universal Analytics”.

    Do I have to change the Analytics.js source code to GTAG.js code on the web or will it work without changing it from July 1st?

    Thank you very much for your time.

    Best regards,

Leave a comment

Your email address will not be published. Required fields are marked *