How to programatically send an event to GA4 without Google Tag Manager?

Viewed 2469

I would like to send a custom event to the new Google Analytics programatically, without defining it in the GTM first.

I am using Google Tag Manager and according to this article: https://developers.google.com/analytics/devguides/collection/ga4/translate-events

It is only possible by defining an event in the GTM itself. I can not do that and would like to keep doing this using the old method, where you could just do:

        if ("ga" in window) {
            var tracker = ga.getAll()[0];
            if (tracker) {
                tracker.send("event", eventCategory, eventAction);
            }
        }

How can I achieve this effect using JavaScript with the new Google Analytics (GA4)?

window.gtag is undefined as I'm using GTM, and window.ga is undefined as I'm using GA4. The only tag configured in the GTM is "Google Analytics: GA4 Configuration".

1 Answers
Related