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
,但我找不到该名称的任何报告。
答案 0 :(得分:113)
想出来。我不确定他们为什么不把它作为内置报告,但也许有一天。
我在信息中心制作了一个自定义小部件,其中Exception Description
用于维度和“崩溃”。对于指标:
这给我一个这样的报告:
您还可以转到Customization
标签并创建自定义报告,为您提供错误表,然后将其添加到信息中心。
与此全局异常处理程序一起使用
if (typeof window.onerror == "object")
{
window.onerror = function (err, url, line)
{
if (ga)
{
ga('send', 'exception', {
'exDescription': line + " " + err
});
}
};
}
您可以将此处理程序放在Javascript初始化的任何位置 - 这取决于您如何配置所有JS文件。或者,您可以将其放在靠近html正文标记顶部的<script>
标记内。
答案 1 :(得分:38)
我使用了Simon_Weaver的指南,进一步制作了自定义报告,并构建了一个相当完整的Google Analytics自定义例外报告。我认为它可能值得分享,因此我将其上传到GA&#34; Solutions Gallery&#34;。
我的模板:Google Analytics Exceptions Report
这是最终结果的图片:
答案 2 :(得分:6)
我只想扩展@Simon_Weaver的优秀答案,提供错误报告以及一些其他细节:
ga()
之前确定window.onerror
(因为在加载Google Analytics库之前可能会触发错误)。/**
* 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!');
回调。{{1}}
编辑正如@Simon_Weaver正确指出的那样,Google Analytics现在提供了有关异常跟踪的文档(我应该在原始答案中将其链接到 - 抱歉,新手错误!):
答案 3 :(得分: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);
}
};
}
}
}));
答案 4 :(得分:0)