本文档将大略介绍如何使用 iOS 版 Google Analytics(分析)SDK v3 对崩溃和异常进行衡量。
概览
崩溃和异常衡量功能可帮助您衡量应用中发生的崩溃和异常的次数及类型。异常包含以下字段:
字段名称 | 跟踪器字段 | 类型 | 必需 | 说明 |
---|---|---|---|---|
说明 |
|
|
否 | 异常的相关说明(最多 100 个字符)。接受 nil 。 |
isFatal |
|
BOOL |
是 | 指示异常是否严重。
YES 表示严重。 |
崩溃和异常数据主要在“崩溃和异常”报告中提供。
捕获的异常
捕获的异常是指您的应用中您为其定义了异常处理代码的那些错误,例如请求数据时偶尔发生的网络连接超时情况。
为了对捕获的异常进行衡量,您应当在跟踪器上设置异常字段值并发送匹配,如下例所示:
/* * 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. NSArray *highScores = [self getHighScoresFromCloud]; } @catch (NSException *exception) { // May return nil if a tracker has not already been initialized with a // property ID. idtracker = [[GAI sharedInstance] defaultTracker]; [tracker send:[[GAIDictionaryBuilder createExceptionWithDescription:@"Connection timeout" // Exception description. May be truncated to 100 chars. withFatal:@NO] build]]; // isFatal (required). NO indicates non-fatal exception. }
衡量未捕获的异常
未捕获的异常表示您的应用在运行时遇到意外情况,这些异常通常是严重的,会导致应用崩溃。
您可以将 trackUncaughtExceptions
属性设置为 YES
,从而自动向 Google Analytics(分析)发送未捕获的异常。例如:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[GAI sharedInstance] setTrackUncaughtExceptions:YES]; return YES; }
在使用自动异常衡量功能时,请注意以下事项:
- 所有使用自动异常衡量功能发送的异常都会在 Google Analytics(分析)中显示为严重异常。
- 默认情况下,说明字段会自动填充以下信息:异常类型、类名称、方法名称和线程名称。