Created
January 4, 2016 15:39
-
-
Save Bnaya/ea9c62bd1412657175f0 to your computer and use it in GitHub Desktop.
Track javascript errors to 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
(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