Report for exceptions from Google Analytics analyt

2019-01-16 03:09发布

问题:

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.

回答1:

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:

Which gives me a report like this:

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.

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.



回答2:

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:



回答3:

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!):

  • https://developers.google.com/analytics/devguides/collection/analyticsjs/exceptions
  • https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#exception


回答4:

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);
                }
            };
        }
    }
}));


回答5:

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