Thursday, May 19, 2011

Tracking Web Page Load and Site Speed Using Google Analytics

Recently I came across one of the interesting feature "Track Page Load Time" in Google Analytics APIs.

This feature provides the metrics on the Page Load Time within the Site Speed Report. This gives us a good opportunity to identifying slow loading pages and fix them for improving the Search Engine Ranking as load time is one of the factor in the page ranking.

Following is the code snippet for capturing the Page Load Time:

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

Note that by default Site Speed Reports are not enabled. The function call _gaq.push['_trackPageLoadTime']) enables this report.