How to setup User ID in Google Analytics 4 and Universal Analytics

Last Updated: December 28, 2023

What is a User ID in GA4?

GA4 User ID is a unique set of alphanumeric characters (like GdffTeeD453589) assigned to a user so that they can be identified across devices/ browsers and over the course of multiple sessions.

User-ID is one of the methods used by Google to identify users on your website. 

Following are the other methods used by Google:

  1. Google Signals (uses data from those website users who have signed in to one of their Google accounts and have turned on ad personalization). 
  2. Device ID (uses the client ID for websites or the app Instance ID for mobile apps). 
  3. Data Modeling (estimates users’ activity when identifiers such as cookies or User ID aren’t fully available).

The User-ID feature in GA4 allows you to track user activity across different sessions and devices by linking your own unique identifiers to each user. 

It is important to remember that each user ID is considered a distinct user in GA4.

To implement the User ID feature, you need to create and consistently use a unique ID for each user, typically assigned during login (like Login ID). The ID should be less than 256 characters. 

It’s important to comply with Google Analytics Terms of Service while using user IDs. 

This includes not using any personally identifiable information in the IDs and informing users about the use of these identifiers in your Privacy Policy.

Ensure that the user ID doesn’t include details that can reveal the user’s identity to others.

Difference between Google Analytics User ID and Client ID

Through User ID, you can create and assign your own unique IDs to track unique users instead of using the Client IDs (generated by the Google Analytics tracking coding).

A Client ID represents a unique device/browser. Whereas a User ID represents a unique user that may interact with your website using multiple devices/browsers.

It is important that you understand the difference between Client ID and User ID and do not get confused. They are not the same thing.

To learn more about the difference between Client ID and User ID, read this article: Understanding the difference between Client ID and User ID

Advantages of using User ID in GA4

The following are the key advantages of using User ID in GA4:

#1 The usage of the user ID feature makes cross-device measurement possible in GA4 for the logged-in users. Which in turn helps to fix cross-device attribution issues. This is the biggest benefit of using user IDs.

#2 Through the User ID feature, GA4 can more accurately identify the number of unique users and their engagement activities (click on links, page navigation, screen navigation, etc) across multiple devices and/or sessions. Unlike Client ID, the User ID can exist on multiple devices.

If a user is accessing your website from different devices/browsers, then without User ID implementation, a unique user can be counted multiple times by GA.

For example,

If a user logs in to your website via smartphone and then later makes a purchase via a company laptop, then in GA without User ID implementation, the number of unique users could be counted as two. 

However, after User ID implementation, the number of unique users would be counted as one. This is because the same logged-in user can then be uniquely identified across devices.

#3 You can attribute user engagement spread across multiple devices and sessions to a single user in your GA reports. Thus, User ID helps in fixing attribution modelling issues.

#4 Through the User ID feature, you can get a holistic view of the users’ purchase journey.

#5 User ID is a great feature to segment and analyze the logged-in users and compare logged-in with logged-out users, as these users generally engage with your website very differently than those who are not logged in. 

Limitations of User ID in GA4

When using the User-ID feature, keep in mind the following limitations:

#1 The  User-ID feature is not useful for you if users can not login to your website and/or mobile app.

#2 The User-ID feature must be used in accordance with the Analytics SDK / User-ID Feature Policy.

#3 The user IDs sent to GA4 should not exceed 256 characters in length.

#4 The data collected in your GA4 property before implementing User-ID will not be reprocessed or linked to a user ID.

#5 User-ID data collected in one property cannot be shared with or transferred to other properties.

#6 When user IDs are collected, and your GA4 property is connected to BigQuery, the data is automatically sent to BigQuery, regardless of user consent.

Note: Do not register User ID as a custom dimension as User ID custom dimension can easily become a high cardinality dimension and cause data to aggregate under the (other) row.

What can you use as a User ID?

Google Analytics cannot generate unique IDs for you that can be used as User IDs. You also can not use Client IDs as User IDs.

It doesn’t work that way.

In order to implement the User ID feature, you need to generate your own unique IDs and assign IDs to new and returning users through your user authentication system. 

This user authentication system is usually your website login, the system through which users can log in and log out.

The unique ID that you use to identify a logged-in user (also known as login ID) on your website can be sent as a User ID to your GA account.

However, the login ID should not contain personally identifiable information (PII) like name, email address, etc.

