Google Tag Manager: Visitor UUID Cookie Setter Tag

Table of Contents

Sometime you want to generate UUID for visitors on your web property and set UUID as cookie. I want to show how easy it is to set your own visitor UUID cookie using Google Tag Manager(GTM). Once visitor UUID cookie is set you can pass this UUID to other tags. Normally most of tags set tag specific UUIDs but by setting your own UUID you can manage your own cookie match table with your own UUID as primary key.

So lets get started. We are gone create a custom HTML tag visitorUUID in GTM using custom HTML tag and following template.

<script type="text/javascript">
(function() {
   // UUID generator
  // Private array of chars to use
  var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

  // RFC4122v4 compliant uuid generator
  Math.uuid = function() {
    var chars = CHARS, uuid = new Array(36), rnd=0, r;
    for (var i = 0; i < 36; i++) {
      if (i==8 || i==13 ||  i==18 || i==23) {
        uuid[i] = '-';
      } else if (i==14) {
        uuid[i] = '4';
      } else {
        if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
        r = rnd & 0xf;
        rnd = rnd >> 4;
        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
      }
    }
    return uuid.join('');
  };

})();

function createCookie(name,value,days) {
     // Cookie setter function
    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=/";
}

// Generate and set visitorUUID cookie with expiry after 1 year i.e. 365 days
createCookie("visitorUUID", Math.uuid(), 365);
</script>

This template has 3 key components,

  1. RFC4122v4 compliant uuid generator Math.uuid (Adopted from work of Robert Kieffer)
  2. A generic cookie setter function createCookie
  3. Finally, expression to generate UUID and set visitorUUID cookie with expiry after 1 year (i.e. 365 days)

Set the priority for the tag higher using advanced settings in case other tags rely on the visitorUUID cookie.

RFC4122v4 compliant Visitor UUID GTM Tag
RFC4122v4 compliant Visitor UUID GTM Tag

Now lets create a macro visitorUUID which reads first party cookie visitorUUID and set value in the macro. We are going to use this macro for the firing rule. Please note other tags can use this macro to get value of our UUID cookie.

Visitor UUID Cockie Macro
Visitor UUID Cockie Macro

Now it is time to set the firing rule for this tag. This tag should fire,

  1. On all pages, after DOM is ready, and
  2. Only when UUID cookie is not set

To find if UUID cookie is not set or not - we are looking for - character in the visitorUUID macro. If visitor UUID cookie is not set then condition visitorUUID macro does not contain character - will be true and hence tag will fire and set the cookie. Create the rule as illustrated below and attach it to our tag.

Visitor UUID GTM tag firing rule
Visitor UUID GTM tag firing rule

That’s it, your visitor UUID tag is ready for action.

Related Posts

Google Tag Manager: Ecommerce Integration

Google Tag Manager: Ecommerce Integration

Believe it or not, on average an US ecommerce website has between 15-20 marketing tags. Implementing …

Google Tag Manager: How it works

Google Tag Manager: How it works

In simple words, a Google Tag Manager (GTM) container holds other tags. GTM container is also know …

Google Tag Manager: Lookup table and DRY Principle

Google Tag Manager: Lookup table and DRY Principle

By default Google Tag Manager (GTM) provides several handy macros and lookup table macro is one of …