Google Analytics

Capturing First-Touch Source Information with Custom Variables

Since Google Analytics released custom variables last October, we’ve been finding all kinds of ways to use this flexible, powerful feature. From accomplishing content groupings to tracking the count of purchases by repeat buyers, custom variables have opened the door to both new data in GA and new abilities to segment our data.

One of our favorite uses for custom variables is storing first-touch traffic source information. As you may know, the default model for conversion attribution in Google Analytics is last-touch. In other words, recorded conversions and transactions are attributed to the last traffic source (with the notable exception of direct traffic, which will not override another traffic source). Therefore, it’s difficult to get an understanding of how other traffic sources contribute to conversions. Custom variables have made it much easier to get as this kind of insight with Google Analytics.

Beware this issue!

When deploying custom variables to track the first touch, we’ve uncovered an interesting aspect of Google’s 64-character limit for custom variables. First, some background: to record and store first-touch source information, you need to parse GA’s _utmz cookie and store the relevant information in the _utmv cookie, which is dedicated to custom variables. In doing so, it’s easy to hit the character limit (consider that you’re potentially storing source, medium, campaign, keyword, and ad content information).

Here’s the rub: if you try to store more than 64 characters’ worth of information in the _utmv cookie, Google doesn’t simply cut you off at 64 characters and send as much data back as possible. Google flat-out won’t send your custom variable at all!

To avoid this, here are some tips:

1. Choose a short name for your custom variable’s “key,” like “FT” for first touch. The custom variable uses a “key/value” structure in which the key is basically a category of data; in this use case, your key is just first touch, while the value is something like google/organic/big blue widgets. By choosing a short name for your key, you’ll save as many characters as possible for your actual value.

2. Perform a RegEx (regular expression) search-and-replace on the value of your key/value pair to keep only characters that do not require URL encoding. You’ll save yourself many characters by avoiding the need to URL-encode some characters.

3. Trim the length of the value string to 64 characters minus the length of the key. In other words: Value = 64 – (Key length).

4. Finally, go ahead and call setCustomVar() to store the first-touch data in a custom variable.

Here’s the code you’ll need to parse the _utmz cookie and then accomplish steps 2 through 4!

//Used to obtain a value from a string of key/value pairs

function _uGC(l,n,s) {

if (!l || l=="" || !n || n=="" || !s || s=="") return "-";

var i,i2,i3,c="-";

i=l.indexOf(n);

i3=n.indexOf("=")+1;

if (i > -1) {

i2=l.indexOf(s,i); if (i2

c=l.substring((i+i3),i2);

}

return c;

}

//Retrieve campaign and referrer info from the _utmz cookie

var z = _uGC(document.cookie, '__utmz=', ';');

var source  = _uGC(z, 'utmcsr=', '|');

var medium  = _uGC(z, 'utmcmd=', '|');

var term    = _uGC(z, 'utmctr=', '|');

var content = _uGC(z, 'utmcct=', '|');

var campaign = _uGC(z, 'utmccn=', '|');

var gclid   = _uGC(z, 'utmgclid=', '|');

//Replace empty values (marked by a dash) with an empty string

if(source=="-"){source=""};

if(medium=="-"){medium=""};

if(term=="-"){term=""};

if(content=="-"){content=""};

if(campaign=="-"){campaign=""};

//If gclid is present, explicitly set source/medium to google/cpc

if (gclid !=="-") {

source = 'google';

medium = 'cpc';

}

//Build utmString

var utmString = source;

utmString=utmString+"!"+medium;

utmString=utmString+"!"+campaign;

utmString=utmString+"!"+term;

utmString=utmString+"!"+content;

//Replace URL-encoded 'spaces' with dashes

utmString=utmString.replace('%20','-');

//RegEx to retain only whitelisted characters

utmString=utmString.replace(/[^a-zA-Z0-9-~!*_.]/g,'');

//Set string to specific length

utmString=utmString.substr(0,62);

//Set first touch information if not already there, using slot 3

var fT=pageTracker._getVisitorCustomVar(3);

if(!fT){

pageTracker._setCustomVar(3,'FT',utmString,1);

}

Finally, a word on why first-touch traffic source information can be so valuable. GA’s default attribution model gives you only one view of how valuable various traffic sources are for you. Getting another view can only help you, especially because GA’s default model tends to undervalue traffic sources that may not be as prone to immediate direct response, but could still be adding value for you. Examples of this kind of traffic commonly include display advertising (e.g. Google content network, other banner campaigns) and social networking. By storing first-touch source information, you give yourself the ability to perform a more holistic assessment of the value of these kinds of traffic.

Be aware that this does not only apply to storing first touch information in the cookie. You should always make sure that your cookie length is not too long and does not contain any special characters.

If you have questions on first-touch source information or our solution above, feel free to leave them in the comments. To get more analytics tips and tricks, don’t forget to subscribe to our feed and follow us on Twitter!
Nick Iyengar

Nick is Vice President of Analytics at Cardinal Path, where he is responsible for the commercialization and delivery of Google Analytics and related services. When not working with clients, Nick authors original research, articles and blog posts, and speaks at conferences around the world. He is an alumnus of the 2023 college football national champion University of Michigan.

Share
Published by
Nick Iyengar

Recent Posts

Optimizing user experiences with Digital Experience Analytics (DXA) platforms

As consumers become increasingly digitally savvy, and more and more brand touchpoints take place online,…

1 month ago

Enabling Value-Based Bidding with Google Tightlock

Marketers are on a constant journey to optimize the efficiency of paid search advertising. In…

1 month ago

Resolving “Unassigned” Traffic in GA4

Unassigned traffic in Google Analytics 4 (GA4) can be frustrating for data analysts to deal…

2 months ago

This website uses cookies.