Skip to content

Instantly share code, notes, and snippets.

@cweekly-yottaa
Created February 22, 2011 21:35
Show Gist options
  • Save cweekly-yottaa/839458 to your computer and use it in GitHub Desktop.
Save cweekly-yottaa/839458 to your computer and use it in GitHub Desktop.
Google Analytics JavaScript enhancement: Push Web Timing Performance Data into GA custom vars
<html>
<head>
<script type="text/javascript">var yoHeadTime = new Date().getTime();</script>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
(function() {
var reportTimeSegment = function(index, name, start, end, scope) {
if( (start && end) && (end-start > 0) ) {
var distance = Math.round( (end-start)/100 )*100;
_gaq.push(["_setCustomVar", index, name, distance, scope]);
}
};
var onLoadHandler = function() {
var onLoadTime = new Date().getTime();
reportTimeSegment(1, "headToOnLoad", yoHeadTime, onLoadTime, 3);
var webTiming = ( window.performance ||
window.mozPerformance ||
window.msPerformance ||
window.webkitPerformance );
if( webTiming && webTiming.timing ) {
var navTime = webTiming.timing.navigationStart || null;
var responseEndTime = webTiming.timing.responseEnd || null;
reportTimeSegment(2,"navToResponseEnd", navTime, responseEndTime, 3);
reportTimeSegment(3,"navToOnLoad", navTime, onLoadTime, 3);
}
_gaq.push(["_setAccount","UA-xxxxxxxx-yy"],
["_trackPageview"]);
};
if(!window.addEventListener) {
window.attachEvent("onload", onLoadHandler);
} else {
window.addEventListener("load", onLoadHandler, false);
}
var ga = document.createElement('script');
ga.type = "text/javascript"; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);
})();
//]]>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment