Get an RSS feed of WebShare's blog
RSS Feed
Follow WebShare on Twitter!
Follow WebShare on Twitter
WebShare's Search and Conversion Marketing Newsletter
Email Newsletter

Search & Conversion Marketing Blog

Welcome to our blog! Here you'll find posts on the topics of web design & traffic acquisition, paid and organic search strategies, analytics & business intelligence, conversion testing, interviews, videos and more. We hope you find it useful, and invite you to read, share, subscribe, leave a comment, or just have a look at what's here.

Capturing First-Touch Source Information with Custom Variables

Wednesday, May 19th, 2010 by Nick Iyengar
Google Buzz

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 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!

<script type=’text/javascript’>
//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 < 0) { i2=l.length; }
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);
}

</script>

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 a senior analytics and web intelligence analyst with WebShare. You can find out more about Nick here.

See more posts by Nick Iyengar

4 Responses to “Capturing First-Touch Source Information with Custom Variables”

  1. thanks Nick, very usful and step by step instructions.

  2. Nick says:

    You’re welcome Tahire – thanks for reading!

  3. Ana says:

    Nick, Thanks a lot for this helpful post.

    I wonder how this code would look like in conjunction with GA asyncronus code and (relative to asyncronus GATC) where to put which parts of your code. Share your ideas?

    Ana

  4. Nick says:

    Hi Ana,

    Stay tuned and we’ll update this post ASAP!

    Thanks for reading,

    Nick

Leave a Reply



WebShare is a Google Analytics Authorized Consultancy
WebShare is a Google Website Optimizer Authorized Consultancy
WebShare is a Google Adwords Qualified Company
WebShare is a Google Analytics Seminar Leader
WebShare is a Google Website Optimizer Seminar Leader
WebShare is a proud memeber of the Web Analytics Association
WebShare is a Microsoft AdExcellence Member
WebShare is a Yahoo Search Marketing Ambassador

© 2006-2010 WebShare, LLC. All Rights Reserved.
Sitemap