We’ve come up with a great way to use Google Analytics to automatically track the loading time of individual pages on a web site using Google Analytics and a bit of JavaScript. After following the steps below, you’ll be able to track the very same page load times each and every one of your trackable users are encountering and you’ll also be able to determine which of your pages are the worst offenders.
Note: Since, for one reason or another, the traditional non-asynchronous Google Analytics code is still in use across a majority of websites, that is what will be covered in this post. Accordingly, the code examples shown below are based on the traditional Google Analytics tracking code.
Update: We’ve just published an update to this article dealing with the new asynchronous version of the Google Analytics code! Go ahead and read it!
The problem many have encountered in trying to implement this sort of solution was that once the load time tracking script had been added and Event Tracking enabled, Google Analytics would stop tracking the website’s bounce rate, which would eventually be reported as 0.00%. This is a highly sub-optimal state of affairs.
The trick to solving this problem is to use a separate tracking profile to handle the Event Tracking. Once isolated from the Event Tracking, the primary account will continue reporting bounce rates and the page load times will be reported in a secondary account.
A great thing about this approach to page-load time tracking is that you can let your visitors do all the work, instead of feverishly refreshing your Firefox and checking YSlow for Firebug each time you want to know how long your webpage has taken to load. No, just follow these steps and check Google Analytics in a few hours; you’ll be able to get a much better handle on your load times once you’re done.
<span style="color: #999999;">try{ var pageTracker = _gat._getTracker("UA-XXXXXXX-X") pageTracker._setDomainName("none"); pageTracker._setAllowLinker(true); pageTracker._setAllowHash(false); pageTracker._link(); pageTracker._linkByPost(); pageTracker._trackPageview();</span> <strong> var secondTracker = _gat._getTracker("UA-XXXXXXX-Y") secondTracker._trackPageview();</strong> <span style="color: #999999;">} catch(err) {}</span>
If your site has an e-commerce component which you are tracking with Google Analytics’ e-commerce functions, you will want to ignore the previous box and instead insert the same code into the area greyed-out in the box below:
<span style="color: #999;">pageTracker._trackTrans();</span> <strong>var secondTracker = _gat._getTracker("UA-XXXXXXX-Y"); secondTracker._trackPageview();</strong><span style="color: #999;"> } catch(err) {}</span>
You will want to have your technical services department or web development contractor ensure that this will not interfere with your e-commerce tracking.
<script type="text/javascript" src="http://<strong>www.yoursite.com/folder/</strong>time-tracker.js"> </script> <script type="text/javascript"> var timeTracker = new TimeTracker(); timeTracker._recordStartTime(); function getPageLoad(){ timeTracker._recordEndTime(); timeTracker._track(secondTracker, 'Page load time', document.location.pathname); } window.onload = getPageLoad; </script>
Enjoy your page load stats!
Jordan Louis
Web Developer
PublicInsite Web Analytics Inc.
Contact us to learn more about what PublicInsite can do for you.
Jordan Louis is a Research Coordinator at Cardinal Path. Jordan splits his time between checking prospective and actual clients' Google Analytics implementations for potential issues, and staring long and hard into analytics data until some insights start staring back. He's also been known to occasionally come up with Rube Goldberg-esque custom tracking code in JavaScript, jQuery and sometimes PHP. Jordan also builds dashboards for clients in either Next Analytics or Shufflepoint, as well as Voice of Visitor surveys with FluidSurveys. Jordan lives with his wife, cat, and dachshund in the dreary capital of Canada, Ottawa. He joined Cardinal Path as part of PublicInsite. Jordan likes cashews and bacon, but not simultaneously. Yet.
One of the most common questions we get about GA4 isn’t really about GA4, itself.…
Using engagement data to improve website performance is a near-universal use-case for users of Google…
Google announced on April 23 that it will again delay third-party cookie deprecation (3PCD) in…
This website uses cookies.