Extracting useful data from your Analytics cookies
As I discussed in my earlier blog post Analytics sets a number of cookies to track visitor interaction with your website. In this post I will show you how you can extract (and insert) useful data from one particular cookie: __utmz. Although I will just be focussing on this cookie, this principal is still the same for the other cookies.
As you may remember __utmz was the cookie that stores data about how a visitor arrived at your site. Here how you cna find your own example of the cookie data:
I generally use Firefox to check the cookie data as it makes it a whole lot easier than IE. To get to the cookie data just select: Tools>Options>Privacy>Show Cookies, then enter the name of your site e.g willmartinez.com.
The important data to focus on from the image above is:
utmccn=(organic)|utmcsr=google|utmctr=motorcaravan+insurance|utmcmd=organic
As you can see all the variables I mentioned earlier are visible in a string of data, each variable separated by the pipe character |.
So what does this data tell us?
It tells us that I arrived at this site when I searched for ‘Motorcaravan Insurance’ on Google and then clicked on an organic search listing. Had I have arrived from a Yahoo organic search listing the data would have remained much the same and providing I searched using the same keywords only the utmcsr variable would have changed from Google to Yahoo.
What If I had arrived through a paid search ad?
Well, I will tell you but I would suggest trying this out for yourself and getting used to seeing the data first hand. Here’s an example:
utmcsr=Google|utmccn=(not%20set)|utmcmd=cpc|utmctr=motorcaravan+insurance
This shows I searched for Motorcaravan Insurance on Google then clicked the paid search ad and landed on the site. cpc refers to paid search or cost-per-click advertising. The utmccn variable is not set because this is not set automatically, you will have to append some variables to the end of your URL to set this, which I will now show you how to do.
Inserting data into the cookie variables
Inserting data is very simple. You just need to add the variable names and the values or data you want added to them at the end of the url.
e.g.
www.willmartinez.com/index.php?utmcsr=Affilliate&utmccn=aff_campaign&utmcmd=thewebsite.com&utmctr=none
To add variable data to be passed to a page you simple add the data after a ‘?’ in the sites URL. Then you write the variable_name=value any additional variable data is added by inserting an ‘&’ between them.
Accessing cookie data using Javascript
The two functions below work together to extract the data from your Analytics tracking cookies:
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function getVariable(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;
} The getCookie function takes a String as an argument which will be the name of the cookie and the function returns the contents of the cookie as a String.
This String can then be passed to the getVariable function which returns the value of the chosen variable.
Please note these functions should be called after the Analytics code in the page.
Here is a full example of a form which submits the details a user supplies as well as how they arrived at the site:
<html>
<head>
<title>Will Martinez’s Example</title>
<script type="text/javascript" src="/js/cookiefunctions.js"></script>
</head>
<body>
<form name="myForm" id="myForm" method="post" action="process.php">
<input type="hidden" name="Referer" id="Referer" value="" />
<p>name: <input type="text" name="name" /> </p>
<p>email: <input type="text" name="email" /> </p>
<input id="submit" type="submit" name="submit" value="SUBMIT" />
</form>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("xx-xxxxxx-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<script type="text/javascript">
var z = getCookie("__utmz");
var c = _uGC(z,"utmcsr=","|");
var d = _uGC(z,"utmcmd=","|");
var ref = c+" "+d;
document.getElementById('Referer').value=ref;
</script></body>
</html>



July 21st, 2010 at 6:39 am
Buy:Viagra Soft Tabs.Super Active ED Pack.Cialis Super Active+.VPXL.Cialis Soft Tabs.Cialis.Levitra.Soma.Tramadol.Propecia.Maxaman.Viagra.Viagra Super Active+.Cialis Professional.Viagra Super Force.Viagra Professional.Zithromax….
August 30th, 2010 at 2:14 am
panasonic http://jjohng0o.01DODGEPARTS.US/tag/TH-50PZ800U+panasonic+pz/ : TH-50PZ800U…
panasonic…
September 5th, 2010 at 10:50 am
Buy:VPXL.Viagra.Zithromax.Viagra Super Active+.Cialis Professional.Tramadol.Super Active ED Pack.Levitra.Propecia.Soma.Viagra Professional.Cialis Soft Tabs.Cialis Super Active+.Viagra Soft Tabs.Cialis.Maxaman.Viagra Super Force….
September 6th, 2010 at 7:09 am
Buy:Lumigan.Prevacid.Nexium.Zyban.Petcam (Metacam) Oral Suspension.Actos.100% Pure Okinawan Coral Calcium.Valtrex.Mega Hoodia.Synthroid.Retin-A.Arimidex.Accutane.Zovirax.Human Growth Hormone.Prednisolone….
September 7th, 2010 at 2:00 am
Buy:Wellbutrin SR.Lipothin.Aricept.Zetia.Female Cialis.Benicar.Female Pink Viagra.Lipitor.Acomplia.SleepWell.Buspar.Amoxicillin.Zocor.Cozaar.Ventolin.Advair.Lasix.Prozac.Seroquel.Nymphomax….