21 Jul 2022

4 min read

How to send User ID to Google Analytics 4?

How to send User ID to Google Analytics 4?

Using unique identifiers for your users is the best way to segment and create audiences for remarketing on your website or app.

Using a user ID is very useful both for measuring activity across platforms, especially when linking with BigQuery, and for tracking the consent status of your users.

In this post, you will learn how to generate and send your user ID to Google Analytics 4, for creating user behavior reports**.**

Firstly, you should generate a unique user ID for each visitor.

You can do this directly from Google Tag Manager.

There are multiple ways for you to assign unique user IDs to your users visiting your website. You can do this directly with your backend and database, or generate a basic user ID using Google Tag Manager.

Of course, Google Analytics Client IDs, aka ‘_cid’, can also be used as user IDs, but it would be better to combine them with your customers' IDs in CRM so that at some point you can combine this variable with your own remarketing data.

For example, on a website using a MongoDB database, you can see how user IDs are stored in this way. And with this data structure, you can sync user IDs with your CRM data.

How to send user id to google analytics 4 mongo

And you can simply push these IDs to dataLayer like this:

However, you may not always have the freedom to work directly with your website's database.

So, let's continue to create the basic user ID on Google Tag Manager and send it to Google Analytics 4.

Generating User ID

Create a new custom HTML tag and replace with this JavaScript.

function getUserID() {  var userID = localStorage.getItem('userId');  if (userID) {    return userID;  } else {    var newUserID = Math.floor(Math.random() * 100) + Date.now();    localStorage.setItem('userId', newUserID);    return newUserID;  }}

 This script will create a random user ID and save it directly to the browser's local storage.

How to send user id to google analytics 4 userid local

You can find more details about window.localStorage here.

Note that, if you have a user ID from the database (backend), you can use it directly. This method is an example of how you can send your user ID to Google Analytics 4. If you generate and keep your user ID in local storage, the user can delete or modify it.

Also, you can use the built-in variable called Random Number on Google Tag Manager. But you may want to combine it with other variables when generating the user ID, like a date or specific characters.

Then, you should push the getUserID variable to dataLayer.

window.dataLayer = window.dataLayer || [];dataLayer.push({  'user_id': getUserID() });

Add an All pages trigger and save it. Your final tag should look like this.

How to send user id to google analytics 4 userid push

Create Variable for User ID

Create a new variable called User ID, and have it refer to the user_id variable from the data layer.

How to send user id to google analytics 4 user id variable

Add User Property to GA4 Configuration

To send User ID variable to Google Analytics 4, you should add this {{User ID}} variable to User Properties.

Note that, if you add a field directly, you can not set a custom definition in Google Analytics 4 UI.

How to send user id to google analytics 4 user id define

Save this tag and click on Preview for testing our script. You should see the User ID on Window Loaded.

How to send user id to google analytics 4 user id test gtm

Add Custom Definitions in Google Analytics 4 UI

Let's create custom definitions for that User ID variable.

Go to your GA4 account, and click Configure > Custom definitions on the left sidebar.

Click Create custom dimensions and add the user ID definition.

Scope should be User, and user property should be user_ID, which is configured before on GTM.

How to send user id to google analytics 4 custom definitions user id

Save it and go to DebugView for testing.

Visit your website and check your User ID definitions in real-time.

How to send user id to google analytics 4 user id debugview

Congrats, now you can use this custom User-scoped definition in your reports and analyses.

How to send user id to google analytics 4 user id user scoped

Resources

Related service

Make the measurement answer a real decision

Our web analytics work connects strategy, collection, reporting, and analysis so your team can review the evidence behind a decision.
Explore web analytics
Berkay Tayfun Demirbaş
Berkay Tayfun Demirbaş