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

Sim*_*ver 74 google-analytics exception-handling

Google Universal Analytics有一种特殊类型

ga('send', 'exception', {
  'exDescription': 'DatabaseError'
});
Run Code Online (Sandbox Code Playgroud)

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

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

Sim*_*ver 120

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

我在仪表板中创建了一个自定义小部件,其中包含Exception Description维度和"崩溃"指标:

在此输入图像描述

这给我一个这样的报告:

在此输入图像描述

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

在此输入图像描述

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

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

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

  • “崩溃”指标对我不起作用。但是这个自定义报告有效 https://imgur.com/a/Ux57LEE 我正在使用 gtag (3认同)
  • 哦,这个问题是四年前问的...他们还没有改善它:( (3认同)
  • ***gtag.js*** ?https://developers.google.com/analytics/devguides/collection/gtagjs/exceptions?hl=es-419 (2认同)
  • 这也不会出现在实时报告中。太遗憾了。 (2认同)

rwd*_*sco 41

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

我的模板:Google Analytics Exceptions Report

这是最终结果的图片:

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

  • 这已成为我最喜欢的异常跟踪报告,我强烈建议其他人选择此报告(直到Google Analytics提供内置报告). (3认同)

Phi*_*cki 7

我只想扩展一下@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!');
Run Code Online (Sandbox Code Playgroud)

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