Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging javascript errors within Google Analytics

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:

Google Analytics

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) {
    }
};
like image 751
lisburnite Avatar asked Jul 15 '26 06:07

lisburnite


1 Answers

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.

like image 53
Philip Walton Avatar answered Jul 20 '26 15:07

Philip Walton