Wednesday, November 22, 2023
HomeWeb AnalysisAll of the ways to get a client ID with Google Analytics!

All of the ways to get a client ID with Google Analytics!

In Google Analytics, both in the era of Universal Analytics and in the era of Google Analytics 4 , users are identified based on the “client ID” recorded in the browser’s cookie . In Google Analytics 4, the use of Google signals is strongly emphasized, but this does not mean that user identification by “client ID” will disappear, but it is a Google signal to complement the client ID, and the importance of the client ID is still No change.

Such a client ID is processed on the server side of Google Analytics in the standard state, and it is not in a state where users themselves can handle it freely. In order for the user to freely use this client ID, it is necessary to customize tags, link with custom dimensions, and link with other tools.

In this article, I would like to cover and explain all patterns of customization methods to utilize such custom dimensions.

Two ways to get the client ID

There are two main methods for obtaining a client ID, classified according to the destination after obtaining the client ID. The first pattern is a pattern that can be used as a custom dimension (event parameter or user parameter in the case of GA4) in the installed Google Analytics. The second pattern is to link the client ID to tools other than Google Analytics, and later integrate users using the client ID as a key.

They look similar, but each requires some attention.

Also, how to use Google Analytics is currently

  • Scene 1: Universal Analytics tag (analytics.js) with direct placement
  • Scene 2: Global tag with direct placement (gtag.js)
  • Scene 3: Universal Analytics by setting template tags in Google Tag Manager
  • Scene 4: Google Analytics 4 tag by setting template tag in Google Tag Manager

Since there are 4 patterns, it is necessary to consider each. Find out how you have implemented Google Analytics on your site before reading on.

Let’s take a look at each scene.

Scene 1: Installation with analytics.js tags

Pattern 1: Set as custom dimension

There are various installation methods, but it is better to implement it in the callback form of the ga function. In code, it looks like this:

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

ga('create', 'UA-XXXXX-Y', 'auto');
ga(function (tracker) {
  ga('set', 'dimension1', tracker.get('clientId'));
  ga('send', 'pageview');

Pattern 2: Use with other marketing tools

The code is almost the same as pattern 1. Note that getting the client id is done asynchronously and the client id is only available inside the callback function. When actually using it with other marketing tools, you need to rely on the tags provided by the tool vendor and think about where to place which code yourself.

ga(function (tracker) {
  var clientId = tracker.get('clientId');
  // 取得したクライアントIDをここで利用する。

Scene 2: Installation with gtag.js tag

Pattern 3: Set as custom dimension

SEM Technology’s past article [latest] How to set the client ID as a custom dimension with gtag.js will be helpful. By using the custom map (custom_map) feature, you can link your client ID to a custom dimension by adding a few lines of code to your regular tracking code.

Specifically, the code is as follows.

gtag('js', new Date());
gtag('config', 'UA-xxxxxxxx-y', {
  'custom_map': {
    'dimension1': 'clientId'

Pattern 4: Use with other marketing tools

You can get the client id using the gtag get command. The get command is an asynchronous command, so care must be taken when implementing it.

The basic idea is the same as pattern 2.

gtag('get', 'トラッキングID', 'clientId', function(clientId) {
  // 取得したクライアントIDをここで利用する。

Scene 3: UA tag installation by GTM

Here is the current standard Google Analytics implementation pattern.

Pattern 5: Set as Custom Dimension

If you are using Google Tag Manager, you only need to create one custom JavaScript variable and change one setting in the Google Analytics settings variable if you only want to set it as a custom dimension.

The custom JavaScript to be created looks like the following and uses the ” task function ” of Universal Analytics . For the function description of the task function, please refer to the developer documentation – tasks .

function () {
  return function (model) {
    model.set('dimension1', model.get('dimension1'));

The settings for the Google Analytics setting variables are as follows. In the “Setting Fields”, specify “customTask” as the field name and the custom JavaScript variable created above as the value. Note that the field name “customTask” will not appear as a suggestion.

Pattern 6: Use with other marketing tools

If you’re using Google Tag Manager, using your client ID with other marketing tools is a little more complicated. In particular, you need to be careful if you want to use it at the same time as Google Analytics is executed when the page loads.

The method is to load the Google Analytics tag first in your custom HTML, set it to a data layer variable after the tracker is generated, and allow a custom event to trigger the completion of getting the client id.

First, custom HTML for getting the client ID and sending custom events. Configure like in the capture below.

If you extract only the HTML code, it will be as follows. As for the processing content, a Google Analytics tracker is generated using a temporary tracking ID and tracker name, and the client ID is obtained and a custom event is sent in the tracker generation callback function.

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

ga('create', 'UA-99999999-99', 'auto', 'tmpTracker', {
  allowLinker: true
ga(function (tracker) {
    event: 'ga.initialized',
    clientId: tracker.get('clientId')

Then create a variable. The variable uses a data layer type variable and the variable name is “clientId”. Specifically, the settings are as shown in the following capture.

Now create the trigger. The trigger uses custom events and is created with the following settings.

The last is linking to marketing tools that use client IDs. Below is the code to link the client ID to the HubSpot tag.

Implementations vary by individual tool, so be sure to refer to each tool’s documentation. The point is to replace the part where you want to set the client ID with the GTM variable “{{GA Client ID}}”, and set the trigger to occur after “GA Initialized” (of course, the tag execution If the timing is not on page load, you’ll need to set it accordingly.)

Scene 4: GA4 tag installation by GTM

The GA4-specific client ID acquisition method is not currently published, but you can use the gtag get command in pattern 4. Also, when installing GA4 from Google Tag Manager, it is better to use the template tag “GTAG GET API” developed and provided by Mr. Simo. Patterns 7 and 8 below also assume the use of the “GTAG GET API”.

Please also see Simo’s blog post ” #GTMTIPS: WRITE CLIENT ID AND OTHER GTAG FIELDS INTO DATALAYER ” for how to use the “GTAG GET API”.

Preparation: Add GTAG GET API

Add “GTAG GET API” to your workspace from the Google Tag Manager tag template.

Next, create a tag using the added tag template. Let’s set the measurement ID of GA4 in “Measurement ID”.

Next, the GTAG GET API tag will send a custom event “gtagApiGet” when it completes its operation, so create a trigger for this custom event.

And the obtained client ID is set in the data layer variable “gtagApiResult.client_id”, so create a variable to get the value from this data layer variable.

Pattern 7: Set as Custom Dimension

Change the tag you want to execute when loading the page related to GA4 to the trigger “gtagApiGet” created from the “page view” trigger, and set the variable “gtagApiResult.client_id” to the event property (user property).

Pattern 8: Use with other marketing tools

If the tag you want to execute when the page is loaded, change the trigger from the “page view” trigger to the created trigger “gtagApiGet” (no need to change if it is executed after the page is loaded, such as when an element is clicked), and the variable “gtagApiResult.client_id” to use.


This time, for both Universal Analytics and GA4, we will explain how to obtain the “client ID”, which is the key used to identify users in Google Analytics, in “How to implement GA tags” and “Obtained client ID How to use” was divided into a total of 8 patterns and explained.

Once you have a client ID, you can integrate it with various CRM information, etc., to perform deeper analysis, so it would be a good idea to try it out.


Leave a reply

Please enter your comment!
Please enter your name here

Recent Posts

Popular Posts

Most Popular

Recent Comments