• Follow us on Twitter
  • Join our Facebook Group
  • Join me on Google Plus
  • Add me on Linkedin
  • RSS

  • Home
  • Services
  • Tools & Downloads
    • Google Analytics Content Targeting Generator
    • A/B/n Split Test Confidence Calculator with Graphing
    • Simple Feedback Form with Google Analytics Integration
  • About
  • Contact

Blog - Latest News

Track Page Load Times with Google Analytics’ Asynchronous Script

10 Comments/ in Analytics / by Robert Kingston
July 10, 2010

Did you know page load times have been shown to impact conversion rates, revenue and other metrics on your site? Here’s some stats:

  • Increasing the size of search results pages from 10 to 30 results increased page load by 500 milliseconds and decreased Google’s revenue by 20%
  • Every 100 milliseconds of extra load time impacts Amazon’s revenue by 1%

For more information on how much load times can cost you, read my article over at Experian Hitwise.

Want to measure page load times on your site to see the impact it has on revenue, conversion rates and other metrics? Well I’ve found a way to do it based on a great article from Panalysis, along with my own tweaks (mostly to suit it to my purposes and modify the code for the updated tracking).

Overview

My modified solution for tracking page load times requires the following things:

  • A website – preferably with enough traffic from different countries or high enough server load to affect page load times
  • Google Analytics’ new asynchronous tracking script installed and functioning on the site
  • Modifying the existing Google Analytics code to work with two tracking objects (to avoid messing up the bounce rate calculation)
  • A second Google Analytics profile setup with a different UA code to your website’s main page
  • A fair knowledge/passion for Google Analytics

So without further ado, here’s how to do it.

Installing and customizing the code

Before doing this, you must update your existing Google Analytics code. This is important as we’re going to use two tracking objects on the site – one for the normal tracking of your site and another for the tracking of page load times. I suggest pre-pending “pageTracker” to any calls to ._trackPageview, ._setAccount or any others. If you were to use the same object for both your normal tracking and the load time tracking, your bounce rate metric would be impacted.

So instead of this:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['._setAccount', 'UA-15373241-1']);
  _gaq.push(['._trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
  })();
</script> 

Your tracking code will look like this:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['pageTracker._setAccount', 'UA-15373241-1']);
  _gaq.push(['pageTracker._trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
  })();
</script> 

Once that’s done, you need to place the following code right at the top of the document’s header (this collects the time that the document started to load):

<script type="text/javascript">
var plstart = new Date();
</script> 

Next, you’ll need to place this code somewhere after the Google Analytics script. This handles the logic and event tracking for the page load times. It’s important that you add your UA code to this section before pasting it on your website:

<script type="text/javascript">
window.onload=function() {
var plend = new Date();
var plload = plend.getTime() - plstart.getTime();
if(plload<1000)
lc = "Very Fast";
else if (plload<2000)
lc = "Fast";
else if (plload<3000)
lc = "Medium";
else if (plload<5000)
lc = "Sluggish";
else if (plload<10000)
lc = "Slow";
else
lc="Very Slow";
var fn = document.location.pathname;
if( document.location.search)
fn += document.location.search;
try {
_gaq.push(['loadTracker._setAccount', 'UA-15373241-2']);
_gaq.push(['loadTracker._trackEvent','Page Load (ms)',lc + ' Loading Pages',fn,plload]);
_gaq.push(['loadTracker._trackPageview']);
} catch(err){}
}
</script>

Here, I have updated the code slightly, so that it records the time down to milliseconds. So, when you see 4,345 as the page load time in Google Analytics, that is actually 4.345 seconds. I’ve also lowered the times in the load category – in general I think it’s best practice to have pages loading within two seconds.

I’ve also updated the code to track a pageview and record the page a visitor is on, so the second profile has all the pages in the reports for you to measure goals with.

Tracking goals and ecommerce transactions

If you want to setup goals and ecommerce tracking for this, you may need to add code to report transactions and page views to “loadTracker”.

You’ll also need to make sure the profile is setup with all the right filters and settings to track everything properly.

Example reports

So, assuming everything has gone well, you should now be able to see the following reports in Google Analytics.

I suggest setting up advanced segments to make the most of this – this will let you look at people with fast load times and see how their conversion rate differs from people with a slow page load time.

I also suggest excluding “Very Slow” if you want to get a good indication of your page load times for the average user – this will remove any outliers which are skewing your average. In my case, 7 “outliers” increased my page load time from 1.7 seconds to 3.4 seconds. BIG difference.

Wishes for the future

What I really wish Google Analytics could do is allow you to filter events – this would make the whole implementation much easier and allow you to customise the data sooo much more before it even reaches the reports. At the very least GA should not count Events against the bounce rate metric. Another really useful functionality to have in the GA reports interface would be to graph the average event value over time of day and day of the week.

Anyway, I hope that was useful. Let me know in the comments how you found it or if you need help.

← Track If People Like Your Blog Posts in Google Analytics
When Testing is a Waste of Time and Resources →
Comments