If your login ID contains PII, you may need to either remove that information from the Login IDs or completely rewrite the Login IDs before you send the Login IDs as User IDs to GA4.

This is because the User ID usage policy requires that the User ID should not be a PII of a user.

If you do not follow this usage policy, then Google may terminate your GA account.

In order to implement User ID, you would need to write code that can pull login IDs or other unique IDs from your authentication system and then place them in your Google Analytics tracking code.

That way, the User IDs can be sent to the GA server along with the event/hit data. 

Consequently, any website which assigns login IDs to its users is a good candidate for implementing the User ID feature.

How to send user IDs to GA4

Follow the steps below to set up User IDs in GA4:

Step-1: Ask your developer to hard code the following data layer on the website (above the GTM container code), which fires the ‘login’ event and pushes the Login ID as the user ID to the data layer once a user successfully logs in. 

data layer code for user id ga4

Note(1): Do not push default values like ‘none’ or ‘null’ for the user ID in GA4. If you include default values, GA4 will count them as unique users, inflating your user count and skewing your data. 

Note(2): Hardcode the data layer code above the GTM container code so the data layer is available even when you use the ‘All pages’ GTM trigger. For single page apps, the location of the data layer code does not matter.

Step-2: Ensure the developer correctly pushes the User ID into the dataLayer upon successful login via ‘GTM preview and debug’ mode. The user ID should be sent only when the user logs in. 

  1. Make sure you are logged out of your website.
  2. Navigate to Google Tag Manager.
  3. Click on the ‘Preview’ button.
  4. Enter your website URL and click on the ‘Connect’ button.
  5. Click on the ‘Continue’ button.
  6. Navigate to your website and refresh it.
  7. Login to your website.
  8. You should now see a ‘login’ event under the ‘Summary’ section.
  9. Click on the ‘login’ event and search for the ‘user id’ parameter in the data layer push.

Step-3: Navigate to your GTM container and create a new data layer variable which fetches the user ID from the data layer hardcoded on your website. This step is required to use ‘user id’ in GTM tags and/or triggers.

  1. Open Google Tag Manager and select “Variables”.
  2. In the “User-Defined Variables” section, click “New”.
  3. Choose “Variable Configuration” and then “Data Layer Variable”.
  4. Name the variable at the top.
  5. In the “Data Layer Variable Name” field, enter the user ID parameter that you pushed into the data layer (like “user_id”).
  6. Click “Save”.
user id data layer variable

Step-4: Update your Google Tag:

  1. In Tag Manager, go to “Tags”.
  2. Choose your Google tag.
  3. Under “Configuration settings”, add a new row with these details:
  4. Event Parameter: user_id
  5. Value: Choose the Data Layer variable you just created.
  6. Click “Save”.
  7. Click “Submit” to apply your changes.
google tag user id

Step-5: Create/update the ‘Event Settings’ variable:

  1. In the tag manager, go to ‘Variables’.
  2. Scroll down to the section ‘User-Defined Variables’.
  3. Click on the ‘New’ button.
  4. Choose the variable type ‘Google Tag: Event Settings’.
  5. Click on the ‘Add parameter’ button.
  6. Event Parameter: user_id
  7. Value: Choose the Data Layer variable you just created.
  8. Click “Save”.
the ‘Event Settings variable

Use the data layer variable you created in the previous step as the value of the ‘user_id’ configuration parameter.

Step-5: Update all your GA4 Event tags one by one to add the ‘user_id’ configuration parameter to them: 

  1. Edit a GA4 event tag.
  2. Click on the “Event Parameters” drop-down menu.
  3. Select the event settings variable from the “Event Settings Variable” drop-down menu.
  4. Click “Save”.
the Event Settings Variable drop down menu

Sometimes, the user ID is not available to an event even when it is available to Google Tag. So, just to be on the safe side, we also pass the user ID with every event. 

Step-6: Refresh the GTM preview mode, navigate to Google Tag (under the ‘Tags fired’ section) and then check for the ‘user_id’ parameter with a certain value. 

Step-7: Navigate to the GA4 debugView report and check one or more GA4 event tags to confirm they also send the ‘user_id’ parameter. The user ID should appear in the debugView report. 

Step-8: If everything works correctly, publish the changes in your container. 

