Google Analytics – How to capture Client ID for reporting purposes

  • Ethereum
  • MetaMask
Scan to Donate Ethereum to 0xe95BF07c484dcC33Af16a57A7a1cB725906bc57D

Donate Ethereum to this address

If you find the blog useful and would like to show some support, a little donation of Ethereum would help to keep this site alive. Appreciate it!

Donate With MetaMask

Donate ETH Via PAY With Meta Mask

Google Analytics is mainly used for capturing web data, aggregating them and displaying them in charts and graphs.

Having said that, there are times where we want to drill down to individuals.

To do that, you will need unique identifiers to differentiate between users.

1. Google Analytics Client ID

In Google Analytics, there is such an unique identifier called Client ID.

It is a randomly generated string, given to each “new user” to the site.

The Client ID will be stored in browser cookies, so that subsequent visits can be associated to the user.

By default, Client ID is not available for reporting purposes.

If you use a GA debugger, you will find the Client ID as below.

But, in order to use Client ID for reporting, we have to capture it in a separate variable, which is also called a Custom Dimension in Google Analytics.

2. How to capture Client ID

First, create a new Custom Dimension:

  • Login to Google Analytics
  • Go to Admin section as shown below

Once in the Admin section:

  • Look under PROPERTY section for Custom Definitions
  • Expand Custom Definitions
  • Select Custom Dimensions

On the Custom Dimension page:

  • Select + NEW CUSTOM DIMENSION

On the Add Custom Dimension page:

  • Give the custom dimension a name
  • Set Scope to User

By setting Scope to User, it ensures that the value stored in this custom dimension will be persistent.

With the persistency, you will only need to capture Client ID once and the data will be tagged to every user interaction in the subsequent sessions.

Once done, we should see a new entry on the Custom Dimension page.

Take note of the Index value, as it is required for creating the tag later on.

Next, to create a new tag to capture the Client ID, we need to first create a variable to store the GA Tracking Code.

First, we need to find the GA Tracking Code:

  • Under the PROPERTY section, select Property Settings
  • Under Basic Settings, copy the Tracking Id

This Tracking Id will be our GA Tracking Code.

With the GA Tracking Code, we can create a variable to store it:

  • Login to Google Tag Manager
  • Go to Variables section
  • On Variables page, under User-Defined Variables section, select NEW
  • Give the variable a name (e.g. GA – Tracking Code as shown below)
  • Under Choose variable type, go to Utilities section and select Constant
  • On the Variable Configuration page, input the GA Tracking Code in the Value field
  • Select SAVE

Next, create a variable to store the Client ID:

  • Go to Variables section
  • On Variables page, under User-Defined Variables section, select NEW
  • Give the variable a name (e.g. Client ID as shown below)
  • Under Choose variable type, go to Page Variables section and select Custom Javascript

We will need the code below to capture the Client ID generated by Google Analytics:

function() {
   try {
      var trackers = ga.getAll();
      var i, len;
      for (i = 0, len = trackers.length; i < len; i += 1) {
         if (trackers[i].get('trackingId') === {{GA - Tracking Code}}) {
            return trackers[i].get('clientId').toLowerCase().trim();
         }
      }
   } catch(e) {} 
   return 'false';
}
  • On Variable Configuration page, copy and paste the code into the Custom Javascript code box
  • Select SAVE

Before we create the tag, we need to create a trigger first.

A trigger contains the conditions to fire a tag.

In our case, for simplicity sake, we want to capture the Client ID everytime a user lands on any of our website pages.

To create a trigger:

  • Go to Triggers section
  • On Triggers page, select NEW
  • Give the variable a name (e.g. Pageview – Client ID as shown below)
  • Under Choose trigger type, go to Page View section and select Window Loaded

Window Loaded was selected as our trigger type, to ensure that Client ID is properly generated before we try to capture it.

[Optional] Additionally, we can define one more condition to ensure that the tag fires only when the Client ID is available.

Now that we have both the variables and trigger ready, we can create the tag:

  • Go to Tags section
  • On Tags page, select NEW
  • Give the tag a name (e.g. Get Client ID as shown below)
  • Under Choose tag type, go to Featured section and select Universal Analytics