10 Responses to Track Page Load Times with Google Analytics’ Asynchronous Script

  1. Hamish says:
    September 22, 2010 at 08:39

    Nice post Rob. I’ve been looking at including the page load time in some tracking, so this is good information. You mention the annoyance of events affecting bounce, this and the fact events can’t be used for goals are probably to two of the more annoying things with GA.

    Out of interest, have you tried the load time in a page level variable instead?

    Reply
  2. Rob Kingston says:
    September 25, 2010 at 13:33

    I’ve thought about setting it as a page level variable but that would mean the tracking beacon wouldn’t be sent if an image is still loading while the visitor moves onto the next page.

    i.e.

    Page downloads
    DOM is ready
    Visitor sees links to click or decides to bounce
    Visitor leaves while elements on the page are still loading
    Page still not considered to be fully loaded (by the tracking) – custom variable is not set and no page view is tracked

    Regardless, the data that is returned is really interesting. I might make a post about it to share some of the details…

    Reply
  3. Al says:
    October 13, 2010 at 00:35

    Thanks for this Rob. I’ve been tinkering with similar scripts all morning and this is the only one that has worked for me.

    I have some long landing pages that I want to measure, so what would I amend so that I could measure and compare the load times of specific pages?

    Thanks

    Al

    Reply
  4. Rob Kingston says:
    October 13, 2010 at 18:23

    Hi Al,

    Thanks – I’m glad it’s helped. You shouldn’t need to amend anything. Just go into Analytics and follow these links to find load times by page:

    Content > Event Tracking > Labels

    Here, you’ll see approximate load times in milliseconds (4,384.67 which corresponds to 4.38 seconds) for each page on your site.

    The only thing that I think you’ll need to consider (if you haven’t already) is that since you want to measure landing pages, it’s going to be very important that you use two separate Google Analytics profiles (i.e. with different UA numbers and all) because the last thing you want to see is a 0% bounce rate on your landing pages.

    Does that answer your question?

    Reply
  5. Al says:
    October 15, 2010 at 18:36

    Rob, me again!

    Like you say, clicking on ‘labels’ provides the info that I need – thanks.

    One issue that I’ve got though…

    On my main Analytics profile I’ve noticed a big jump in visitors since I implimented this code. When I look at hits on content though, that’s remained constant. I assume that when people are clicking to another page, they’re being classed as a new visitor. Do you know what would cause this?

    Thanks

    Al

    Reply
    • Rob Kingston says:
      October 17, 2010 at 08:59

      Hi Al,

      I’ve noticed similar. As far as I know, it looks like some visitors do and don’t trigger the analytics code. E.g. I’m seeing a 1.2% higher number of visits in one profile and 2% less on another site I track load times on. How about you?

      Note the other site that was lower did not use trackPageview for loadTracker for a short while…

      Reply
  6. rory says:
    January 11, 2011 at 13:59

    thanks man, this is super helpful! you’re a genius :)

    Reply
  7. Rob Kingston says:
    January 11, 2011 at 17:23

    Thanks, Rory – Glad you liked it.

    I can’t take full credit for this, I have updated the code from Panalysis and added my own tweaks: http://www.panalysis.com/tracking-webpage-load-times.php

    Reply

Trackbacks & Pingbacks

  1. Temps de chargement et Google Analytics says:
    June 30, 2011 at 17:08

    [...] La seconde solution est plus lourde, mais je la trouve beaucoup plus fiable. On va faire appel au suivi d'évènements et à l'ajout de deux codes javascripts qui vont permettre le suivi du temps de chargement de la page. La solution d'origine vient d'OptimisationBeacon. [...]

    Reply
  2. Track site speed with Google Analytics events • André Scholten says:
    March 30, 2011 at 18:54

    [...] I'm going to explain will track page load time and page render time per individual URL. I know some other articles that describe a comparing technique but I really think the way I use it gives you more [...]

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Rob

I'm an ex-Hitwise Analyst turned freelancer that helps clients turn their analytics data into better converting websites. I also like to run, drink, eat and be merry. My girlfriend, on the other hand, would say I play too many computer games and use my Androids too much (yeah, right :P).

Subscribe For More Great Tips


Subscribe by RSS


Follow me on Twitter


Follow Optimisation Beacon on Google+

Categories

  • Analytics
  • Conversion Rate Optimisation
  • Marketing & Consumer Insight
  • News
  • Testing

Most Popular Posts

  • Google Analytics’ New Site Speed Report Tracks Page Load Times
  • Track WordPress Blog Comments in Google Analytics
  • Build a Conversion Rate Heatmap by Hour & Day of Week in Google Docs
  • Track Page Load Times with Google Analytics’ Asynchronous Script
  • See Google Website Optimizer (MVT) Tests in Google Analytics

Recent Posts

  • Web Browsers Most Susceptible to Browser Fingerprinting
  • Cohorts.js: Open Source JavaScript MVT Split Testing Framework
  • 10 Sources of Direct Traffic & How To Track Direct Shares
  • Track What Visitors Copy From Your Site in Google Analytics
  • Track How Far Your Users Scroll in Google Analytics

Blogroll

  • Analytics Ninja
  • Justin Koro
  • LunaMetrics Blog
  • Marketing Experiments
  • SnowPlow Analytics
  • Teacup Marketing
  • Visitor Centric CRO Community
  • Home
  • Services
  • Tools & Downloads
    • Google Analytics Content Targeting Generator
    • A/B/n Split Test Confidence Calculator with Graphing
    • Simple Feedback Form with Google Analytics Integration
  • About
  • Contact
417Follower294Subscribers
© Copyright - Optimisation Beacon - Wordpress Theme by Kriesi.at