Step-9: Set up the reporting identity of your GA4 property with the User-ID option:

  1. Go to the Admin section of your property.
  2. Under the “Data display” category, click on “Reporting Identity”.
  3. Choose one of the following options:
    • Blended: This option uses user ID, Google signals, device ID, and modelled data for evaluation.
    • Observed: This uses user ID, Google signals, and device ID for evaluation.

Note(1): In GA4, when you include User ID in your reporting identity, it enables the de-duplication of user IDs across all reports. This means that each user is counted only once, regardless of the number of sessions or devices they use. This feature provides a more accurate and unified view of user interactions across different platforms and devices, enhancing the quality and reliability of the data in your reports.

Note(2): Starting February 12, 2024, Google signals will no longer be part of the reporting identity. This update will affect all GA4 properties but will only change reporting features.

Step-10: After 24 to 48 hours have elapsed, navigate to the ‘user explorer’ report, where you should be able to see the user id data.

Here is how you can filter out User ID data in GA4.

GA4 uses a dimension called ‘Effective User ID’.

effective user id

This dimension reports on the User ID if one is available. Otherwise, it reports on the Device ID.

The Device ID is either the app instance ID for a mobile app or the client ID for a website.

So, when you look at the values of this dimension, you can see both the user IDs and device IDs.

GA4 does not allow you to use the ‘Effective User ID’ dimension in the dimension filter.

So you will have to use another dimension called ‘Signed in with user ID‘ while creating the reporting filter in the ‘user explorer’ report.

This dimension returns ‘yes’ if a user signed in with a user ID.

Use this dimension to filter out all the data related to user IDs.

What can you do with User IDs in GA4?

Using User-ID, you can:

#1 Compare signed-in and non-signed-in users:

  • Build a comparison using the “Signed in with user ID” dimension.
  • Set the dimension value to “yes” to differentiate between users who are signed in and those who are not.
signed in with user id
signed in with user id ga4

#2 User Exploration:

You can view detailed information about individual users, including acquisition details, summary metrics, and a timeline of their activities on your website or app.

#3 DebugView:

Monitor user properties in real-time, troubleshoot issues, and understand user behaviour on your website or app.

#3 Create Remarketing Audiences Based on User-ID Data:

  • You can create audiences for remarketing purposes using user ID data.
  • If your GA4 and Google Ads accounts are linked, these audiences will be available in your Google Ads shared library.

#4 GA4 BigQuery Export:

GA4 BigQuery Export includes both Client ID (as user_pseudo_id) and User ID (as user_id) in its schema, allowing for detailed user-level analysis.

You can use BigQuery Export to combine your GA4 data with first-party data (like CRM data) for measuring offline conversions.

Additional Points:

  1. When using User-ID, GA4 only includes the user identifier and the device identifier for the last device used by each logged-in user.
  2. Without User-ID, GA4 includes all device and user identifiers in the audience information sent to Google Ads.

How GA4 handles sessions when the User ID feature is turned on.

GA4 manages sessions where User-ID isn’t immediately available in the following way:

#1 Before a User Signs In:

  1. If a user interacts with your website or mobile app before signing in, GA4 initially tracks these actions using a session ID.
  2. Once the user signs in, GA4 links the session (including previous events) to the user ID provided at sign-in.

#2 After a User Signs Out:

When a user signs out, GA4 stops linking any new events to their user ID.

For example, consider the following scenario:

  1. A user visits your website and triggers two events (Event 1 and Event 2) without a user ID.
  2. The user then signs in and triggers another event (Event 3).
  3. At this point, all three events (Events 1, 2, and 3) are connected to the user’s ID.
  4. If the user signs out and triggers another event (Event 4), this new event won’t be associated with the user ID. However, Events 1, 2, and 3 will still be linked to that user.

Implementing User ID without a login

You can implement User ID features in many ways other than through a login system

For example, Walt Disney World tracks offline customers through a special wristband known as the ‘magic band’.

This colourful, harmless, innocent-looking band, which is used to enter the parks, check in to Disney hotels, and buy food and merchandise, is actually a sort of tracking device (in my opinion) through which the company collects the browsing and buying behaviour of their offline customers.

Then through measurement protocol and User ID feature, this offline customer data is connected with the online data to get a holistic view of the customers’ journey.

Similarly,

If someone uses some type of loyalty card for making a purchase in a supermarket, then that loyalty card can be scanned to assign a unique ID to the user which can later be sent to GA via measurement protocol and User ID.

