Google Analytics is an excellent service to know users’ behavior on your site. We only have to copy and paste an snippet, and we can know a lot of things . But this service doesn’t tell us how fast/slow our pages are. Performance information is very important as we known.
So, how can we use google analytics as performance monitoring service?
I wrote a tiny script to use eventTrack function of Google Analytics API and track measures of performance. Using this script you can store and analyze the real performance time from your visitors.
Steps
<script>
var _gameasures = [['start',(new Date()).getTime()]];
function addGAMearsure(mark){
_gameasures.push([mark,((new Date()).getTime()-_gameasures[0][1])]);
}
</script>
<script>addMeasure("Perceived Ready Time");function sendGAMeasures(){addMeasure("load");if(_gaq)for(var a=_gameasures.length,b=encodeURI(window.location);a-- >1;)_gameasures.push(["_trackEvent","Performance",b,_gameasures[a][0],_gameasures[a][1]])}if(typeof window.onload=="function"){var prevOnload=window.onload;window.onload=function(){prevOnload();sendGAMeasures()}};</script>
Optional
........
</head>
<body>
<script> addMeasure('Perceived ResponceTime'); </script>
The complete code (feel free to comment):
// mark startTime
var _gameasures = [['start',(new Date()).getTime()]];
//
// Add measure
// @mark string
//
function addMeasure(mark){
_gameasures.push([mark,((new Date()).getTime()-_gameasures[0][1])]);
}
//
// add load event
//
function sendGAMeasures(){
//add final measure
addMeasure('load');
// if _qaq is defined means that google analytics is set ok
if(_gaq){
// set page name
// by default it get the page url, if you want you can change the value of pname
var len = _gameasures.length, pname = encodeURI(window.location);
// send all measures
while(len-- > 1){
_gaq.push(['_trackEvent', 'Performance', pname, _gameasures[len][0], _gameasures[len][1]]);
}
}
}
// set onload event checking if has been set previoully
if (typeof window.onload != 'function') {
_sendGAMeasures;
}else{
var prevOnload = window.onload;
window.onload = function(){
prevOnload();
sendGAMeasures();
};
}
I hope this script help you to monitoring the speed of your site
Martin





Faster sprite download
The new Tip
In the common way to css and sprite loading
body{ background-image:url(sprite.png); }In this case the browser downloads first the style.css, and then the sprite.png.
Try this online demo (the style is delayed for demo purposes ).
Css and sprite parallel downloads
setTimeout(function(){var s = document.createElement('image');(document.getElementsByTagName('head')[0]).appendChild(s);The browser now will have the sprite image faster.
Try the Online Demo.
To summarize
Feel free to comment
Martín