Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to Capture and Monitor a JavaScript Event in GA4 Using GTM Reply To: How to Capture and Monitor a JavaScript Event in GA4 Using GTM

  • Addison

    Member
    14 May 2023 at 7:33 pm

    Certainly, mate. The basic idea is to push the event data into the data layer which GTM can access and then send it to GA4. To do this, you’d modify your “doThis” function to push your event data into the ‘dataLayer’ like so:

    `javascript
    function doThis() {
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
    ‘event’: ‘event_triggered’,
    ‘custom_param’: ‘custom_value’
    });
    }
    `
    Where ‘event_triggered’ is the name of the event that you want to track, and ‘custom_param’/’custom_value’ represents any additional data you want to track.

    Next, in GTM, create a new User-Defined Event trigger with the event name matching ‘event_triggered’ from your code. Then, create a GA4 Event tag for your event, with the above trigger. The “event name” in the GA4 Event tag should read directly from the dataLayer as well.

    To test if it’s working, go to the Preview Mode in GTM, trigger your custom event on your site, and then check if your GA4 Event tag fires in the GTM preview panel. Once confirmed, you should be able to view those events in your GA4 in real-time report as well as in the event reports.