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.

5 Google Analytics posts you don’t want to miss in May

Here’s again the last post of the month saved for the top 5 interesting Google Analytics related resources I found on other sites. Here are the five for May – you may want to bookmark those as they can be very useful:

Spam Filter Insertion Tool

The first 3 are from Simo Ahava: earlier this month he created a web app to filter the spam referrals we all get. You need to download it, grant Edit access to your GA views and it will create exclude filters automatically for all known spam sites as of this moment.

Data is difficult

Here Simo argues about data quality and explains 3 rules: the rule of data passivity, the rule of data subjectivity and the rule of data scarcity. Check out how to make sure you’re gathering meaningful data.

#GTMtips: How To Get Google Tag Manager Help

This one is short, but contains great guidelines how to properly post your Google Tag Manager – related question on support forums like Google+ Google Tag Manager community, the official forum or Stack Overflow.

YouTube Tracking In Google Analytics & Google Tag Manager

The Lina Metrics guys updated the Youtube tracking with the latest changes in Google Analytics and GTM. It contains a detailed step-by-step instructions how to create the tags in Google Tag Manager.

Google Analytics Training Resources and Tutorials

Here’s a list of over 50 GA resources in all kind of areas: getting started with GA and GTM, specialized tracking, cross-domain tracking, enhanced ecommerce, attribution modelling and GA debugging. All from Himanshu Sharma.

P.S. Got a Google Analytics question? Send it to me and I’ll try to answer it on the blog.

(2015) Navigation summary report explained!

I was having difficulties understanding some of the changes that were made to the Navigation Summary report in Google Analytics lately, specifically the numbers in the Next Page Path section. So here’s the explanation of all the numbers there:

Explorer tab

Explorer and Navigation Summary tabs

Let’s get the main numbers first:

Pageviews and % Exits

The home page of our sample website received a total of 8,817 pageviews and 40.08% Exit rate (3,534 exits). We’re not interested in the bounce rate, as the bounces are included in the exits number.

Navigation Summary

We can switch to the Navigation Summary tab now, and let’s say we’re interested in understanding what % of the people on the home page continued to the Pricing page (/pricing.html)

Navigation Summary report

If we want to calculate how many visitors continued to another page and not exited the site, we’ll get it from 8,817 pageviews – 3,534 exits = 5,283 pageviews to a second page. So 1,002 pageviews to the /pricing.html page will be 19%, but the % Pageviews in the Next Page Path column shows 20.33%. Where’s the difference coming from?

If you export this report to csv you’ll see 4,928 total pageviews for all Next Page Path pages (you can use the Show rows drop down to select up to 500 pages). And this is exactly 20.33% for the /pricing.html page.

But our calculations showed 5,283 pageviews – so where are the missing 355 pageviews? Please note that the difference here is not that big, but it depends on the website and this number can be much higher.

Let’s build a custom report, combining previous page path and next page path, with metric Pageviews (HT to Analytics Edge):

Custom Next Page Path report

You can save the custom report template and use the filter to select the page you want to analyze.

What do you notice here? Our missing 355 pageviews are coming from the cases where the next page path is the same as the page we’re analyzing. This was explained by visitors hitting reload, having images that open in the same window or something else causing the page to reload (generating “self referring” hits).

I’m not sure at what point GA made this change in the Navigation Summary report and started “hiding” the same-page pageviews, but it’s good to know everything adds up.

And to answer my question: 20.33% of visitors to the homepage who continued to another page viewed the Pricing page, so next time I’ll just take the % Pageviews number in the Next Page Path column and won’t think about it.

P.S. Got a Google Analytics question? Send it to me and I’ll try to answer it on the blog.

PadiTrack discontinued – here’s a Google Sheets alternative

I received an email today from PadiCode, the creators of PadiTrack, announcing the closing of this awesome free tool. The discontinuation will be effectively starting on March 15th, 2015 and the product will close down effectively a month later, on the 15th of April 2015.

