83

Google Universal Analytics has a hit type of exception

ga('send', 'exception', {
  'exDescription': 'DatabaseError'
});

I was expecting to be able to just go to the Google Analytics console and find an exeption report at the same level as 'events' however it's nowhere to be seen.

The Android and iOS APIs say Crash and exception data is available primarily in the Crash and Exceptions report but I can't find any report by that name.

5 Answers 5

134

Figured it out. I'm not sure why they don't make this a built in report but maybe someday.

I made a custom widget in a dashboard with Exception Description for dimension and 'Crashes' for the metric:

enter image description here

Which gives me a report like this:

enter image description here

You can also go to Customization tab and create a custom report to give you a table of errors, and then add it to your dashboard.

enter image description here

Used with this global exception handler

if (typeof window.onerror == "object")
{
    window.onerror = function (err, url, line)
    {
        if (ga) 
        {
           ga('send', 'exception', {
               'exDescription': line + " " + err
           });
        }
    };
}

You can put this handler anywhere in the initialization of your Javascript - which will depend upon how you have all your JS files configured. Alternatively you can just put it inside a <script> tag near the top of your html body tag.

6
44

I took Simon_Weaver's guide to making a custom report a few steps further and built out a fairly complete Google Analytics custom exceptions report. I figured it might be worth sharing, so I uploaded it to the GA "Solutions Gallery".

My template: Google Analytics Exceptions Report

Here's a picture of the end result:

https://imgur.com/a/1UYIzrZ

5
  • 3
    This has become my favorite report for exception tracking, and I highly recommend others choose this one (until Google Analytics provides a built-in report). Commented Jun 13, 2017 at 18:10
  • 1
    If you ever get a chance to add some screenshots here of how your template works that would be great. Commented Jun 12, 2018 at 3:24
  • @Simon_Weaver I just tried it and went ahead and added what it looks like. Filtered out the actual exception descriptions but they do show up. Hopefully that helps
    – aug
    Commented Nov 17, 2018 at 0:37
  • 1
    This link doesn't work for me - it just leads to the generic Google Marketing Platform site.
    – RocketMan
    Commented Aug 21, 2021 at 0:17
  • You can search for GA reports here: analytics.google.com/analytics/gallery Commented Aug 29, 2021 at 8:08
7

I just wanted to expand a bit on @Simon_Weaver 's excellent answer to provide error reports with a few additional details:

  • Make sure ga() is defined before trying to call it (as an Error could be triggered before the Analytics library is loaded).
  • Log Exception line numbers and column index in the Analytics Reports (although minified JavaScript code used in production might be difficult to read).
  • Execute any previously-defined window.onerror callback.
/**
 * Send JavaScript error information to Google Analytics.
 * 
 * @param  {Window} window A reference to the "window".
 * @return {void}
 * @author Philippe Sawicki <https://github.com/philsawicki>
 */
(function (window) {
    // Retain a reference to the previous global error handler, in case it has been set:
    var originalWindowErrorCallback = window.onerror;

    /**
     * Log any script error to Google Analytics.
     *
     * Third-party scripts without CORS will only provide "Script Error." as an error message.
     * 
     * @param  {String}           errorMessage Error message.
     * @param  {String}           url          URL where error was raised.
     * @param  {Number}           lineNumber   Line number where error was raised.
     * @param  {Number|undefined} columnNumber Column number for the line where the error occurred.
     * @param  {Object|undefined} errorObject  Error Object.
     * @return {Boolean}                       When the function returns true, this prevents the 
     *                                         firing of the default event handler.
     */
    window.onerror = function customErrorHandler (errorMessage, url, lineNumber, columnNumber, errorObject) {
        // Send error details to Google Analytics, if the library is already available:
        if (typeof ga === 'function') {
            // In case the "errorObject" is available, use its data, else fallback 
            // on the default "errorMessage" provided:
            var exceptionDescription = errorMessage;
            if (typeof errorObject !== 'undefined' && typeof errorObject.message !== 'undefined') {
                exceptionDescription = errorObject.message;
            }

            // Format the message to log to Analytics (might also use "errorObject.stack" if defined):
            exceptionDescription += ' @ ' + url + ':' + lineNumber + ':' + columnNumber;

            ga('send', 'exception', {
                'exDescription': exceptionDescription,
                'exFatal': false, // Some Error types might be considered as fatal.
                'appName': 'Application_Name',
                'appVersion': '1.0'
            });
        }

        // If the previous "window.onerror" callback can be called, pass it the data:
        if (typeof originalWindowErrorCallback === 'function') {
            return originalWindowErrorCallback(errorMessage, url, lineNumber, columnNumber, errorObject);
        }
        // Otherwise, Let the default handler run:
        return false;
    };
})(window);

// Generate an error, for demonstration purposes:
//throw new Error('Crash!');

Edit: As @Simon_Weaver duly noted, Google Analytics now has documentation about Exception Tracking (which I should have linked to in my original answer -- sorry, rookie mistake!):

1
  • idk if it's valid to put appName and appVersion in the exception object you send? I believe you have to explicitly set those see here
    – aug
    Commented Nov 14, 2018 at 20:05
1

This is what I came up with so you don't need to include the code everywhere. Just add new ErrorHandler(); to each .js file. This was done for a Chrome Extension, but should work anywhere, I think. I implement the actual ga() stuff in a separate file (hence the app.GA), but you could bake it in here too.

/*
 *  Copyright (c) 2015-2017, Michael A. Updike All rights reserved.
 *  Licensed under the BSD-3-Clause
 *  https://opensource.org/licenses/BSD-3-Clause
 *  https://github.com/opus1269/photo-screen-saver/blob/master/LICENSE.md
 */
// noinspection ThisExpressionReferencesGlobalObjectJS
(function(window, factory) {
    window.ExceptionHandler = factory(window);
}(this, function(window) {
    'use strict';

    return ExceptionHandler;

    /**
     * Log Exceptions with analytics. Include: new ExceptionHandler();<br />
     * at top of every js file
     * @constructor
     * @alias ExceptionHandler
     */
    function ExceptionHandler() {
        if (typeof window.onerror === 'object') {
            // global error handler
            window.onerror = function(message, url, line, col, errObject) {
                if (app && app.GA) {
                    let msg = message;
                    let stack = null;
                    if (errObject && errObject.message && errObject.stack) {
                        msg = errObject.message;
                        stack = errObject.stack;
                    }
                    app.GA.exception(msg, stack);
                }
            };
        }
    }
}));
0
0

You can now find a "Crashes and Exceptions" view under Behavior (if property is created as a "mobile app" in Google Analytics).

Side menu in Google Analytics as of May 2018

5
  • I don't see this in my GA dashboard
    – technomage
    Commented May 30, 2018 at 17:44
  • @technomage Was your GA property created as a "website" or "mobile app"?
    – ajcurtis
    Commented May 30, 2018 at 20:50
  • It was created as a website. I eventually found the raw data, but I had to fashion a custom report for it.
    – technomage
    Commented Jun 15, 2018 at 15:39
  • I'm manually sending "exception" information via GA javascript.
    – technomage
    Commented Jun 15, 2018 at 15:39
  • Ok, that makes sense why you might not see the same view. The screenshot was taken from a property created as a mobile app (also with exceptions sent manually).
    – ajcurtis
    Commented Jun 16, 2018 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.