고급 구성 - Android SDK v2 (기존)

이 문서에서는 Android v2용 Google 애널리틱스 SDK의 고급 구성 기능을 간략하게 설명합니다.

개요

Android용 Google 애널리틱스 SDK는 두 가지 클래스를 사용하여 구현의 전역 상태를 관리하고 Google 애널리틱스 서버로 데이터를 전송합니다. EasyTracker는 이러한 클래스를 래핑하여 구성 및 세션 관리를 단순화합니다.

  • GoogleAnalytics - Tracker 객체를 가져오는 작업을 포함하여 구현의 전역 상태를 처리하는 싱글톤입니다.
  • Tracker: Google 애널리틱스로 데이터를 전송하는 데 사용하는 클래스입니다.

예를 들어 간단한 Activity의 뷰를 측정하는 방법은 다음과 같습니다.

import android.app.Activity

import com.google.analytics.tracking.android.GoogleAnalytics
import com.google.analytics.tracking.android.Tracker

/**
 * A simple Activity that sends a screen view to Google Analytics
 * when it is displayed to the user.
 */
public class HomeScreen extends Activity {

  private Tracker mGaTracker;
  private GoogleAnalytics mGaInstance;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the GoogleAnalytics singleton. Note that the SDK uses
    // the application context to avoid leaking the current context.
    mGaInstance = GoogleAnalytics.getInstance(this);

    // Use the GoogleAnalytics singleton to get a Tracker.
    mGaTracker = mGaInstance.getTracker("UA-XXXX-Y"); // Placeholder tracking ID.
    ... // The rest of your onCreate() code.
  }

  @Override
  public void onStart() {
    super.onStart();

    // Send a screen view when the Activity is displayed to the user.
    mGaTracker.sendView("/HomeScreen");
  }
}

여러 트래커 사용

SDK 버전 2부터는 고유한 추적 ID당 하나씩, 여러 추적기를 한 번에 구현할 수 있습니다. 모든 추적기는 GoogleAnalytics 싱글톤에서 보유한 동일한 전역 상태를 공유합니다.

다음 예에서 화면 조회수는 각각 고유한 속성 ID가 있는 두 개의 추적기를 사용하여 두 개의 개별 속성으로 전송됩니다.

import android.app.Activity

import com.google.analytics.tracking.android.GoogleAnalytics
import com.google.analytics.tracking.android.Tracker

/**
 * A simple Activity that sends a screen view to Google Analytics
 * when it is displayed to the user.
 */
public class HomeScreen extends Activity {

  private GoogleAnalytics mGaInstance;
  private Tracker mGaTracker1;
  private Tracker mGaTracker2;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the GoogleAnalytics singleton.
    mGaInstance = GoogleAnalytics.getInstance(this);

    // Use the GoogleAnalytics singleton to get two Trackers with
    // unique property IDs.
    mGaTracker1 = mGaInstance.getTracker("UA-XXXX-Y");
    mGaTracker2 = mGaInstance.getTracker("UA-XXXX-Z");

    ... // The rest of your onCreate() code.
  }

  @Override
  public void onStart() {
    super.onStart();

    // Send a screen view to "UA-XXXX-Y" the Activity is displayed to the user.
    mGaTracker1.sendView("/HomeScreen");

    // Send another screen view to the second property, "UA-XXXX-Z".
    mGaTracker2.sendView("/Home");
  }
}

EasyTracker에서 제공하는 자동 측정 기능은 호출을 위해 기본 추적기만 사용합니다. 이러한 EasyTracker 기능을 사용하고 다른 추적기를 사용하여 데이터를 보내려면 데이터를 수동으로 전송해야 합니다.

기본 추적기

구현에서 여러 추적기를 사용할 수 있지만 전역적으로 하나의 기본 추적기가 있습니다. 검색된 첫 번째 Tracker가 기본 추적기가 됩니다.

기본 추적기를 가져오려면 다음을 사용하세요.

// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Get default tracker.
Tracker myDefault = myInstance.getDefaultTracker();

기본 추적기를 설정하려면 다음을 사용하세요.

// First get a tracker using a new property ID.
Tracker newTracker = myInstance.getTracker("UA-XXXX-2");

// Then make newTracker the default tracker globally.
myInstance.setDefaultTracker(newTracker);

디스패치

디스패치 설정은 GAServiceManager 싱글톤에서 관리합니다. 전달 기간을 설정하는 방법은 다음과 같습니다.

// Set dispatch period to 30 seconds.
GAServiceManager.getInstance().setDispatchPeriod(30);

예약된 간격이 아닐 때 수동으로 전달하는 방법은 다음과 같습니다.

// Manually dispatch any queued hits.
GAServiceManager.getInstance().dispatch();

샘플링

클라이언트 측 샘플링을 사용 설정하면 Google 애널리틱스로 전송되는 조회수를 제한할 수 있습니다. 앱에 사용자가 많거나 Google 애널리틱스로 대량의 데이터를 전송하는 경우 샘플링을 사용 설정하면 보고가 중단되지 않습니다.

예를 들어 EasyTracker 및 XML을 통해 50% 의 속도로 클라이언트 측 샘플링을 사용 설정하려면 analytics.xml 파일에서 다음 매개변수를 사용합니다.

<string name="ga_sampleFrequency">50.0</string>

프로그래매틱 방식으로 클라이언트 측 샘플링을 사용 설정할 수도 있습니다.

// Get tracker from singleton.
GoogleAnalytics gaInstance = GoogleAnalytics.getInstance(this);
Tracker tracker = gaInstance.getTracker("UA-XXXX-Y"); // Placeholder property ID.

// Set a sample rate of 50%.
tracker.setSampleRate(50.0d);

앱 수준 선택 해제

앱 수준 선택 해제 플래그를 사용 설정하여 앱 전체에서 Google 애널리틱스를 사용 중지할 수 있습니다. 설정된 플래그는 앱의 수명 기간 동안 또는 재설정될 때까지 유지됩니다.

앱 수준의 선택 해제 설정을 가져오려면 다음을 사용하세요.

// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Get the app opt out preference using an AppOptOutCallback.
myInstance.requestAppOptOut(new AppOptOutCallback() {
   @Override
   public void reportAppOptOut(boolean optOut) {
     if (optOut) {
     ... // Alert the user that they've opted out.
     }
   });
}

앱 수준 선택 해제 플래그를 설정하려면 다음을 사용합니다.

myInstance.setAppOptOut(appPreferences.userOptOut);

테스트 및 디버깅

Android용 Google 애널리틱스 SDK는 Google 애널리틱스로 전송되는 데이터에 관한 유용한 정보를 로그에서 출력하는 디버그 모드를 제공합니다.

EasyTracker를 사용하여 디버그 모드를 사용 설정하려면 analytics.xml 리소스 파일에 다음을 추가합니다.

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

프로그래매틱 방식으로 디버그 모드를 사용 설정하려면 다음을 사용하세요.

// Get singleton using application context.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Enable debug mode.
myInstance.setDebug(true);

출력은 GAV2 태그를 사용하여 logcat에 출력됩니다.