Other users’ authentication systems widely used are:

  1. Biometric identification (like retinal scan, fingerprint scan, iris recognition, voice recognition, signatures, etc.).
  2. Punching cards to record attendance (like attendance of employees).
  3. Wristbands (like the one used by Disney).
  4. Retail loyalty cards (used by many supermarket chains).
  5. Online and offline Coupons.

User IDs can be set for all of these authentication systems as well with the help of measurement protocol, but the implementation is not going to be easy.

Once the User ID feature is implemented, GA combines and reports all the hits with the same User ID, regardless of which device the hits come from. This makes cross-device measurement possible in GA.

You can send User ID to Universal Analytics by creating a new custom dimension (with user scope):

ga(‘set’, ‘dimension1’,userId);

Note: You can implement the User ID feature for both websites and mobile applications.

User ID hits and sessions in Universal Analytics.

In order to understand how User ID works, you need to get familiar with the User ID related terminologies that are frequently used:

1. User ID hits – these are those GA hits (pageviews, screenviews, events, transactions, etc) for which a User ID has been set.

2. Non-User ID hits – these are those GA hits for which a User ID has not been set.

Note(1): User ID hits are processed separately from non-User ID hits.

Note(2): If you are not using the User ID feature then the non-User ID hits are simply known as hits.

3. User ID session – It is the Google Analytics session in which a User ID is assigned to a user. User ID session is a collection of User ID hits but may also include non-User ID hits provided the session unification setting (more about it later) has been turned ON.

4. Non-user ID session – it is a collection of non-user ID hits. If you are not using the user ID feature then the non-user ID sessions are simply known as GA sessions.

Note: A standard session (non-user ID session) turns into a user ID session the moment a user ID is assigned to a user. This can happen when a user login in the middle of a standard GA session.

User ID Property (Universal Analytics)

User ID property is that Google Analytics property in which user ID setting has been turned on:

ga user id user id turned on

User ID View (Universal Analytics)

User ID view is the GA view which collects only the data related to user ID sessions:

ga user id User ID reports enabled

You know you are looking at the User id view when you see the setting ‘User ID Reports‘ enabled under ‘View Settings’.

You should consider adding the words ‘User ID’ somewhere in your view name for easy reference.

In the case of User ID views, GA calculates unique users by counting the number of unique User IDs assigned to the website visitors instead of counting the number of unique Client IDs assigned to the website visitors.

Non-User ID View (Universal Analytics)

Non-User ID view is the GA view that collects data related to standard GA sessions.

It does not recognise User ID hits. In the case of non-User ID views (regular views), GA calculates unique users by counting the number of unique Client IDs assigned to the website visitors.

You know you are looking at the non-User ID view when you see the setting ‘User ID Reports‘ disabled under ‘View Settings’:

ga user id User ID reports disabled

If you are not using the User ID feature then the non-User ID views are simply known as views.

The number of unique users reported by non-User ID views is usually more than the number of unique users reported by User ID views. This is because User ID views only report on the users which triggered the User ID sessions.

Revenue in a User ID view is based on users and not sessions.

When does a regular GA session turn into a User ID Session? (Universal Analytics)

If you are using login IDs as User IDs then as soon as a user logs in, a unique ID (login ID) is assigned to the user which is then sent as a User ID to GA.

So once the user login, a regular GA session turns into a User ID session and the User ID is set for all subsequent hits.

By default, In Google Analytics, collected hits can be associated with a User ID only in a User ID session and that too before and during the user’s login but not after the user has logout.

So, if the user doesn’t log in then the User ID is not assigned to him and no hits are associated with the User ID.

Similarly, when the user logs out, the User ID is no longer assigned to him and the new collected hits are not associated with the User ID.

Limitations of User ID (Universal Analytics)

Through the User ID feature, you cannot track users and their activity from the very first time they visited your website and before they decided to sign up and log in. Thus, the User ID feature works well only for logged in users.

Now not all the users who visit your website login.

So you may not be able to track a large portion of the users’ conversion journey.

In order to overcome this limitation, you can keep your users logged in unless they explicitly logged out.

For example, Facebook keeps its users logged in all the time unless they explicitly logged out. In this way, Facebook is able to collect most of the cross-device behaviour data about its users.

