Extracting Data from 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 are, of course, data privacy concerns that follow Google around but I won’t get into those here.

On a recent 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 it felt unnecessary to duplicate what Google Analytics is already doing.

Another option I considered was exporting data using the Google Analytics API but unfortunately the quota limits of 10,000 requests per 24 hours means I wouldn’t have the real time statistics I wanted.

I decided to use the Google Analytics cookies directly and record 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 server side 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 the data within the Google Analytics cookies.

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 separated by a period (.), with the

__utmz cookie further separating 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 separated by pipes and then the label/value is separated 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):

https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage?csw=1

 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on reddit
Share on Reddit
Share on whatsapp
Share on WhatsApp
Share on email
Share by Email

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More Articles by Carey Baird

When is your app ready?

This old mantra still does the rounds: Release early, release often! This doesn’t give you the full picture. When I first started out creating my

Read More »

Finding Your True Goals

Setting goals is easy, but quite often we don’t achieve them. The most important step in goal setting is finding out what we really want. This is how to find your true goals.

Read More »

Success is a Garden

Last Updated: 5th September 2021 Success is not a place you reach and rest in. Success is a garden. It needs regular watering, pruning, shaping

Read More »