Skip to content

Instantly share code, notes, and snippets.

@morkeleb
Created August 31, 2012 11:01
Show Gist options
  • Save morkeleb/3551461 to your computer and use it in GitHub Desktop.
Save morkeleb/3551461 to your computer and use it in GitHub Desktop.
A simple javascript file that attaches to jquerys ajax events and logs metrics based on server performance.
(function() {
$(document).ready(function() {
function saveTime(e, jqxhr, settings) {
settings.startTime = new Date().getTime();
}
function registerTime(e, jqxhr, settings) {
_gaq.push(['_trackEvent', 'AjaxResponseTime', settings.type, settings.url.split('?')[0] ,(new Date().getTime() - settings.startTime), true]);
}
$(document).ajaxSend(saveTime);
$(document).ajaxComplete(registerTime);
$(document).ajaxError(function(event, xhr, settings) {
_gaq.push(['_trackEvent', 'AjaxError', xhr.status || xhr.statusText || 'Unknown', settings.url.split('?')[0],(new Date().getTime() - settings.startTime), true]);
});
window.onerror = function(msg, url, lineno) {
_gaq.push(['_trackEvent', 'JavaScriptError', msg, url+":"+lineno, true]);
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment