Store Client ID in Google Analytics using Custom Tasks

Google’s client (sometimes referred to as cookie) ID is how Google Analytics identifies you as the same person on returning to a website. This ID has only recently been exposed in the Google Analytics interface, and is only visible in the User Explorer report.

But what if you need this ID to be more visible. Perhaps your CRM system picks up Google’s client ID on a form submission, and stores it alongside the users’s record. At a later date, when this lead becomes a customer, you may want to run some attribution to determine which marketing channel drop them to the website.

In order to do that, you need to break down client ID by marketing channel in Google Analytics.

Therefore, I recommend you save Google’s client ID as a custom dimension in Google Analytics.

Here’s how.

Step 1: Create a custom dimension

Log in to Google Analytics and create a hit level custom dimension. That’s right, hit not session. A session scoped custom dimension might be useful, but I’ve encountered situations where multiple people use the same computer, and the session lasts a long time, continually overwriting the previous values.

We’re also going to be sending the client ID with every Google Analytics request, so hit scoped is fine.

Client ID Custom Dimension in GA

I’ve created my Client ID dimension in slot 5. Remember this number, it’s important.

Step 2: Edit your GTM settings

Create a new Custom JavaScript variable in Google Tag Manager called client_id_setter. Add the following JavaScript:

(Thanks to Seva Mironovich for pointing out my first version of the code was more complex than it needed to be!)

This uses an advanced feature of Google Analytics, which allows manipulation of the function used to build the pixel request sent to Google Analytics.

You’ll need to replace the text dimension5 with the index number of the custom dimension you created to hold the client ID in Google Analytics.

For more information on custom tasks please refer to the official Google documentation.

Next up, open your Google Analytics Settings variable (create on if you don’t have one).

Under Fields to Set, create a new entry called customTask (note the case), and select the client_id_setter variable you just created as the value.

Custom Task in GTM

Preview your Google Tag Manager container. If you look at requests being sent to Google Analytics you’ll see an extra custom dimension parameter, in my case cd5, is being sent.

Try it out

GTM Bot will automatically set up a Google Tag Manager container for you with the settings above, along with many other common analytics tags.

Give it a go!

Ed Brocklebank is the founder of Metric Mogul, a digital analytics consultancy. He helps business of all sizes become more data-driven through marketing technology. He runs training for General Assembly London on Google Analytics and Google Tag Manager.