Google Analytics: Semi-Advanced Use of the UTMZ Cookie ;)
I recently had an interesting Google Analytics project come up that I thought may be useful to others. Let me set the stage with a little background information.
We use SalesForce as our CRM. SalesForce has a plethora (it’s always fun to use that word in a sentence) of reports natively available, but we found that some of the reports showing profitability of various marketing channels didn’t quite meet our needs.
The solution was to push additional data into SalesForce at the time of record creation to use as additional tracking fields. Simple solution, right? So we created tracking fields to store values for the following: medium, source, campaign, content, and term. If you’re familiar with utm tagging for analytics you’ll recognize that these are the five traditional manual utm tags.
At that point it is a simple process to capture the utm values from their corresponding URL parameters.
But, what about organic traffic that doesn’t come to your site nicely and neatly tagged? What about traffic that navigates away from the initial landing page (thereby losing the utm variable from the url), but later comes back and converts?
At this point, the only way to get to that data is with a little ninja/kung-fu coding. All the data is stored in the Google Analytics cookie. It’s merely a matter of parsing it so you can pull it back out of the cookie. Definitely not black-belt level coding, but it did take some trial and error.
A quick Google search turned up the following article on the utm cookie. Sadly it didn’t really help with my particular needs. Through a long process of trial and error involving setting my manual utm tags and watching the values stored in the cookie, I was able to identify what I needed.
The secret sauce is in the utmz cookie value ($_COOKIE[‘__utmz’] for all you fellow php programmers out there). This wonderful little cookie contains a single string value with a wealth of information, including all the utm tags that are either set by you through manual tagging, or by Google Analytics for organic visitors.
For example, if you used this URL (note the manual tags):
It would produce a utmz cookie value similar to this:
There are two important parts to this value.
Part the first – the integers:
The cookie value always begins with four integer values, delimited by a “.” (dot). It looks kind like a funky IP address. These represent timestamps, expiration dates, and visitor counts. For our purposes, we can ignore it.
Part the second – the value pairs:
Following the four integer values you will find value pairs (utmxyz=value123) delimited by a “|” (pipe). The value pairs vary, and there may additional pairs beyond the five we are interested in. The key five are:
utmctr = term
utmccn = campaign
utmcct = content
utmcsr = source
utmcmd = medium
This is where the code kung-fu comes in handy. There are several ways to access it, so pick your poison. I’m a php advocate, so I use something like this:
parse_str(str_replace(“|”,”&”,preg_replace(‘/^([0-9]+\.)+/’, ”, $_REQUEST['__utmz'])),$output);
This will create an array ($output) which can be accessed by index name of the parameter you want. For example, the medium value would be: $output[‘utmcmd’]
There you have it, Semi-Advanced Google Analytics embellishment. Using this convention we now have data congruency between Google Analytics and SalesForce.
Now isn’t that nice?