Search code examples
javascriptgoogle-analyticsanalytics

Google Analytics ga() does not work with new GA codes


We have a client that is still using ga() (not gtag()) as google analytics.

We use to do:

ga('create', "UA-XXX", {name: 'companyname'});

and to send pegeview:

ga('companyname.send', {
    hitType: 'pageview',
    title: "some-title",
});

But this only works with UA codes. When we try the new google tag if that starts with G-XXX, the code above does now work.

How can we use the new G-XXX with the ga() function and not gtag()?

Thanks in advance.


Solution

  • TL;DR

    To use the ga() library, you need to be using Universal Analytics properties (and property IDs UA-xxxxx-x).

    Setup a Universal Analytics property, it is hidden. Instructions below.

    Disclaimer: I operate a Google Analytics consulting firm affiliated with Google.

    Difference in IDs

    The ids you are referring to (G-XXXXX) are "Measurement Id's" for GA4 properties. These GA4 properties are not built to consume Universal Analytics data sent through the Universal Analytics library, ga(). Hence, initializing a GA4 Measurement ID in place of a UA Property ID will fail.

    Option 1: Setup a UA Property

    Set up a UA property and continue to record data the way you have before. This is likely what you are trying to do.

    As of the mid-2021, the Google Analytics interface is defaulting the creation of new properties to GA4 rather than Universal Analytics. You can still create a UA property, it is just hidden:

    Advanced Settings

    Option 2: Migrate to GA4

    GA4 is the future (as of now 2021). It is event/user based instead of session based like UA. There is some translation of default tracking e.g. pageviews and events, but the concept of metadata and scoping is different on the whole, way more flexible, performant, and ready for machine-based activation... but different.