Here are a few more User ID limitations:

  • Assigning User ID: Google Analytics on its own cannot generate or assign a User ID. This is generally done by the authentication system you use, Once the user signs up, or logs in, to your website, the User ID is passed to Google Analytics.
  • Product integration: You can connect Google Ads or Search Ads 360 with the User ID view, but you cannot integrate (connect) User ID view with Display & Video 360, Campaign Manager 360 and Google Ad Manager.
  • Feature limitations: A few Google Analytics features are not available in the User ID view:
    • Real-time reporting: Google Analytics does not show real-time data for User ID view. Data in the User ID view comes from the identified sessions. To protect individual user data (PII regulations) real-time view is not supported in User ID views.
    • Demographics and interest reports: The PII regulations also apply to demographics and interest reports which combine gender, age, and interest details of the users. Hence you won’t see any demographics and interest reports in the User ID view.
  • Blocking extensions – Adblockers and script blockers: If any of the website users are using the blocking extensions which block Google Analytics script and Google Tag Manager script and prohibit sending data to Google Analytics, this can affect the data that you collect including the User ID feature.

Overcoming the limitations of User ID through session stitching (Universal Analytics)

You can associate Client IDs with User IDs to track users’ activities before they decided to sign-up and login through a method known as Session unification (or session stitching).

Thus, to an extent, you can overcome the limitations of the User ID.

If a person visits your website without logging in, then a unique Client ID is assigned to him. Now if the same person later logged in, then a unique User ID is assigned to him.

By associating the Client ID with the User ID, you can determine what the logged-in person did on your website before he signed up and/or logged in. In this way, you can get a better picture of the user’s purchase journey.

However:

‘Session Unification’ won’t solve all your attribution issues.

Associating Client ID with User ID works well only when a person is visiting and logging in to your website via the same device and browser and he is the only person using that device and browser.

If multiple users are using the same device and browser (quite common in the case of cyber cafes or office computers) then there is no guarantee that the person who browsed your website without logging in, is the same person who later visited your website after login.

Similarly, a person can visit your website via multiple devices and browsers before logging in. In that case, he would be assigned multiple Client IDs but only one User ID. Session unification associates only one Client ID with the User ID.

How to turn off session unification (Universal Analytics).

Session unification is a User ID setting that can be turned ON and OFF.

Session unification is turned ON by default and it should be kept turned on unless your company’s privacy policy issue prohibits you from collecting such data.

Since the session unification setting is turned ON by default, all of the hits collected before the login and during the login, in a User ID session are automatically associated with the User ID.

If the session unification setting is turned off then only the hits collected during the login, in a User ID session can be associated with User ID. The hits collected before the login in the User ID session cannot be associated with the User ID then.

To turn off the session unification follow the steps below:

Step-1: Navigate to the ‘Admin’ section of your User ID view.

Step-2: Click on the ‘User ID’ link under the ‘Tracking Info’ drop-down menu:

ga user id admin section

Step-3: Click on the ‘Edit’ link next to the step: ‘Set up the User ID’:

ga user id edit user id

Step-4: Scroll down a bit and then set the ‘Session Unification’ toggle switch to OFF:

ga user id session unification turned off

Note(1): Turning OFF the session unification setting is not recommended.

Note(2): Session unification is only available for User ID enabled properties.

How session unification works (Universal Analytics).

Let’s take an example of how session unification works. The below image explains how session unification works when it is turned off.

ga user id Session Unification Turned off activity

Session 1: A user lands on your website and generates hits (events or pageviews). But the user is not signed in and hence no data will be assigned to any User ID

Session 2: A user lands on your website and generates hits (events or pageviews) and after this, the user signs in (specific User id is assigned) and generate more hits. In this complete session Hit 1 and Hit 2 will not be assigned with User ID if the session unification is turned “Off”. While Hit 3, Hit 4 and Hit 5 will be assigned to a User ID.

Session 3: In this session, the user is signed in for the complete session and hence all hits (events and page views) will be assigned to the User ID.

Session 4: In Session 4, the user is signed in when the session started, then he generates Hit 1 and Hit 2 and after that, he signs out. The user continues browsing and generates Hit 3, Hit 4, and Hit 5. In this session only Hit 1 and Hit 2 will be associated with the User ID (Since the user was signed in) but Hit 3, Hit 4, and Hit 5 will not be associated with User ID even they are recorded in the same session.

Session 5: In the complete session, the user is not signed in and hence all hits in this session will not be assigned with a User ID

