クラッシュと例外 - Android SDK

このドキュメントでは、Android 向け Google アナリティクス SDK v4 を使ったクラッシュと例外の測定(高度な内容)について概要を説明します。

概要

クラッシュと例外の測定では、アプリで発生するキャッチ済みおよびキャッチされないクラッシュと例外の数と種類を測定できます。例外には次のフィールドがあります。

フィールド名 トラッカー フィールド タイプ 必須 説明
説明 Fields.EX_DESCRIPTION String × 例外の説明で、最大 100 文字です。null を使用できます。
isFatal Fields.EX_FATAL 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.
  ArrayList highScores = 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 アナリティクスに自動的に送信できます。

EasyTracker の使用

EasyTracker を使用してアプリでキャッチされなかったすべての例外を自動的に送信するには、次の行を analytics.xml ファイルに追加します。

<bool name="ga_reportUncaughtExceptions">true</bool>

自動例外測定を使用して例外を送信すると、EasyTracker は例外を Thread のデフォルトの例外ハンドラに渡します。

高度な実装

高度な実装を使用していて EasyTracker を使用しない場合は、キャッチされない例外の自動測定を実装するには、ExceptionReporter クラスを使用します。

ExceptionReporter は、アプリ内の特定のスレッドまたはすべてのスレッドで、キャッチされない例外のデフォルト ハンドラとして機能します。Google アナリティクスに例外を送信した後、ExceptionReporter クラスは必要に応じて、指定した捕捉されていない例外ハンドラに例外を渡すことができます。

次のコードは、新しい ExceptionReporter オブジェクトを作成し、捕捉されなかった新しいデフォルトの例外ハンドラとして設定します。その結果、捕捉されなかった例外はすべて Google アナリティクスに送られて、捕捉されなかった例外の以前のハンドラに渡されます。ほとんどのアプリでは、デフォルト ハンドラがその例外をログに記録して、アプリを停止します。

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 アナリティクスで致命的な例外としてレポートされます。
  • Description フィールドは、例外の種類、クラス名、メソッド名、スレッド名を使ってデフォルトで自動的に設定されます。

例外説明の解析

SDK には、例外の説明を取得して Google アナリティクスに送信するプロセスを簡素化するための StandardExceptionParser が用意されています。

// Using StandardExceptionParser to get an Exception description.
try {

  // Request some scores from the network.
  ArrayList highScores = 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 アナリティクスに例外を送信するときに、その setDescription メソッドを呼び出します。