Skip to content

Instantly share code, notes, and snippets.

@350d
Created September 20, 2013 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 350d/6643530 to your computer and use it in GitHub Desktop.
Save 350d/6643530 to your computer and use it in GitHub Desktop.
Google Analytics Javascript errors tracking
$(window).error(function (error) {
if (error && error.originalEvent) {
error = error.originalEvent;
var message, url, line;
if (error && error.message) {
message = error.message;
url = getfilename(error.filename);
line = error.lineno;
} else {
message = "(message empty)";
url = getfilename(document.location.href);
line = "?";
}
gajserror(message,url,line);
}
return true;
});
window.onerror = function(message, url, line) {
gajserror(message,url,line);
return true;
};
function gajserror(message,url,line) {
_gaq.push(["_trackEvent", "JS Exception Error", message, url + " (" + line + ")", 0, true]);
};
function getfilename(url) {
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
url = url.substring(url.lastIndexOf("/") + 1, url.length);
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment