Skip to content

Instantly share code, notes, and snippets.

@schfkt
Last active November 18, 2021 21:54
Show Gist options
  • Save schfkt/73602f2e2e95d346f4d2 to your computer and use it in GitHub Desktop.
Save schfkt/73602f2e2e95d346f4d2 to your computer and use it in GitHub Desktop.
Track JavaScript errors using Universal Analytics from Google.
function trackJavaScriptError(e) {
var ie = window.event || {};
var message = e.message || ie.errorMessage || '<no message>'
var filename = e.filename || ie.errorUrl || '<no filename>';
var lineno = e.lineno || ie.errorLine || '<no lineno>';
var colno = e.colno || ie.errorColumn || '<no colno>';
var source = filename + ':' + lineno + ':' + colno;
ga('send', 'event', 'JavaScript Exception', message, source, {'nonInteraction': 1});
}
if (window.addEventListener) {
window.addEventListener('error', trackJavaScriptError, false);
} else if (window.attachEvent) {
window.attachEvent('onerror', trackJavaScriptError);
} else {
window.onerror = trackJavaScriptError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment