Implementing User ID in Google Analytics 4 (GA-4) allows businesses to accurately track user behavior across devices and sessions, offering a clearer picture of the customer journey. With User ID, each visitor can be assigned a unique identifier, making it possible to follow their interactions as they move between platforms like mobile, desktop, or tablet. This creates more precise data, which helps understand user habits and improve overall marketing effectiveness.
In this blog, we’ll walk through the steps to implement User ID in GA-4, focusing on enhancing data security and user authentication. This guide will outline key processes and best practices, from configuring your property to integrating it with your website or app. Whether you’re just getting started with analytics or looking to enhance your current setup, implementing User ID will give you a deeper understanding of your audience and drive more informed business decisions.
What is the User ID in GA4?
- User ID lets us associate a permanent ID of a single user with that user’s engagement data from one or more sessions initiated from one or more devices.
- User ID helps us create reports that tell a more unified, holistic story about a user’s relationship with our business.
What are the Conditions Under Which User ID Can Be Implemented?
User IDs will work well for your business only if visitors can access your website or app. This means that once a visitor or user accesses his or her account, you have the ID in your database.
Steps to Integrate User ID Through GTM
- Please check if you have a Google Tag in your GTM Container. We will need to set the user_id parameter in that tag.
- User ID must be included in the user parameters of Google Tag. For best practices, add the user ID to all event tags.
- If a user is browsing our website anonymously (read not logged in), that variable will return undefined. Therefore, the user_id will not be sent to GA4. However, the ID becomes available once the user logs in to his/her account on your website. Then, the GA4 event tags will start getting the user ID.
Best Practices for Implementing Secure User ID Systems
- Historical Data: User ID is not applied to historical data that goes beyond the scope of the same session. When a new visitor arrives on your website, GA4 assigns a cookie with a randomly generated client ID (not to be confused with a user ID). If the user eventually registers on the site after several sessions, they are assigned a user ID. However, this user ID has not been retroactively applied to past sessions.
- On the other side, if a visitor performs an event, e.g., a page_view (without a user ID), then logs in during the same session, the user ID will also be automatically applied to the page_view. So, the events that happen before the user ID is sent to GA4 during the same session will also get the user ID.
- User ID length: The maximum length of the user ID that you can send to Google Analytics 4 is 256 Unicode characters.
- User ID can’t be merged across GA4 properties: To track a single-user journey in GA4, you must use the same GA4 property.
- You must send user_id with every event to GA4 (when user_id is available).
- You should set the user_id field in the GA4 config tag.
How to Implement User IDs on GA4?
1. Asking a developer to push the User ID to the Data Layer:
If your website has an authentication feature (login), you can ask a developer to push the User ID to the DataLayer.
The code which the developer can push is:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event' : 'login',
'userId' : '1234567' //this number must be replaced with an actual User ID
})
If you’re working on a regular website, it’s preferred to have this code placed above the GTM container (that way, it’ll be available even with the All Pages GTM trigger).
If you’re working on a SPA (Single Page Application), then the location of this code does not matter. Also, the ‘event’ parameter is not required.
Another important thing would be to ask a developer to push the User ID every time a new page loads (when the page entirely refreshes and the previous values in the Data Layer are wiped out).
For Testing:
You must test the userID pushed in the dataLayer in GTM preview mode.
Once you log in or sign up, you should see this in GTM preview mode.
Even though the User ID is in the Data Layer, we cannot simply use it in our tags or triggers without turning that information into a GTM Variable. That’s why creating a Data Layer Variable for that User ID is necessary.
Note: Please remember the DataLayer variable name should be the same as what is written in the code.
2. Insert the User ID variable in the Google Tag:
We must send the user_id parameter in the Google Tag to implement User ID tracking.
Open your Google Tag, click Configuration settings > Add parameter, and enter user_id. The value of this parameter must be some variable that returns the actual user ID of a user who logs in on your website.
3. Adding User ID in the Configuration Parameters of GA4:
Since UserID is a user property value, you must add it to the User Properties in the Google Tag.
4. Creating Custom Dimension In GA4:
After adding it to the Configuration Parameters and User Properties of Google Tag, we must create a Custom Dimension in GA4. When the UserID value is passed to GA4, GA4 needs to recognize it. As we have created a separate data layer variable in GTM, which is a user property, a custom dimension needs to be created in GA4.
Note: Please check that the user property name you enter in GA4 is the same as the one you gave in the GTM Google Tag.
Configuring User ID as the custom dimension in GA4 helps record the User ID, which we can add to the reports. GA4 offers a User ID report with a default user ID but has some limitations. If you need to see accurate data by combining different dimensions and metrics with the User ID, create a custom dimension in GA4.
Note: Recently, some methods have been introduced without creating Custom dimensions in GA4. However, as GA4 is in beta mode, some methods work, and some don’t, in my experience. You can pick the one that works best for you, according to your requirements!
Conclusion
Incorporating User ID into Google Analytics 4 through Google Tag Manager is a crucial advancement for enhancing user identification and authentication. This approach not only facilitates more detailed and accurate tracking of user behavior across various devices and sessions, utilizing identifiers like the user token number and user code, but also strengthens data privacy and security. By following the best practices for implementation, businesses can leverage GA4’s capabilities to refine their marketing strategies and make more informed, data-driven decisions, ensuring robust data protection and optimized user insights.