Skip to content

Instantly share code, notes, and snippets.

@Bnaya
Created January 4, 2016 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bnaya/ea9c62bd1412657175f0 to your computer and use it in GitHub Desktop.
Save Bnaya/ea9c62bd1412657175f0 to your computer and use it in GitHub Desktop.
Track javascript errors to google analytics
(function () {
'use strict';
var duringErrorTrack = false;
var ua = 'UA-XXXXXX-X'
// don't break old browsers
if (!window.addEventListener) {
return;
}
window.addEventListener('error', function (e) {
// Prevent theoretical error handle recursion
if (duringErrorTrack) {
return;
}
duringErrorTrack = true;
if (!window.ga) {
// Load analytics on demand if its not yet available on the page
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
}
window.ga('create', ua, 'auto', {'name': 'jsErrors'});
window.ga('jsErrors.send', 'event', 'DEBUG: js error', 'message:' + e.message, 'location:' + e.filename + ':' + e.lineno + ':' + e.colno);
duringErrorTrack = false;
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment