비정상 종료 및 예외 - Android SDK

이 문서에서는 Android v3용 Google 애널리틱스 SDK를 사용하여 비정상 종료 및 예외 측정을 개략적으로 설명합니다.

개요

비정상 종료 및 예외 측정을 사용하면 앱에서 발생하는 비정상 종료 및 포착되지 않은 비정상 종료와 예외의 수와 유형을 측정할 수 있습니다. 예외의 필드는 다음과 같습니다.

필드 이름 추적기 필드 유형 필수 설명
설명 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 애널리틱스에서 심각한 것으로 보고됩니다.
  • 기본적으로 설명 필드는 예외 유형, 클래스 이름, 메서드 이름, 스레드 이름을 사용하여 자동으로 설정됩니다.

파싱 예외 설명

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.
}

Google 애널리틱스로 예외를 전송할 때 ExceptionParser 인터페이스를 구현하고 setDescription 메서드를 호출하여 자체 파서를 구현할 수도 있습니다.