Universal Analytics Implementation – Tracking code

Here’s a long due update to Part 2 of the Google Analytics Implementation Guide where we’ll look at the Universal Analytics tracking code.

Universal Analytics Tracking code

It used to make sense for all simple single domain websites to use the basic GA tracking code without any modifications. You just select your account, go to Admin and click on the Tracking Info tab. This is how the basic UA tracking code looks like, where XXXXXX-Y is your property ID:

 (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-XXXXXX-Y', 'auto');
 ga('send', 'pageview');

* Replace UA-XXXXXX-Y with your property ID and don’t forget to add the opening and closing JavaScript tags.

 

Adjusting bounce rate with Universal Analytics

There are some other options you can tweak right on that page, like cross-domain tracking, AdWords remarketing through GA and custom campaign tags. We’ll look at something else now – bounce rate adjustment according to time spent on site.

The bounce rate is a really important metric used to evaluate the quality of traffic and landing pages. It shows how relevant your content is for the visitors and how engaged they are with your website.

A bounce is just one hit on your website. And there are two ways to measure it – as a single pageview and as a minimum time spent on the site. The standard GA script tracks a single pageview. Google Analytics, as other web analytics softwares, use timestamps to measure time on pages. It will take the timestamp of the initial hit on the page (e.g. 10:34:10) and the timestamp of the next engagement hit (e.g. 10:35:20) and subtract them to get the actual time on page (1 minute and 10 seconds). When there’s just one hit, there’s no second timestamp and Google Analytics counts this visit as a bounce and time on site is 0:00.

The truth is many visitors (almost half of those bounces) did really paid attention to your website before leaving it. This is valid especially for blogs, where new and regular visitors come, read an article which could be a longer one and take 10-15 minutes, then leave. And there is a way to track this, recommended as best practice by GA themselves (back in July 2012).

By adding one line to our basic GA tracking code, which executes an event when a user has spent over a certain amount of time on the page. You should determine this amount of time depending on your website and what you think is enough time to consider the visitor successfully engaged with your content – 10, 15 seconds or a few minutes.

When this event is executed, the visitor will no longer be counted as a bounce, because the hits are two now – one pageview and one event. And the bounce rate metric will show the real bounces who didn’t spent the required minimum time on the website.

Here’s how we should adjust the code to track minimum time on site (GA uses milliseconds to track time, so we are using 15,000 milliseconds in this case which are equal to 15 seconds):

 

 (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-XXXXXX-Y', 'auto');
 ga('send', 'pageview');
 setTimeout("ga('send','event','15 seconds','time on page more than 15 sec')",15000);

 

* Replace UA-XXXXXX-Y with your property ID and don’t forget to add the opening and closing JavaScript tags.

* If you have a very big site that generates millions of hits be careful with this implementation. The free GA has a limit of 10 million hits per month (hit being pageview, event, virtual pageview, ecommerce transaction, etc.) and if you’re close to this monthly number you may want to consider GA Premium.

 

In-Page Analytics – Enhanced Link Attribution with Universal Analytics

The In-Page Analytics report is available under the Content section and it’s a visual representation of how visitors interact with your pages. It gives us information about the design layout usability, are visitors focusing on the right content and are they engaging with our call to actions, what are they clicking on the page.

Until November 2012, the In-Page Analytics feature had some flows – it showed clickthrough information by URL and not by the actual link on the page. It also had clickthrough information only on links and not on other elements like buttons. So if you have two or more links to the same URL, Google Analytics shows the combined statistics for both links without distinguishing between one and the other and which of them the visitor actually clicked.

To fix this, you need to customize your tracking script by adding two lines of code and enable the feature in the web property settings. With these changes In-Page Analytics will be able to distinguish between multiple links to the same URL, attributing the right number of clicks to each link; provide click-through information even when redirects are being used; click-through information for other elements such as buttons or navigations that were triggered by JavaScript code. Here are the changes to your Universal Analytics tracking 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-XXXXXX-Y', 'auto');
 ga('require', 'linkid');
 ga('send', 'pageview');

 

* Replace UA-XXXXXX-Y with your property ID and don’t forget to add the opening and closing JavaScript tags.

 

Additionally, to enable enhanced link attribution:

  • Click Admin at the top of any GA page.
  • Click Property Settings tab.
  • In the In-Page Analytics section, select the Use enhanced link attribution option.
  • Click Apply.
Enhanced link attribution setup

Enhanced link attribution setup

 

Track subdomains with Universal Analytics

The good news here is that Universal Analytics supports subdomain tracking by default so no special changes to the tracking code are needed.

 

Track multiple domains with Universal Analytics

In case you need to track across multiple domains and subdomains, you need to customize your Universal Analytics tracking code as well. For example you may want to track together your main website (www.domain.com), your blog (blog.domain.com) and your shopping cart (shop.domain2.com)

Here’s the cross-domain tracking examples for domain.com and domain2.com:

 

 (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-XXXXXX-Y', 'auto',{'allowLinker': true }); 
 ga('require', 'linker');
 ga('linker:autoLink', ['domain.com','domain2.com']);
 ga('send', 'pageview');

 

* Replace UA-XXXXXX-Y with your property ID and domain.com and domain2.com with your actual domain names. Don’t forget to add the opening and closing JavaScript tags.

Another important thing here is to add both domains to the Referral Exclusion list. Go to Admin and click on Referral Exclusion List under Property – Tracking Info. Click the + Add Referral Exclusion button and add your domains: domain.com and domain2.com. There’s no need to add subdomains as Universal Analytics recognizes only the actual domain.

 

Track custom dimensions and metrics with Universal Analytics

With custom dimensions and metrics you can further refine Universal Analytics tracking like defining additional segments. There are different custom dimensions you can define – for example track logged in users, who shares your content on social media, or how people who were exposed to different variations in an A/B test behave.

Also, you should define a scope for every custom dimension, meaning at what level it should apply – hit, session or visitor level. When you set up visitor level dimensions it’s a good practice to do it in your main GA tracking code to save one additional hit. Here’s how the code for adding custom dimension and metric in Universal Analytics should look like:

 

 (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-XXXXXX-Y', 'auto');
 ga('set', {
 'dimension1': 'custom dimension data',
 'metric1': 'custom metric data'
 });
 ga('send', 'pageview');

 

* Replace UA-XXXXXX-Y with your property ID and the custom variable settings with your own. Don’t forget to add the opening and closing JavaScript tags.

 

It’s important that the custom variable is set before the send pageview function.

 

Universal Analytics AdWords Remarketing

If you use Google AdWords you’re most probably familiar with the concept of remarketing. With the changes from July 2012 you’re now able to create remarketing lists from the Google Analytics interface – based on metrics like pages viewed, visit duration and conversions. All you need to do is:

  • change one line in your GA tracking code
  • update your privacy policy

Here’s how you need to tweak your Universal Analytics 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-XXXXXX-Y', 'auto');
 ga('require', 'displayfeatures');
 ga('send', 'pageview');

 

 

I know it’s often overwhelming and a lot to go through at once and decide what’s right for your website. So the easy way is to answer these questions:

  • Do you want to adjust bounce rate (recommended)?
  • Do you want to take advantage of enhanced in-page analytics settings?
  • Do you want to use GA remarketing?
  • Do you want to track custom variables?
  • Do you need to track multiple domains (where’s your shopping cart located)?

Check the guide here: Google Analytics Implementation Guide.

Still have questions? You can ask them as comments below and I’ll try to answer.

is a Google Analytics fan, who also loves SEO, conversion optimization, online marketing and social media. You can follow her on Twitter and LinkedIn.

2 thoughts on “Universal Analytics Implementation – Tracking code

Leave a Reply

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