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):

<script>
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();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";};
createCookie('NewClient', x);
</script>

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!

3 thoughts on “New vs Returning Client-ID Variable In Google Tag Manager

  • Hello! I understand this is kind of off-topic however I needed to ask.
    Does running a well-established website like yours take a massive amount work?
    I am brand new to running a blog however I do write in my journal everyday.
    I’d like to start a blog so I will be able to share my own experience and feelings online.

    Please let me know if you have any kind of recommendations
    or tips for brand new aspiring bloggers.

    Thankyou!

    • Hi! I would not say that running a text blog is very hard for me or takes lot of time, If you are a beginner I can definitely suggest to build it on WordPress, If you need domain and hosting – I can recommend you to try Hostinger for that purposes. Hope I’ve helped You =)
      Kind Regards,
      Serhii

Leave a Reply

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