Skip to content

Instantly share code, notes, and snippets.

@dkrnl
Last active January 28, 2016 06:01
Show Gist options
  • Save dkrnl/44286c3c9e5b1e1dd2d4 to your computer and use it in GitHub Desktop.
Save dkrnl/44286c3c9e5b1e1dd2d4 to your computer and use it in GitHub Desktop.
Track JS errors VIA Google Analitics
<script>
{% textstrip %}
var _gaq = window._gaq || [];
var trackError = function(msg, url, line, column, error, action) {
if (!(error !== undefined && error.stack)) {
error = function() { try { throw Error(msg) } catch(e) { return e; } }();
}
var report = (error !== undefined && error.stack ? String(error.stack).replace(/\n\s*/g, ' --> ') + ' message:' + msg : msg) + ' at ' + url + ':' + line + ':' + column + ' date:' + Date();
_gaq.push(['_trackEvent', action, report, '#{{ request.user.id|default:"0" }} ip:{{ request.META.REMOTE_ADDR|escapejs }} ua:' + window.navigator.userAgent + ' error:' + report, 0, true]);
if (typeof(console) != 'undefined') {
console.error(report);
} else if(action == 'JS Error') {
alert(report);
throw error;
}
};
window.onerror = function(msg, url, line, column, error) {
trackError(msg, url, line, column, error, 'JS Error');
return true;
};
jQuery.error = function (msg) {
trackError(msg, document.location, undefined, undefined, undefined, 'jQuery Error');
};
{% endtextstrip %}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment