April 3, 2017

Does your website have a login system? Setting up Google Analytics (GA) userId tracking with Google Tag Manager (GTM) is a pretty simple thing you can do to track logged-in users across browsers and devices.

I was recently asked to setup userId tracking connected to a login system. After spending some time exploring the Google documentation and a couple of third-party how-to’s I found much of the information to be overkill. Here I’ll explain the 4 basic steps you need in order to get GA userId tracking setup up on your site.

Step 1

Enable userId tracking in Google Analyics

Navigate to the settings page for your Google Analytics tracker and review Google’s User-ID Policy to enable UserId tracking. After enabling userId tracking you’ll be prompted to create a new User-ID view for you GA property. The new view your create here will only be used to display data for logged-in users.

Step 2

Create a new Variable in GTM to retrieve the logged in userId

There are many ways you can retrieve a variable in GTM, in this example we’ll retrieve a logged-in user’s userId that was stored in a cookie which was set by your site’s login system. To get started:

  1. Create a new Variable in GTM, name the variable “User ID.
  2. Select “Custom Javascript” as the variable type.
  3. Write a JavaScript function that retrieves the userId variable from your site’s login cookie. The retrieval of your userId variable depends on your site’s particular setup so you’ll need to write you own function here to retrieve this data. Important: the end result of your function should return just the identifier that you want to pass to google analytics as the userId.

Note: Google’s terms of service prohibit using personally identifiable information as a userId, don’t use names and email addresses.

Step 3

Create a new Tag in GTM that will fire on each page view, setting the userId

  1. Create a new Tag, name it “userID Tag“.
  2. Select the GA Tracking ID for the profile you enabled User-ID tracking for in step 1.
  3. Under more settings, set the Field Name to “userId“. Set the value to “{{user id}}“.
  4. Set the triggering to All Pages.


Step 4

Check your work in preview mode

Put your GTM container in preview mode to verify that your tag is firing and that the userId is getting passed.

If your userId is being captured, you’re ready to publish your work! If not, check your JavaScript function in step 2.


You can now view your userId Tracking view in Google Analytics. Note: data in this view updates every 10-15 minutes.