Last active
November 4, 2018 11:27
-
-
Save c3ry5/5781740 to your computer and use it in GitHub Desktop.
crossbrowser tracking js errors using google analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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