A Complete Guide to Google Universal Analytics Cookies

Google Universal Analytics Cookies

Image Source

Back in October 2012, Google launched a completely overhauled version of their analytics platform (the Google Analytics), called the Universal Analytics.

Obviously, Universal Analytics was a totally revamped version of the erstwhile Google Analytics. The new analytics platform came with a bevy of improvements in the back-end, a completely revamped tracking code and of course, a revolutionary new cookie structure, which will be our focus in this post. Let’s analyze what this new cookie structure is all about and how to get the most out of it.

So let’s get started.

But, if you are totally new to the world of cookies (not the edible chocolate chip cookies J), let me quickly enlighten you what a cookie in the computer world really is! (Btw, you can skip the intro if you have the basic know-how of a cookie!)

What is a Cookie?

A cookie is a text file containing a tiny chunk of information that is sent by a website and stored on the user’s computer by the user’s browser.

The cookie serves different purposes for a website. It can store information about the users’ preferences, location, and other data. It can block unauthorized access to users’ data and control certain functionalities of the website. The cookie also can make advertising more effective by displaying customized ads to users and facilitates re-marketing. And most importantly, it can collect important analytics tracking data for a website.

A cookie is browser-specific, which means, a cookie placed by the Firefox browser on your computer will not be accessible by Chrome browser. Cookies are also site-specific. That is, a cookie placed by one website cannot be accessed by another website. However, a cookie can be enabled and disabled through your web-browser settings.

Understanding the Universal Analytics Cookie

All Google Analytics (GA) versions are designed to place cookies on your computer to store and recall several customizations and tracking data. The Universal Analytics is no different. However, unlike the earlier Google Analytics versions that used four different cookies to collect analytics data, the Universal Analytics does that with just one cookie which they call the _ga cookie.

The _ga cookie stores one critical information, the Client ID, which represents the website user. The Client ID will look something similar to this GA1.2.7889899.1500475869, which is stored by this cookie and then placed on the user’s computer.

Interestingly, unlike the earlier Google Analytics versions, the Universal Analytics is crafted to collect user interaction and other analytics data without any cookies at all by using two other collection methods namely: a GA JavaScript Library named Analytics.js and GA Measurement Protocol.

How does the _ga cookie generate the Client ID?

In a basic implementation of Universal Analytics, when a user visits your site, the tracking code is executed, which checks if a _ga cookie already exists on the user’s computer, if not, the tracking code generates one and a random Client ID is created and stored in the cookie.

To technically understand how a _ga cookie generates the Client ID, we must first take a look at the Universal Analytics tracking code itself.

Here’s the example of the tracking code:

Universal Analytics Tracking Code Example

If you take a look at the tracking code, the function encapsulated in the RED box is what creates the _ga cookie. The _ga cookie then makes use of the Analytics.js library and generates a unique 32-bit number to every user visiting your site, which is the Client ID that we discussed earlier. This cookie and other analytics tracking data pertaining to your GA account are then utilized by the Google Analytics servers to calculate a wide range of metrics data about your website like the traffic, user sessions, ad campaigns, transactions and so on. In the analytics world, any user interaction that results in data being sent to the servers is known as a hit.

But you must note one important point here that _ga cookie is browser-specific and device-specific. If one user accesses your site through multiple browsers, Universal Analytics treats that as different users and different Client IDs are created for each browser. The same is true when a user opens your site on different devices.

Understanding the Client ID format in depth

As mentioned in the previous section, the _ga cookie stores a unique Client ID for every user that visits the site and it would look something like this,

GA1.2.7889899.1600644955

The Client ID is divided into four parts separated by dots.

In the first part i.e. GA1 the ‘GA’ obviously refers to Google Analytics and the ‘1’ represents the version of the cookie format.

In the second part, the number 2 represents the cookie path or where the cookie is set up on your website. For instance, if the cookie is set at document.example.org, the number would be 3 (representing the number of components of the domain separated by the dots). And if the cookie is set up at example.org, the number would be 2 and so on.

The third part of the Client ID i.e. 7889899 represents the randomly generated unique user id. Each user visiting your site will get a unique numerical user id.

The fourth part 1600644955 is the timestamp denoting when the user first landed on your site. And it is the rounded off to the closest second when the user first visited your site.

How is the _ga cookie info converted into meaningful analytics information?

