Send user IDs

This article describes how to send user IDs from your website or app once you've generated the user IDs. To learn more about the User-ID feature, see Measure activity across platforms.

Before you begin

gtag.js

Add the global site tag to your website and make sure you can access Analytics and the website source code.

Tag Manager

Add the Google Analytics: GA4 Configuration tag to your website and make sure you can access Analytics and the website source code.

Send user IDs

gtag.js

To send a user ID to Analytics, add the user_id parameter to the config command on each page where you include your Analytics tag. Using the following snippet, replace G-XXXXXXXX with your measurement ID and USER_ID with the generated user ID.

gtag('config', 'MEASUREMENT_ID', {
'user_id': 'USER_ID' 
});

Tag Manager

Before you send user IDs to Analytics, create a Google Analytics 4 Configuration tag and add the generated user ID to your data layer as follows:

dataLayer.push({
'user_id': 'USER_ID'
});

First, create a Data Layer variable for the user ID:

  1. In Google Tag Manager, click Variables.
  2. In the User-Defined Variables section, click New.
  3. Click Variable Configuration > Data Layer Variable.
  4. Enter a name for the variable at the top.
  5. In the Data Layer Variable Name field, enter a name for your user ID variable (such as "user_id").
  6. Click Save.

Second, modify the Configuration tag so all events associated with the tag use the Data Layer variable:

  1. In Google Tag Manager, click Tags.
  2. Select your Google Analytics 4 Configuration tag.
  3. In the Tag Configuration section, click Fields to Set.
  4. Click Add Row.
  5. In the Field Name field, enter "user_id" so Analytics recognizes the user ID.
  6. In the Value field, select the Data Layer variable you created.
  7. Click Save.
  8. Click Submit to publish the changes.