Session 6: In this session, the user is signed in for the complete session and hence all hits (events and page views) will be assigned to the User ID.

Now let’s take another example of how session unification works when it is turned ON:

ga user id Session Unification Turned On activity

Session 1: A user lands on your website and generates hits (events or pageviews). But the user is not signed in and hence no data will be assigned to any User ID

Session 2: A user lands on your website and generates hits (events or pageviews) and after this, the user signs in (a specific User id is assigned) and generate more hits. In this complete session, all Hits will be assigned with User ID if the session unification is turned “On”.

Session 3: In this session, the user is signed in for the complete session and hence all hits (events and page views) will be assigned to the User ID.

Session 4: In session 4, the user is signed in when the session started, then the user generates Hit 1 and Hit 2 and after that, he signs out. The user continues browsing and generates Hit 3, Hit 4, and Hit 5. In this session only Hit 1 and Hit 2 will be associated with the User ID (Since the user was signed in) but Hit 3, Hit 4, and Hit 5 will not be associated with a User ID, even though they are recorded in the same session.

Session 5: In the complete session, the user is not signed in and hence all hits in this session will not be assigned with a User ID

Session 6: In this session, the user is signed in for the complete session and hence all hits (events and page views) will be assigned to the User ID.

Setting up the User ID (Universal Analytics)

The user ID feature is available only in universal analytics and is set at the property level. The implementation of User ID depends upon your environment/platform.

Setting up the User ID includes:

  1. Creating a new User ID view
  2. Generating unique IDs and then assigning them to website users.
  3. Sending IDs and all related engagement data to GA

How to create a new User ID view in Universal Analytics

Follow the steps below to create a new User ID view in Google Analytics:

Step-1: Navigate to the Admin section of your GA Property and then click on the ‘User ID’ link under the ‘tracking info’ drop-down menu:

ga user id admin section

Step-2: Read and agree to the User ID policy by toggling the blue switch to ‘ON’ and then click on the ‘Next Step’ button.

ga user id user id policy

Step-3: Keep the ‘Session Unification’ setting intact (i.e. switched ON) and click on the ‘Next Step’ button:

ga user id session unification turned on settings

Step-4: Click on the ‘Create’ button to create a new User ID view:

ga user id create button

Note: The User ID data will appear only in the User ID view.

Step-5: Select your app (website or mobile app) for the User ID view:

ga user id website or mobile app

Step-6: Name your new reporting view and select your ‘reporting time zone’:

ga user id View Name and time zone

Tip: Include the word ‘User ID’ somewhere in the reporting view name. This way, you can easily differentiate this view from regular views.

Step-7: Toggle ‘On’ the ‘Show User ID Reports’ button and then click on the ‘Create View’ button.

ga user id create view

The process of creating the new User ID view is now complete.

Important points about the User ID view (Universal Analytics).

#1 Make sure that, whenever you create a new User ID view, you include the words ‘User ID’ somewhere in your view name. This way, you can easily identify this reporting view later.

#2 You can create multiple User ID views on a User ID enabled property. The process is similar to creating multiple non-User ID views. Just turn the ‘User ID view’ toggle to ‘ON’.

#3 A User ID view collects and reports on only User ID sessions related data. So in order to get a complete picture of customer journeys, you need to use this view along with the non-User ID views.

#4 You will see data in your User ID view reports only after you have successfully implemented the User ID feature. Otherwise, your User ID view reports will remain empty. You won’t see any data in them, not even in the real-time reports.

#5 Cross-device reports are available in the User ID views.

#6 Any existing non-User ID view cannot be converted to a User ID view and vice versa. Therefore to collect User ID data, you have to create a new User ID view.

#7 User ID view collects data only after the User ID feature has been implemented. It can’t collect data retroactively.

#8 Once you have enabled the User ID feature for a view, you cannot disable it.

How to generate, assign and send User IDs to Universal Analytics using analytics.js.

Add the following line of code to your Google Analytics tracking code, on each web page of your website before any hit data is sent:

ga(‘set’, ‘userID’, {{USER_ID}});

So your Google Analytics tracking code may look something like the one below:

<script>

(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-1234567-34’, ‘auto’);

ga(‘set’, ‘userID’, {{USER_ID}}); 

ga(‘send’, ‘pageview’);

</script>

Note: Do not copy-paste this code on your website. It won’t work. You would need to first modify this tracking code.

Here:

