报告Google Analytics analytics.js异常跟踪的例外情况

问题描述 投票:70回答:5

Google Universal Analytics有一个hit type of exception

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

我希望能够直接进入Google Analytics控制台并找到与“事件”处于同一级别的异常报告,但是无处可见。

Android和iOS API说Crash and exception data is available primarily in the Crash and Exceptions report,但我找不到任何名称的报告。

exception-handling google-analytics
5个回答
113
投票

弄清楚了。我不确定他们为什么不把它作为内置报告,但也许有一天。

我在仪表板中创建了一个自定义小部件,其中Exception Description用于维度,“Crashes”用于度量标准:

这给我一个这样的报告:

您还可以转到Customization选项卡并创建自定义报告以提供错误表,然后将其添加到仪表板。

与此全局异常处理程序一起使用

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

您可以将此处理程序放在Javascript初始化的任何位置 - 这取决于您如何配置所有JS文件。或者,您可以将它放在靠近html body标签顶部的<script>标签内。


38
投票

我采用了Simon_Weaver的指南,进一步制作了自定义报告,并构建了一个相当完整的Google Analytics自定义例外报告。我认为可能值得分享,所以我将其上传到GA“解决方案库”。

我的模板:Google Analytics Exceptions Report

这是最终结果的图片:

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


6
投票

我只想扩展一下@Simon_Weaver的优秀答案,提供错误报告以及一些额外的细节:

  • 确保在尝试调用之前定义了ga()(因为在加载Analytics库之前可能会触发错误)。
  • 在Analytics报告中记录异常行号和列索引(尽管生产中使用的缩小的JavaScript代码可能难以阅读)。
  • 执行任何先前定义的window.onerror回调。
/**
 * 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!');

编辑:正如@Simon_Weaver正确指出的那样,谷歌分析现在有关于异常跟踪的文档(我应该在我的原始答案中链接到 - 抱歉,菜鸟错误!):


1
投票

这就是我想出来的,所以你不需要在任何地方都包含代码。只需将new ErrorHandler();添加到每个.js文件即可。这是针对Chrome扩展程序完成的,但我认为应该可以在任何地方使用。我在一个单独的文件(因此app.GA)中实现了实际的ga()内容,但你也可以在这里烘焙它。

/*
 *  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
投票

您现在可以在行为下找到“崩溃和例外”视图(如果在Google Analytics中将属性创建为“移动应用”)。

Side menu in Google Analytics as of May 2018

© www.soinside.com 2019 - 2024. All rights reserved.