Configure as per what is shown in the screenshot below:

  • Track Type: Event
  • Category: Up to you. We use “misc” as an example
  • Action: Up to you. We use “get id” as an example
  • Label: Up to you. We created another variable to capture the page URL for our example.
  • Non-Interaction Hit: True
    • To exclude this from counting towards user interaction on website, which will artificially reduce bounce rate
  • Enable overriding settings in this tag: Check the box
  • Tracking ID: Click on the icon on the right of the field, select the variable we have created – GA Tracking Code

Expand More Settings section and expand Custom Dimensions section within:

  • Index: Use the Index number of the Custom Dimension we have created earlier in Google Analytics
  • Dimension Value: Click on the icon on the right of the field, select the variable we have created – Client ID

At the very bottom, add a trigger to the tag by selecting Choose a trigger to make this tag fire….

Add the trigger to the tag:

  • On Choose a trigger page, select the trigger created earlier – Pageview – Client ID
  • Select SAVE

Once you see the new tag, submit the changes:

  • Select SUBMIT

Lastly, verify that the new tag is working.

We can see that dimension1 is capturing exactly the same value as clientId.

Congratulation! You can now carry out data analysis based on individual users.

About Zenny Tan Zhong Ming 32 Articles
Let's connect on LinkedIn ( https://www.linkedin.com/in/zenny-tan-zhong-ming ).

12 Comments

  1. Hi Zenny,

    Great article – very insightful indeed. I wonder, once the client ID has been added as a custom dimension, what does the reporting function look like for it?

    Can it be filtered by individual client ID and if so does it include any extra fields in addition to sessions, avg. session duration, bounce rate, revenue, transactions and goal conversion rate?

    Harry

    • Hi Harry! Apologies for not seeing your comment here and thanks for reaching out to me on LinkedIn! Hope I have answered all your questions there.

      Usually we couple Client ID with events, which are used to captured user interaction on web page (e.g. button click, video play, form fill, etc).

      With Client ID as a dimension and events as metrics, you will be able to see which are the users who have performed a certain action on your website.

      And to answer your 2nd question, yes we can definitely filter by individual Client ID with the intent to find out how is a particular user is doing.

      I will be writing a piece on how to truly identify our own customers and find out what each individual customer is doing on our website to provide a personalized user experience.

  2. Hi Zenny,

    Great article and definitely useful as I’ve used this method to push Client ID for almost all of our clients. However, there are a few that are not using GTM. What is the best way to go about those? What code needs to go directly on the site besides creating custom dimension of course?

    Emir

    • Hi Emir,

      Glad to hear that you have had some successes with capturing Client ID! For those few that are not using GTM, what platforms are they using? Are they using DTM, or no tag manager at all?

      For Adobe Analytics, DTM made it really easy for us to capture Client ID (Visitor ID). Create a data element with the custom script:
      return _satellite.getVisitorId().getMarketingCloudVisitorID();

      And there you go! As usual, map the data element to the respective eVar (for storing the Visitor ID) in one of your page load rule. And remember to set the expiration of eVar to “Never”.

      But if you are talking about Classic Google Analytics (the legacy one), you’re probably more familiar with _gaq.

      Below is the piece of code will need to directly on the site (replace XXXXXXX with your Tracking Id. In the article, we stored it in a variable name “GA Tracking Code”):

      for (i = 0, len = trackers.length; i < len; i += 1) { if (trackers[i].get('trackingId') === "XXXXXXX") { var clientID = trackers[i].get('clientId').toLowerCase().trim(); } } _gaq.push(['_setCustomVar', 1, 'Client ID', clientID, 1]);

      So what this piece of code does is... use the similar piece of code as seen in the article, instead of returning value, we now store the Client ID in a variable named clientID. Next, using the _gaq.push command, we store the Client ID in a custom variable named "Client ID".

      That should cover it (:

  3. It’s nearly impossible to find knowledgeable people for this topic, but you seem like
    you know what you’re talking about! Thanks

  4. An impressive share! I’ve just forwarded this onto a co-worker who was doing a little homework on this.
    And he actually ordered me dinner due to the fact that
    I found it for him… lol. So allow me to reword this….
    Thank YOU for the meal!! But yeah, thanks for spending some time to discuss this topic here on your internet
    site.

Leave a Reply

Your email address will not be published.


*