本文档将大略介绍如何使用 Android 版 Google Analytics(分析)SDK v4 对崩溃和异常进行衡量。
概览
崩溃和异常衡量功能可帮助您衡量您的应用中发生的崩溃和异常的数量及类型。异常包含以下字段:
字段名称 | 跟踪器字段 | 类型 | 必需 | 说明 |
---|---|---|---|---|
说明 |
|
|
不支持 | 异常的相关说明(最多 100 个字符)。接受 null 。 |
isFatal |
|
boolean
|
是 | 指示异常是否严重。
true 表示严重。 |
崩溃和异常数据主要在“崩溃和异常”报告中提供。
捕获的异常
捕获的异常是指您的应用中您为其定义了异常处理代码的那些错误,例如请求数据时偶尔发生的网络连接超时情况。
为了对捕获的异常进行衡量,您应当在跟踪器上设置异常字段值并发送匹配,如下例所示:
/* * An app tries to load a list of high scores from the cloud. If the request * times out, an exception is sent to Google Analytics */ try { // Request some scores from the network. ArrayListhighScores = getHighScoresFromCloud(); } catch (IOException e) { // May return null if EasyTracker has not yet been initialized with a // property ID. EasyTracker easyTracker = EasyTracker.getInstance(this); // StandardExceptionParser is provided to help get meaningful Exception descriptions. easyTracker.send(MapBuilder .createException(new StandardExceptionParser(this, null) // Context and optional collection of package names // to be used in reporting the exception. .getDescription(Thread.currentThread().getName(), // The name of the thread on which the exception occurred. e), // The exception. false) // False indicates a nonfatal exception .build() ); ... // Display alert to user that high scores are currently unavailable. }
衡量未捕获的异常
未捕获的异常表示您的应用在运行时遇到意外情况,这些异常通常是严重的,会导致应用崩溃。
您可以使用 EasyTracker 或 ExceptionReporter
类将未捕获的异常自动发送到 Google Analytics(分析)。
使用 EasyTracker
如需使用 EasyTracker 自动发送您的应用中所有未捕获的异常,请将下面这行代码添加到您的 analytics.xml
文件中:
<bool name="ga_reportUncaughtExceptions">true</bool>
在使用自动异常衡量功能发送异常后,EasyTracker 会将异常传递到 Thread
的默认异常处理程序。
使用高级实现
如果您使用的是高级实现,而且未使用 EasyTracker,请使用 ExceptionReporter
类来实现自动未捕获异常衡量。
ExceptionReporter
可以充当应用中特定线程或所有线程的默认未捕获异常处理程序。向 Google Analytics(分析)发送异常后,ExceptionReporter
类可以选择将异常传递给您提供的未捕获异常处理程序。
以下代码会创建一个新的 ExceptionReporter
对象,并将其设置为新的默认未捕获异常处理程序。结果,每个未捕获异常都会先被发送到 Google Analytics(分析),然后被传递到该未捕获异常处理程序。对于大多数应用,默认处理程序会将异常记录到日志中,然后终止应用。
UncaughtExceptionHandler myHandler = new ExceptionReporter( GoogleAnalytics.getInstance(this).getDefaultTracker(), // Tracker, may return null if not yet initialized. GAServiceManager.getInstance(), // GAServiceManager singleton. Thread.getDefaultUncaughtExceptionHandler()); // Current default uncaught exception handler. // Make myHandler the new default uncaught exception handler. Thread.setDefaultUncaughtExceptionHandler(myHandler);
在使用自动异常衡量功能时,请注意以下事项:
- 所有使用自动异常衡量功能发送的异常都会在 Google Analytics(分析)中显示为严重异常。
- 默认情况下,说明字段会自动填充以下信息:异常类型、类名称、方法名称和线程名称。
解析异常说明
SDK 提供的 StandardExceptionParser
可简化获取异常说明并将其发送到 Google Analytics(分析)的流程:
// Using StandardExceptionParser to get an Exception description. try { // Request some scores from the network. ArrayListhighScores = getHighScoresFromCloud(); } catch (IOException e) { // May return null if EasyTracker has not yet been initialized with a // property ID. EasyTracker easyTracker = EasyTracker.getInstance(this); easyTracker.send(MapBuilder .createException(new StandardExceptionParser(this, null) // Context and optional collection of package names // to be used in reporting the exception. .getDescription(Thread.currentThread().getName(), // The name of the thread on which the exception occurred. e), // The exception. false) // False indicates a fatal exception .build() ); ... // Display alert to user that high scores are currently unavailable. }
您也可以实现自己的解析器,方法是实现 ExceptionParser
接口,并在向 Google Analytics(分析)发送异常时调用该接口的 setDescription
方法。