Using the Google Analytics Cookies

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

About the author

Carey

View all posts