I'm trying to get information on JavaScript errors that are occurring within my web application and logging these using events within Google Analytics. I'm doing this as follows:
$(document).ready(function() {
var sendAnalyticsEvent = function(category, action, label) {
if (window.ga) {
window.ga('send', 'event', category, action, label);
}
};
window.onerror = function(error) {
try {
sendAnalyticsEvent('JavaScript Error', error.message, error.filename + ': ' + error.lineno);
} catch(e) {
}
};
$(document).ajaxError(function(e, request, settings) {
try {
sendAnalyticsEvent('Ajax Error', settings.url, e.result);
} catch(e) {
}
});
});
This is successfully firing the events to Analytics for example:

However when I click into 'JavaScript Error' it doesn't give me any other information, for example the error message that should have been sent with the code above.
Any ideas why this isn't sending the error message along with the error?
EDIT
It turns out I was declaring the window.onerror function incorrectly. The following works correctly:
window.onerror = function (message, source, lineno, colno, error) {
try {
sendAnalyticsEvent('js error', message, source + ': ' + lineno);
} catch(e) {
}
};
You're sending the error message as the Event Label dimensions, but that report is looking at the Event Category dimension. If you click on Event Label, you should see it.
That being said, why not use exception tracking instead of event tracking to measure this? Exception tracking was built exactly for this use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With