Send GA Client ID as custom dimension with GTM

There are many reasons why you would want the GA client ID collected.  Let’s just say getting the value of the GA cookie gives you a lot of options for extra reports and extra info. In the future I’ll use the GA client ID for some fancy stuff, so you’ll see some practical uses of collecting it.

For the moment let’s keep to the point!

What is the GA Client ID?

First – let’s start with the fact I am writing about Universal Analytics. If you are still using the Classic Analytics code (the ga.js library) don’t expect me to give you a solution for it. It may sounds harsh, but there is no reason not to use Universal Analytics, so if you don’t – just upgrade your analytics account. It’s just 2 clicks hassle and you get a lot of benefits (including this guide).

So what is GA client ID?

The GA Client Id is a unique, randomly generated string (actually it’s combination of timestamp of your first visit and random number) that gets stored in the browsers cookies, so subsequent visits to the same site can be associated with the same user (copy-pasted right from the Google Analytics documentation).

While a visitor don’t change the browser/device or don’t clear his/her cookies – the client Id is used to identify if the visitor is new or returning.

How to collect the GA User ID with Google Tag manager?

Grabbing the Client Id is as easy as writing (or copy-pasting) a few rows of code:

function(){
try {
    var tracker = ga.getAll()[0];
    return tracker.get('clientId');
  } catch(e) {
    return "n/a";
  }
}

Just place this code in a new Custom JavaScript Variable and you are good to go.

Sending the GA User ID to Google Analytics

Now, after you collected the Client ID you’ll need to send it. Just create an Universal Analytics Event tag and send the collected Client Id as a custom dimension. Go to your Google Analytics admin panel, select “Custom Definitions” and then “Custom Dimension”. Create a new dimension with the name “Client Id” and remember the dimension’s index! Make the scope of this dimension as “User“, because you want to catch the value and assign it to the user.

Creating Custom Dimension in Google Analytics

When you have this ready it’s time to send the event with Google Tag Manager. This is easier done than said. Just create new GA event tag like this:

Client ID tag

The {{Constant - UA Code}} is your UA code (check this article for more information) and the {{CJS - Client ID}} is the variable we just created the previous step. Don’t forget to put true in the “Non-Interaction” option, so your tag won’t trigger an interaction.

Add the custom dimension you created in the previous step (it’s 1 in the screenshot, don’t forget to change that number depending on your dimension index).

Fire the Client ID event

As the Client Id is in direct dependency with your Pageview tag – you don’t need to make any trigger for the moment. In order to be sure you sent the Client Id you need to be sure you have the Client Id in the first place!

Your Pageview tag is the one that creates the Id, so you want to trigger your event right after the GA Pageview tag fired. That’s why instead of using regular trigger you will want to save the tag without one. Then go to your Pageview Tag and add the Client Id event to fire right after the Pageview Tag fired (this will make sure that you already collected the Client Id value when you are firing the Client Id tag):
Fire your client Id event

After that step it’s time to debug and publish your setup.

Now you have everything ready to start collecting the Client Id values of your users! In this blog you’ll find some great practical uses of this Client Id for some great custom reporting!


Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home/turbose/public_html/wp-includes/class-wp-term.php on line 198

Check these related articles:


Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/turbose/public_html/wp-includes/wp-db.php on line 3030

Deprecated: Creation of dynamic property WP_Query::$comments_by_type is deprecated in /home/turbose/public_html/wp-includes/comment-template.php on line 1528

11 Comments

  1. Hi, it is not working for me as I am always getting “n/a” for the clientID, apparently when the pageview event has finished the client id tag event start but it seems the ga variable has not been set yet so I always get “n/a”, do you know a way of avoiding this?

    From this page [https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id] I have got this..
    “You should not directly access the cookie analytics.js sets, as the cookie format might change in the future. Instead, developers should use the readyCallback to wait until analytics.js is loaded, and then get the clientId value stored on the tracker.

    ga(function(tracker) {
    var clientId = tracker.get(‘clientId’);
    });

    Do you think is has something to do?

    1. Hey Gustavo,
      The easiest way to avoid this is to use the “Window Loaded” trigger for the Client ID event. This will ensure the clientId is available when you need it.

      1. Emil, thanks for your answer. I also did that but still getting “n/a” on a custom report I created where I have clientId as a dimension a Pageviews as metric. I have checked on the preview mode and the tag was triggered ok with the right value but it does not seem to arrive to google that way, not sure how to debug that. Do I have to do something else to tie up pageviews with the clientId? I am asking this because pageview tag runs first and then, when the page fully load, the clientId tag runs. Thanks!

      2. Did you made the change in the last hour or two before checking the custom report? It’s possible you’ll need to wait a few hours for the data to come in the reports, as there is some delay, based on my experience – between 2 and 8 hours.
        Nothing else is needed for the setup, as the dimension is user based – it could be used in combination with every other dimension/metric.

      3. I did the change last night, I am seeing the new pageviews on the report but (with the right timestamp, I also implemented that) but on the clientId dimension I am seeing “n/a”. I will keep debugging to see if can find why.. Thanks for your answers.

      4. When debugging I am seeing the pageview tag goes to GA but not the even, the weird part is that on the preview mode, I am seeing the tag being fired. Sorry to keep asking, but do you know what that could be?

      5. It’s hard to know what may be the problem without checking the debug panel, but I’ll fire in the dark – is your custom dimension with user scope?

  2. Hi, great post. Just checking, once this has been setup – How do you view it in GA? Where’s the report found?

    1. Hey Neil, once you do the setup you can use it as custom dimension in any report you need (or make a custom reports using it).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.