Skip to content

Instantly share code, notes, and snippets.

@atma
Created April 29, 2015 17:54
Show Gist options
  • Save atma/947aafb440ce735ecae5 to your computer and use it in GitHub Desktop.
Save atma/947aafb440ce735ecae5 to your computer and use it in GitHub Desktop.
Track JavaScript errors with Google Analytics
// jQuery AJAX errors handler
$(document).ajaxError(function (event, request, settings) {
ga(
'send',
'event',
'jQuery Ajax Error',
settings.url,
JSON.stringify({
result: event.result,
status: request.status,
statusText: request.statusText,
crossDomain: settings.crossDomain,
dataType: settings.dataType
}),
0,
true
);
});
// jQuery errors handler
jQuery.error = function (message) {
ga(
'send',
'event',
'jQuery Error',
message,
navigator.userAgent,
0,
true
);
}
// Pure JavaScript errors handler
window.addEventListener('error', function (err) {
var lineAndColumnInfo = err.colno ? ' line:' + err.lineno +', column:'+ err.colno : ' line:' + err.lineno;
ga(
'send',
'event',
'JavaScript Error',
err.message,
err.filename + lineAndColumnInfo + ' -> ' + navigator.userAgent,
0,
true
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment