HomePosts

Fix GA Events not being sent due to unknown target undefined or window.ga not defined

Web Dev

Fri Dec 30 2022

When integrating Google Analytic events into your application you may encountered similar issues such as:

Command ignored. Unknown target: undefined

window.ga not defined

The first point of call is to understand what version of GA you are using to send events.

A few years ago GA changed their default GA tracking method to make use of Google Tag Manager know as Gtag instead of using GA directly. As of early March they have now deprecated the Google Universal Analytics infavour of GA 4.

However a lot of sites still use Google Universal Analytics and therefore you may need to know how to send events using Google Universal Analytics. 

If you see either of the issues mentioned you should check what version of GA you are including in your site.

If you see "https://www.googletagmanager.com/gtag" being imported within the site then you are using the Gtag version of Google Universal Analytics.

This means that window.ga or ga is not available on the site. As the reference to Google Analytics is via gtag on the window. 

Instead of using window.ga where each argument is a separate parameter instead you need to use gtag where the first argument is the event, the second is an argument to clarify what the event is, and the third is an object containing the event label, event category and event action.

gtag('event', 'event', {
        'eventCategory': 'article',
        'eventAction': 'click',
        'eventLabel': 'example-label', 
        'eventValue': 'example-value'
})

The snippet should help you understand how you send the gtag event.