In my last post I quickly discussed that the new Universal Analytics code was in beta. You can read that write-up here.
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:
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.
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.
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.
Behind the scenes the “title” and “page” values are set. These can also be overwritten should need be.
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.