Track Drupal page load times easily with the Google Analytics module
Track Drupal page load times easily with the Google Analytics moduleTuesday, 14th May 2013
If you're a Drupal developer who's worked on a project with any real complexity I'm sure you've experienced some (or maybe a lot) of slow pages due to some kind of performance bottleneck in your code. Drupal is flexible enough that there's usually quite a few options available to help speed up slow pages, from the "one click" caching solutions provided by core, to third party cache tools like Entity Cache, through to targeted profiling and code refactoring on the level of individual functions with a dedicated PHP tool like xhprof.
In this post I'm not going to discuss fixing performance issues; I just want to talk about an easy way to detect that you have a problem in the first place.
So many times when I've heard somebody complain about a slow site (or section within a site) I see them click around a bit, maybe the cache kicks in and then they say "oh, it's not so bad" or maybe part of the site is only slow in a particular browser or maybe mobile devices are hanging on that cool javascript animation you wrote or maybe your audience doesn't live in the same country as your server. Whatever the situation, the performance you should be interested in is not what you or your clients see on a development setup but the page load times the users of your production site are experiencing.
Did you know that Google Analytics tracks page load times out of the box?
Well... not quite. "Out of the box" Google Analytics only tracks the page load times of 1% of your total page impressions. From Google's own API documentation:
"If you have a relatively small number of daily visitors to your site, such as 100,000 or fewer, you might want to adjust the sampling to a larger rate."
If you're anything like me though, you'll probably want to start weeding out slow pages long before your traffic is averaging anywhere near 100,000 hits a day - unless you like thrashing your server and making your users wait. For a new site that 1% might translate to just a handful of data points that are unlikely to be related or help diagnose anything, really.
Since the sample rate is adjustable we can just continue to push it up until we get meaningful data - Google's daily cap is 10k so if you're getting fewer hits than that each day you can safely push the sample rate right to 100%.
Configuring the Google Analytics Site Speed sample rate
Unfortunately the sample rate can't be set through the Google Analytics UI, we need to add the following line to the tracking script embedded in the site we want to track:
// Sets the Google Analytics Site Speed sampling rate to 100%.
_gaq.push(['_setSiteSpeedSampleRage', 100]);
If you haven't worked with the Google Analytics API directly before even a relatively simple snippet like this could be confusing out of context (you might be wondering what _gaq
is, for example?)
Luckily the Drupal Google Analytics module has a convenient way to handle dropping in custom GA snippets like this one into the right place for you.
On the Google Analytics module's configuration page, inside the "Advanced settings" fieldset, open the "Custom JavaScript code" fieldset and paste the above snippet into the textfield labeled "CODE SNIPPET (BEFORE)". Save the form and you should be done!
Like most changes to Google Analytics, new data will start tracking immediately but won't become available in a useful format in the dashboard for about 24 hours.
To find the site speed statistics in the Google Analytics UI navigate to Content->Site Speed
through the left hand side bar. If you haven't configured the sample rate as per the instructions above this section might look completely "broken", depending on your current traffic levels.
Now that you've tweaked your sample rate for your site, you might want to read up on how to interpret your awesome new data.
Syndicate: planet drupal