Tracking Users With Google Analytics 4 (GA4)
In this story, I will talk about how to track user's activities and identify users on website with Google Analytics 4 and Tag Manager. before starting make sure you have GA4 account with basic setup and set your tag manager in frontend project, index HTML file.
Let’s start, by first ensuring that your GA4 account already set data streams.
These are all defined variables I have, only need these 2 variables.
Configure user-id as data layer variable for storing user_id. The user_id is built-in variable, will automatically be used by GA4 to identify users. In my case, the report needs to be based on the company, so I will store the company ID here.
Configure ws_user_id_var, another data layer variable that I use to store the real user id of website user.
After having both needed variables. We need to define an event so we can use it to set the value of our variables, user-id and ws_user_id_var. Create set-scope-props-tag event and link to both variables.
The event needs a trigger to make the event fire. Here I set the trigger to fire on all custom events and link it to set-scope-props-tag event. The relevant custom events of this trigger will appear on this configuration page.
Last, This is how my GA4 configuration is configured, make sure that the Measurement ID of data streams is correct. I define user-id and ws_user_id_var here to be able to use them across all events.
setScopeProps(companyId: number, userId: number){
window.dataLayer.push({
‘event’: ‘set_scope_props’,
‘user_id’: companyId,
‘ws_user_id_var’: userId,
});
}In the code, call dataLayer.push with the event and the variables to set the value. This function should be called after user login and logout. Also, good to be called on the first landing page of website to clear existing user variables.
Get Ciizo’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s look at the report.
In The debugging view, we can check all events in the last 30 minutes here. I call set_scope_props event on the first landing page of website so if no user session then the set_scope_props will clear the value of both variables. After user login set_scope_props event set 7800 to ws_user_id_prop and company id 119234 to user_id.
Note:
- Use the debugging view, to see real-time events, all variables that GA4 can set will be presented here.
- Tag manager preview mode (aka tag assistant), to see real-time tags firing. May see all defined variables were sent here but that doesn’t mean GA4 can set all of them.
This real-time report for the last 30 minutes.
To see the report that is separate per user-id sessions need to custom report including Signed in with user ID comparison. (To see data, You may need to wait 24 hours on the first day setting tag manager)