It has been already discussed that the Client ID stored by the _ga cookie helps Universal Analytics to uniquely identify each user visiting your site. This distinction is made possible through the third and fourth part of the Client ID that we saw in the previous section. These sets of numbers also help uniquely identify the users when they revisit your site.

Earlier in the post, it was mentioned that every user action/behavior that results in analytics data being sent to the Google Servers is treated as a Hit. A Hit can be any user behavior like pageviews, transactions, bounces, completion of surveys, or any other event. Every Hit sent to the Universal Analytics server will have the Client ID attached to it.

Hit Data of particular User

Image Source

This Client ID attached to every hit helps Universal Analytics to connect all the information together pertaining to a single user and create Sessions. A user (or a Client ID) can have one or many Sessions divided into different time periods of a day.

Hits forming a Session

Image Source

How does –ga cookie handle cross-domain tracking?

Cross-domain tracking is the process of linking two related sites (one main domain and sub-domains) and the sessions on those sites into one single session. In Universal Analytics, this is made possible by adding a configuration object ‘cookieDomain’ as a parameter in the tracking object ga (), which we referred to in the last part of the third section – How does the _ga cookie generate the Client ID?.  

As an example, by default, the tracking object/function will look like this within the tracking code,

ga (‘create’, ‘UA-XXX-Y’, ‘example.com’);

Now let’s imagine that you have multiple sub-domains under your main domain (example.com) such as documents.example.com, forums.example.com, blog.example.com, and so on, and you want the _ga cookie to track all of them.

Then all you need to do is add a cookieDomain parameter/configuration object to the tracking function and set it to your main domain, and the resultant _ga cookie will automatically track all your sub-domains. Here’s how it is done technically,

ga (‘create’, ‘UA-XXX-Y’, { ‘cookieDomain’:  ‘example.com’});

Setting the expiry time of the _ga cookie

By default, the _ga cookie expires after 2 years or 24 months, but you can change the default expiration period to a customized value by adding the cookieExpires configuration parameter to the ga () function and assigning a value to it. This configuration parameter only accepts the value in seconds.

For instance, if you want the cookie to expire in 5 minutes, you need to assign 300 (60 seconds * 5) to the cookieExpires object, and it is done like this,

ga (‘create’, ‘UA-XXX-Y’, { ‘cookieExpires’:  300});

But if you want the cookie to expire after a single browser session, set the value to ‘0’. Generally, you wouldn’t want to do this as a website owner as it completely refutes the purpose of creating the _ga cookie that allows you to track the behavior of one user on your site over different time periods.

Changing the _ga cookie path

Although changing the cookie path is not at all recommended by Google, you can still change it by modifying the cookiePath parameter value (from ‘/’ to ‘/new_path/’) in the ga () function as shown below,

ga (‘create’, ‘UA-XXX-Y’, { ‘cookiePath’:  ‘/new_path/’});

By default, the _ga cookie is set up at the root level path (/) on the top level domain.

Disabling the _ga cookie

You can choose not to use the _ga cookie at all to store the Client ID and still retain the Client ID across sessions by using your own storage mechanism before sending it to the Google Analytics servers for processing your analytics tracking data. This is how you can do it.

ga (‘create’, ‘UA-XXX-Y’, { ‘storage’:  ‘none’, ‘clientid’: <create and retrieve it using a function>});

Note: When you are not depending on the tracking code to generate the Client ID you must rely on some other user-defined or system function to create the Client ID, and then retrieve it to the ga () function before sending it to the Universal Analytics servers.

What happens when a user clears the cookie?

This is the biggest disadvantage of using a cookie. It can get cleared anytime by the user. When a user clears the _ga cookie after his/her Client ID has been already sent to the Google Analytics server, on their next visit, the user will be assigned a brand new cookie and Client ID. Essentially, the same user will be treated as a completely new user on their subsequent visit if the cookie is cleared.

Let me conclude…

The _ga cookie forms an integral part of the Google’s Universal Analytics Cookie structure. And it is critical to know how it is created, configured and customized to make the most of the Universal Analytics platform.

I hope this post will serve as a good reference source that you can bookmark and refer back at your convenience to refresh your knowledge about the Universal Analytics cookies or tweak your _ga cookie on your website.

Your thoughts…

How comfortable are you with tweaking the analytics tracking code? Do you perform the changes on your own or have it done from a coder? Please share your thoughts in a comment below.

Lets work together

Get in touch