{{USER_ID}} is a server variable (of type string) that pulls the unique ID generated by your authentication system.

You or your developer needs to write a code through which login IDs can be pulled from your authentication system and sent as User IDs to GA. 

Your final Google Analytics tracking code after User ID implementation is also going to include some server-side script.

For example, if you use PHP as the backend script then your final Google Analytics tracking code may look like the one below:

ga user id analytics code

Ask your developer to write a code through which login IDs can be pulled from your authentication system and sent as User IDs to Google Analytics.

There are two points, you need to keep in mind before setting up the User ID:

#1 The User ID must be set after the tracking object has been created

#2 The User ID must be set before you send any hit data to Google Analytics

1. The User ID must be set after the tracking object has been created

The User ID must be set after the tracking object has been created. otherwise, the User ID will not be associated with your web property and won’t work.

The following User ID set up will not work:

ga(‘set’, ‘userId’, {{USER_ID}}); // User ID is set before the tracking object

ga(‘create’, ‘UA-1234567-34’, ‘auto’); // create a tracking object

ga(‘send’, ‘pageview’);

The correct User ID set up would be:

ga(‘create’, ‘UA-1234567-34’, ‘auto’); // create a tracking object

ga(‘set’, ‘userId’, {{USER_ID}}); // User ID is set after the tracking object

ga(‘send’, ‘pageview’);

Note: The User ID must be set on all the pages of your website otherwise you may not get accurate user data.

2. The User ID must be set before you send any hit data to Google Analytics

The User ID must be set before you send any hit data to GA. In this way, the User ID is set for all subsequent hits that occur on a page.

So following User ID set up is not recommended:

ga(‘create’, ‘UA-123456-34’, ‘auto’);

ga(‘send’, ‘pageview’);

ga(‘set’, ‘userId’, {{USER_ID}}); // here User ID is set after the pageview hit

The correct set up would look like the one below:

ga(‘create’, ‘UA-1234567-34’, ‘auto’);

ga(‘set’, ‘userId’, {{USER_ID}}); // here User ID is set before the pageview hit

ga(‘send’, ‘pageview’);

Similarly, the following User ID set up is not recommended:

ga(‘create’, ‘UA-1234567-34’, ‘auto’);

setTimeout(“ga(‘send’,’event’,’Profitable Engagement’,’time on page more than 3 minutes’)”,180000);

ga(‘set’, ‘userId’, {{USER_ID}}); // here User ID is set after the event hit

How to send User IDs to Universal Analytics using gtag.js

To set User ID using Global Site Tag (gtag.js), you can use below command with the tracking code.

gtag(‘config’, ‘UA-XXXX-YY’, {

  ‘user_id’: ‘USER_ID’

});

Your final code may look like below

          <!– Global site tag (gtag.js) – Google Analytics –>

<script async src=”https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID”></script>

<script>

           window.dataLayer = window.dataLayer || [];

           function gtag(){dataLayer.push(arguments);}

          gtag(‘js’, new Date());

          gtag(‘config’,’UA-XXXX-YY’);

          ‘user_id’: ‘USER_ID’

</script>

User ID coverage report (Universal Analytics).

Through the User ID coverage report, you can determine the percentage of sessions that include activity from logged in users (provided your authentication system is login).

Strangely, you can’t access the User ID Coverage report through the User ID view.

This report is available in any non-User ID view which belongs to a GA property for which the User ID is enabled. You can find this report under Audience > Behavior > User ID Coverage:

ga user id user id covrage

This report shows the User ID status, which can be:

  • Unassigned – the number/percentage of sessions in which users weren’t assigned a User ID.
  • Assigned – the number/percentage of sessions in which users were assigned User ID.
ga user id assigned or unassigned

Note: Use the User ID Coverage report in conjunction with User ID view reports.

Cross-device reports in Universal Analytics.

Once you have set up User ID in your Google Analytics property, you can then access and use the cross-device reports via the User ID view.

ga user id device overlap

Through cross-device reports, you can get a better understanding of how users engage and convert on your website across different devices and over the course of multiple sessions. The sample report looks like below:

ga user id cross device report

You also get the total number of users using multiple devices:

ga user id cross device report numbers

To learn more about User ID cross-device reports, read this article: Google Analytics User ID Reports – Device Overlap, Device Paths, Acquisition Device

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade

Learn and Master Google Analytics 4 (GA4) - 140 pages ebook

X