New vs Returning Client-ID Variable In Google Tag Manager

Sometimes, while configuring tags in Google Tag Manager, we want to know if the user visiting our site for the first time or not. Variable like that can be very useful if we want to fire some of our tags only for new or returning users, or assign different conversion value of the same conversion for this two groups of users.

For implementing this – we need to create a new cookie file to keep that information during the session of our user. We can do that with the help of Google Tag Manager and some JavaScript code. So go to your Google Tag Mager account and create a new custom HTML tag.  First part of our tag should read ‘_ga’ cookie`s value – that cookie is used to distinguish users and keeps some information like client ID or timestamp of the first time the cookie was set for that user. It`s value is not so interesting for us now, the idea is – if that cookie is already exists – user has been already been on our site, if not – he is the new one. By the way, you can see cookies in “developers tool” of your browser (ctrl+shift+i in Chrome):

So if ‘_ga’ cookie doesn’t exists yet – we should create a new cookie that would keep an information about our user (is he new or not) during his session. Also we should create a new variable in Google Tag Manager, that would return the value, wich that cookie stores. After first visiting our site user would get his ‘_ga’ cookie on his browser, so if he would close our site and will come back in the next session – our cookie`s value would be “false”. If he doesn`t have  ‘_ga’ cookie yet, our cookie`s value would be  – “true”. So that is the JavaScript code, that would do that (I combined it from few ready functions):

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);}
return null;}
var clientID = readCookie('_ga');
if (!clientID) {var x = 'true'}
else {var x = 'false'};
function createCookie(name,value,days) {
if (days) {
var date = new Date();
var expires = "; expires="+date.toGMTString();}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";};
createCookie('NewClient', x);

All you need is to create a custom HTML tag with this code, and fire it only once on the landing page:

You can do that with trigger`s logic: “Page View”, where “Refferer” doesn’t contain “your site domain”:

After that – create a custom 1-st Party Cookie variable with your cookie`s name, so it would return the value of the cookie that code creates:

I called that cookie “NewClient”:

In the same way, I named the variable:

So now you can use that variable in your Tag Manager triggers. Good luck!

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments