Learn business growth with Google Analytics 4 Forums Google Analytics 4 How can I send user properties in React-GA4 for Google Analytics?

  • How can I send user properties in React-GA4 for Google Analytics?

    Posted by Sawyer on 1 April 2023 at 11:39 am

    Hey, so I’ve been working with react-ga4 recently. All good, but I’m a little stuck on how to send user properties with it and then set those up in the Google Analytics panel – I think I might be missing something. I’ve got ReactGA4 initialized like this:

    ReactGA.initialize(
          [
            {
              trackingId: id,
              gaOptions: {
                role: userRole,
              }
            },
          ]
    )
    

    Any ideas or advice you could toss my way?

    Sophia replied 1 year, 3 months ago 3 Members · 2 Replies
  • 2 Replies
  • Michael

    Member
    7 May 2023 at 10:08 am

    Sure! So, it looks like the piece you might be missing is how to use the ReactGA.gtag function to send user properties. This can be done directly whenever you want to track some custom analytics data, or you can save yourself some time and effort by setting it up in a utility function.

    To put it simply, the gtag function takes a string and an object of data to send back to Google Analytics. You can use something like "set", "user_properties" for the string, and then put whatever data you want in the object. After that, it’s as simple as just calling the function whenever you want to send data.

    Here’s an example of tracking whether a user’s account is verified or not, and what their name is:

    `jsx
    import ReactGA from “react-ga4”;

    ReactGA.gtag(“set”, “user_properties”, {
    account_verified: true,
    });

    ReactGA.gtag(“set”, “user_properties”, {
    name: “John”,
    });
    `

    You can also create utility functions to simplify tracking your properties, like this:

    `jsx
    import ReactGA from “react-ga4”;

    export const setAccountProperty = (value: boolean) => {
    ReactGA.gtag(“set”, “user_properties”, {
    account_verified: value,
    });
    };

    export const setNameProperty = (value: string) => {
    ReactGA.gtag(“set”, “user_properties”, {
    name: value,
    });
    };
    `

    After setting up everything in your app, you can go to your Google Analytics dashboard and click on DebugView to check if everything’s working fine.

  • Sophia

    Member
    24 May 2023 at 11:10 am

    From looking at your initialization, I see that you’re using gaOptions for user properties. However, gaOptions is actually used to set up fields that control how the pageview hit is processed, so it’s not the right place to put user properties.

    To send user properties, you should use the set command, which is used to set user-level and session-level dimensions and metrics. You can use it similar to how you’d do in Google Analytics’ JavaScript (gtag.js). It would look something like the following:

    `
    ReactGA.set({ ‘user_role’: userRole });
    `

    Keep in mind that you first need to create these user properties in Google Analytics and use the exact name you set there. In the new GA4 property, you’ll need to set it up under User-Scoped Custom Definitions.

    However, as per react-ga4 official documentation, please note that set command is not currently supported. Therefore, you might need to leverage gtag.js directly or use another library that supports GA4 user properties if this is mandatory for your application.

    This is a rapidly developing area – make sure you are on top of the latest releases and checking the react-ga4 GitHub page for any updates.

Log in to reply.