Skip to content

Instantly share code, notes, and snippets.

@c3ry5
Last active November 4, 2018 11:27
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 c3ry5/5781740 to your computer and use it in GitHub Desktop.
Save c3ry5/5781740 to your computer and use it in GitHub Desktop.
crossbrowser tracking js errors using google analytics
window.onerror = (message, filename, lineno, colno, error) => {
function stack() {
return (`${(error.stack).replace(/(\r\n|\n|\r)/gm," ")} in ${navigator.userAgent}`)
}
function nostack() {
return (`${message} on line ${lineno} for ${filename} in ${navigator.userAgent}`)
}
function submitToGa(msg) {
_gaq.push(["error._setAccount", "XX-XXXXXX-XX"]);
_gaq.push(['error._trackEvent', 'Error', 'JS', msg]);
if (window.console && console.log) {
console.log(msg);
}
}
if (error) {
if (error.stack) {
submitToGa(stack());
} else {
submitToGa(nostack())
}
} else {
submitToGa(nostack())
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment