Skip to content

Instantly share code, notes, and snippets.

@kimmobrunfeldt
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save kimmobrunfeldt/26a76e1252bf57f17067 to your computer and use it in GitHub Desktop.

Select an option

Save kimmobrunfeldt/26a76e1252bf57f17067 to your computer and use it in GitHub Desktop.
Google Analytics error reporting skeleton. NOTE THAT THIS DOES NOT WORK!!! This is just an example how to hook to window.onerror
function sendErrorToAnalytics(err) {
console.log('Sending error to analytics', err);
// WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Google analytics exception's exDescription max length is 150 bytes.
// USE ANOTHER EXCEPTION LOGGING SERVICE IF YOU NEED TO LOG E.G. STACKTRACES
ga('send', 'exception', {
exDescription: err.stack,
fatal: false
});
}
var Promise = require('bluebird');
Promise.onPossiblyUnhandledRejection(function(error) {
throw error;
});
// The first 3 parameters are quite standard but some browsers offer the actual
// error object as fifth parameter.
window.onerror = function onGlobalError(message, file, line, column, errorObj) {
var shortDescription = file + '(' + line + '): ' + message;
if (errorObj) {
sendErrorToAnalytics({
stack: shortDescription + '\n' + errObj.stack;
});
} else {
sendErrorToAnalytics({
stack: shortDescription
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment