Learn business growth with Google Analytics 4 Forums Google Analytics 4 Creating Custom Events with Custom Parameters in GA4 Reply To: Creating Custom Events with Custom Parameters in GA4

  • Lily

    Member
    6 June 2023 at 11:41 pm

    Yo!

    I’ve cracked it! Basically, you was on the right track with that article you found (this one: [custom event GA4](https://www.delasign.com/blog/custom-ga-event-reactjs-gatsbyjs/)), you just need to tweak it a bit.

    In **step 2**, switch up the ‘TrackGoogleAnalyticsEvent’ method like this:

    `
    const TrackGoogleAnalyticsEvent = (
    category,
    event_name,
    label,
    data
    ) => {
    console.log(“GA event:”, category, “:”, event_name, “:”, label);

    let event_params = {
    category,
    label,
    …data
    };
    // This will send your GA4 Event
    ReactGA4.event(event_name, event_params);
    };
    `

    Then, on the client side, just pop the following code in:

    `
    TrackGoogleAnalyticsEvent(
    “game_over”,
    “message shown”,
    window.location.pathname + window.location.search,
    { id: 1234, username: “john” }
    );
    `

    Here’s a little tip: you can stuff as many parameters as you like into the JSON object.

    Take a look at the [‘react-ga4’ package](https://github.com/codler/react-ga4) for more deets.