Using the Google Analytics Cookies

2 Jun
2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

14th August 2007 / Day 226
Creative Commons License photo credit: Mrs Magic

In this article I will be looking at the raw data that Google Analytics records, picking apart the Google cookies and showing some examples of how to extract and use that data.

I personally find Google Analytics a great free tool and use it as default on most of my sites. There is of course the old privacy accusations that follow Google around (I have some insight and warnings for a future post) but generally I don’t let this keep me up at night.

On a project I found myself wanting access to certain Google Analytics data from within my own custom admin dashboard. I had looked at recording my own information but consider it bad practice to duplicate in this way. Another option I considered was exporting )data using the Google Analytics API but unfortunately the quota limits of 10000 requests per 24 hours (more API limits detailed here) meant I couldn’t have the real time statistics I wanted.

Eventually I decided to take a look at using the Google Analytics cookies themselves and recording the data locally. This also gave the benefit of being able to link website actions (in this case adding a product to cart, removing it etc.) to a specific Google Analytics user and therefore giving me a bigger picture of my user habits.

So as an overview, this is how I did it:
1) The page loads with the Google Analytics javascript, creating the cookie and sending data to Google
2) After the Google Analytics javascript, an Ajax request is fired passing along any local website actions (e.g. adding to cart)
3) The code run by the Ajax request deciphers the Google Analytics cookies and records all the information locally

There is a good reason for using an Ajax request. With PHP (or indeed any server side language) the Cookie details are accurate to the point of the initial page request. If this is a new visitor the Google Analytics Cookies will not yet exist as the Javascript has not been executed, therefore the script must fire after the Google Analytics Javascript has run.

Using Ajax also ensures that the details of the final page request are recorded.

Picking Apart the Google Analytics Cookies

The part that required the most figuring out was what the data within the Google Analytics cookies meant. There are actually three Cookies placed by Google Analytics:

__utma – A “long term” cookie containing the main details of the user
__utmb – A “current session” cookie containing details about the current website visit
__utmc – A “no expiration” cookie which is used only to determine if the user has closed their browser (therefore initiating a new session when they next visit)
__utmz – A “current session” cookie containing referral details about the current visit

Each cookie contains data seperated by a period (.), with the __utmz cookie further seperating details with a pipe (|). The cookies break down as follows (the segments represent the data in the order as they appear in the string):

__utma
Segment 1: Unique number identifying the user. Useful for keeping track of return visits and as a primary key for storing locally.
Segment 2: Unix timestamp of this visitors first ever visit to the website.
Segment 3: Unix timestamp of this visitors previous visit to the website.
Segment 4: Unix timestamp of this visitors current visit to the website (the start of the current session).
Segment 5: Total number of sessions.

__utmb
Segment 1: Pages viewed in the current session
Segment 2: Unknown (My notes say Responses…)

__utmz
Segment 2: Referral Count
Segment 3: Source Count
Segment 4: This segment contains all the information about where the visitor came from. It is seperated by pipes and then the label/value is seperated again by an equals sign. An example for this segment: “utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=keywords”. The information is as follows:
utmcsr: Source
utmcmd: Medium (e.g. for google it can be organic or adwords)
utmctr: The keywords used
utmcct: Campaign content. Adwords information.
utmccn: Campaign name. Adwords information.
utmgclid: Click ID from Adwords.

Some other cookies you may also see are __utmv and __utmx which are related to customer user segments and the Website Optimizer respectively.

If you have any questions on this leave a comment and I will update the article so everyone can benefit. You may also be able to glean some information from (although they don’t spec the cookies in detail):

http://code.google.com/apis/analytics/docs/concepts/gaConceptsCookies.html

Bookmark and Share

4 Responses to Using the Google Analytics Cookies

Avatar

Mike Belasco

October 26th, 2009 at 2:08 pm

Excellent break down. Do you have code sample of accessing and reading data from the cookie available?

Thank you!
Mike

Avatar

Polly

December 4th, 2009 at 9:34 am

Thanks, please can share an examlpe code for accessing and reading data from GA cookies?

Thank you
Polly

Avatar

Jorge

March 12th, 2010 at 1:59 pm

Thank you! This is great information.

Since you seem to know your way around GA and the way they use cookies I have a pickle for you…

Let’s say a person goes to google.com, searches for certain keywords and then clicks on an AdWords ad that gets them to my website. When they get to my website they get served all the GA cookies as usual. it so happens that 40% of our “conversions” (which is a form being submitted) happen over the phone; that is, the visitor calls our office and then one of our reps fills the form for them.

We also have a “Session ID” generated on our website that is unique for each visitor. So when a person clicks on an AdWords ad, they get to our site, get their GA cookies and we also generate the Session ID which we store locally. We then ask for the Session ID over the phone (we have it on a corner of our website) and they read it back to us.

My question is: Since I can match a unique Session ID with a unique set of GA Cookies, would I be able to “recreate” those cookies in my local machine, fill out the form and when I submit it “trick” GA to think that the one who filled out the form was actually the original website visitor? All this is aimed at being able to record conversions down to the keyword level for all online AND offline conversions.

Thank you!

Avatar

Carey

March 16th, 2010 at 6:21 am

@Polly @Mike – I will try and get some code examples on soon. It is quite integrated to a system I use but I should be able to give a pseudo code workflow at least.

@Jorge it sounds like a tricky scenario but here is what I would suggest… once your sales guy has filled out the form, give the visitor a reason to revisit your website. For example, a free report or confirmation of details page. Email this to them and encourage them to click through in order to complete. Make that page a goal conversion page in GA (you could have it as a separate “offline conversions” goal).

Trying to spoof cookies would be a bit unreliable and could cause problems if the visitor comes back to your site. I have never tried so I can’t say that it will or won’t work, but it seems a bit “hacky” to me and I would try and avoid that route.

Hope that helps!

Comment Form

top