Simulate Googles trackPageLoadTime without sampling

A while ago I wrote an article about a method to track page load times in Google Analytics. Short after this article Google came with their own technique to track page load times, but both methods have some disadvantages.

To give a clear understanding about the differences I want to show you this image:

This image shows all steps from the beginning of a request for a page to the end. The timing of all these steps are stored in a global javascript object called "performance". And with a simple script you can read out all these timings to calculate the load time. This comes directly from the Navigation Timing specification of the W3C.

The green line is the timing I measure in the above mentioned article, that means: only the time from when the HTML starts loading until the onload event happens.

The red line is the timing Google uses to measure page load time in Google Analytics. A much more trustworthy number because it includes server response times. But Google only uses a sample of this data to calculate page load times.

The best way is a bit of both: the more trustworthy measurement method of Google but without the sampling. That can be done with a simple addition to the Google Analytics tracking script:

if (typeof performance == "object")
  var loadtiming = parseInt(performance.timing.loadEventStart – performance.timing.fetchStart);
  if ((loadtiming > 0) && (loadtiming < 40))   {     _gaq.push(['timer._setAccount', 'UA-XXXXXX-X'], ['timer._trackPageview'], ['timer._trackEvent','w3c-navigationtiming',location.pathname,,loadtiming]);   } }

You can execute this code after the onload so visitors won't be slowed down and be sure to use a NEW Google Analytics profile, and not your main profile, it will ruin bouncerate measurements. The loadEvenStart and fetchStart are also the ones Google used in their Google Analytics trackPageLoadTime functions.

Now there's only a small disadvantage with this script: not all browser are supporting this new "performance" object, right now only Google Chrome supports it. But as Site Speed is becoming more and more important I suspect that other browsers will add it soon.

Click to activate social bookmarks