PadiTrack is/was great for showing historical data of your conversion funnels (and many other things included in their Pro package). As you know, if you create or update your goal funnel steps in GA, they will show you data only going forward. One work-around is to build advanced segments and export the data in Excel or another tool. This is where PadiTrack was extremely useful.

Here’s an alternative of PadiTrack (although not that beautiful), using Google Sheets and the GA Add-on. Open the GA Funnel Builder and save a copy in your account.

There are several fields you need to fill in (colored in yellow) on the first sheet called Dashboard.

1. You need to get your GA View ID – go to Admin – View Settings and copy the number under View ID:

view-id

 

 

2. Enter it in the yellow field next to GA profile.

3. Define your start and end dates, by updating the values in the yellow fields.

step1

4. Enter your funnel steps by using the page URI, just like you’d define them when configuring a goal.

5. Select your condition: exact match, contains or matches regular expression by entering “yes” next to the option you want. The example here is using the default Magento shopping cart process.

6. Select Add-ons – Google Analytics – Run reports.

run-reports

7. Google Sheets will get the GA data and you’ll get a status report like the one below. In my example I have 3 successful reports as I defined 3 steps:

status

8. Your funnel is ready.

funnel

 

 

The final result / visualization could be improved a lot, here’s just the data. Also, segmentation could be easily applied or the number of steps expanded.

Note: PadiTrack is using the Users metric to calculate their conversion funnel, while I’m using Sessions here. If you’d like to show Users, you can modify the metrics in the Report Configuration sheet by changing ga:sessions to ga:users.

Hope this will help you in your conversion funnel building 🙂

If you have questions or comments, please share them below.

Update:

Here’s a separate version of the GA Funnel Builder where first step is mandatory.

P.S. Got a Google Analytics question? Send it to me and I’ll try to answer it on the blog.

Master Google Analytics API with Google Spreadsheets Add-on

In this post we’ll see how to add the Google Analytics Add-on to Google Spreadsheets and take advantage of all cool things you can do with GA API, no coding skills required.

First, we’ll add the Google Analytics Add-on to Google Spreadsheets. Just login to Google Drive and click Create – Spreadsheet.

step1

In your empty spreadsheet select Add-ons – Get Add-ons:

Add add-ons

A pop up will open with the available add-ons, just use the search field in the top right corner to look for google analytics and select one called Google Analytics by clicking the + Free button on the right:

Select the GA add-on

The add-on will ask you for permissions, just scroll down and click on Accept:

Accept permissions request

That’s all! Now you can build your first report with the GA API. Go to Add-ons – Google Analytics – Create new report

Get started configuring your GA report

The tool will help you with the initial setup of your report – you can enter a Name for the report, select the account, property and view you’ll be pulling data from, and choose metrics and dimensions.

Here’s how a test report configuration looks like:

Report configuration settings

By default the tool will select the last 7 days, you can also specify specific start and end dates by removing the value next to Last N Days and enter the dates you want following the YYYY-MM-DD pattern. In this example I want to see the number of sessions for 3 dimensions simultaneously: source / medium, landing page and date. You can also sort, add filters and segments, define max results, etc.

Let’s run our report and see what data we’ll get. Just click on Add-ons – Google Analytics – Run Reports:

Report configuration settings

Google Spreadsheets will think a bit and return a success message when ready:

Successfully run report

And the results (by default sorted by the first column):

GA report is ready!

Of course there’s a lot more to say for this handy tool which I’ll do in future posts.
Here are some really useful links that you should bookmark as you’ll use them a lot for reference:

1. Dimensions and Metrics Reference

2. Configuration parameter reference

3. More details how to build your filters and segments, and sometimes even the Segments Dev guide

Next time we’ll see how to get more insights of our conversion visits and different paths. Stay tuned!

P.S. Got a Google Analytics question? Send it to me and I’ll try to answer it on the blog.