How Does Universal Analytics Work?

Posted by in Analytics on May 13th, 2013 2 Comments

In my last post I quickly discussed that the new Universal Analytics code was in beta. You can read that write-up here.

google 300x228 How Does Universal Analytics Work?

Now I wanted to go over how Universal Analytics actually works. Its brand new code to us all, looking and working completely differently to the previous versions. However, it is possible to de-mystify the code and gain a better understanding of how it works.

First of all let’s have a look at the new code:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-XXXXX-Y’, ‘example.co.uk’);
ga(‘send’, ‘pageview’);

 

The code asynchronously loads the tracking code onto the page. Then a tracker object is created for the web property that was specified with the UA number. Last, but by no means least, the view of the page on which the script was run is recorded!

In a little more detail, the code runs and creates a new global object called ga. Then the library analytics.js is loaded asynchronously onto the page.

Although the JavaScript loads the analytics.js library in an asynchronous manner, the ga function can be used before the library has actually loaded onto the page. When you first start executing methods, each one is added into a “queue”. Then, once the library is fully loaded, the methods queued are carried out.

Although Javascript is still used within this new code all of the methods have changed.

So, if we look in more detail at the second half of the code we can see the “ga” function is used to “create” a default tracker object. The “string” of the UA number is then used to set the tracking id and then the optional “object” configures the domain.

ga(‘create’, trackingId, opt_configObject);

 

To send a pageview, you get the “ga” function to “send” a method with the “pageview” hit type. This lets the Analytics servers know what page the person is on.

ga(‘send’, ‘pageview’);

 

Behind the scenes the “title” and “page” values are set. These can also be overwritten should need be.

ga(‘send’, ‘pageview’, {
‘page’: ‘/new-page-path-goes-here’,
‘title’: ‘New Page Title Goes Here’
});

 

That’s the slightly techie side of how the code works, but there’s one other thing that I want to cover in this post. COOKIES! Or should I say cookie, the new Universal code only sets down one cookie.

The new cookie, named “_ga” is mainly to identify the user – the main reason for the new code. This is done through assigning a random number to the user and storing that number in the cookie. The number is then provided in each call to the analytics servers and is used to calculate visit, visitor and campaign metrics. The new cookie lasts for 2 years, although this will once again get pushed back by 2 years each time the user visits the site.

This first party cookie really is the cornerstone to the new code, allowing us to track the user rather than merely the visit.

So that’s it for this tech heavy blog post, I hope that you all now have a better understanding of how the new Universal Analytics code works.

pixel How Does Universal Analytics Work?