2012年2月10日

【Android】複数ActivityでGoogleAnalyticsまとめ(2)

では、おまちどうさま。
今回はGoogleAnalyticsの複数Activityの場合の導入方法です。

参照先は
Google Analytics in Android app - dealing with multiple activities
になりますが、英語です。
注意して下さい。

1回の起動を1訪問と数えてくれ、
Activityの移動をPageViewと数えてくれる優れものです。


今回は2つのクラスを作ります。
1.GoogleAnalyticsSessionManager.java
2.GoogleAnalyticsActivity.java
になります。

GoogleAnalyticsSessionManager.java

(ちょっと手を加えてありますが、解説しません)
public class GoogleAnalyticsSessionManager {
  private static final String UA_ACCOUNT = "UA-0000000-1";
  protected static GoogleAnalyticsSessionManager INSTANCE;

  protected int activityCount = 0;
  protected Integer dispatchIntervalSecs;
  protected String apiKey;
  protected Context context;

  /**
   * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.
   */
  protected GoogleAnalyticsSessionManager( String apiKey, Application context ) {
    this.apiKey = apiKey;
    this.context = context;
  }

  /**
   * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.
   */
  protected GoogleAnalyticsSessionManager( String apiKey, int dispatchIntervalSecs, Application context ) {
    this.apiKey = apiKey;
    this.dispatchIntervalSecs = dispatchIntervalSecs;
    this.context = context;
  }

  /**
   * This should be called once in onCreate() for each of your activities that use GoogleAnalytics.
   * These methods are not synchronized and don't generally need to be, so if you want to do anything
   * unusual you should synchronize them yourself.
   */
  public void incrementActivityCount() {
    if( activityCount==0 )
      if( dispatchIntervalSecs==null )
        GoogleAnalyticsTracker.getInstance().startNewSession(apiKey,context);
      else
        GoogleAnalyticsTracker.getInstance().startNewSession(apiKey,dispatchIntervalSecs,context);

      ++activityCount;
  }

  /**
   * This should be called once in onDestroy() for each of your activities that use GoogleAnalytics.
   * These methods are not synchronized and don't generally need to be, so if you want to do anything
   * unusual you should synchronize them yourself.
   */
  public void decrementActivityCount() {
    activityCount = Math.max(activityCount-1, 0);

    if( activityCount==0 )
      GoogleAnalyticsTracker.getInstance().stopSession();
  }

  /**
   * Get or create an instance of GoogleAnalyticsSessionManager
   */
  public static GoogleAnalyticsSessionManager getInstance( Application application ) {
    if( INSTANCE == null )
      INSTANCE = new GoogleAnalyticsSessionManager(UA_ACCOUNT, 60, application);
    return INSTANCE;
  }

  /**
   * Only call this if you're sure an instance has been previously created using #getInstance(Application)
   */
  public static GoogleAnalyticsSessionManager getInstance() {
    return INSTANCE;
  }
}

次に

GoogleAnalyticsActivity.java

public abstract class GoogleAnalyticsActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Need to do this for every activity that uses google analytics
    GoogleAnalyticsSessionManager.getInstance(getApplication()).incrementActivityCount();
  }

  @Override
  protected void onResume() {
    super.onResume();

    // Example of how to track a pageview event
    GoogleAnalyticsTracker.getInstance().trackPageView(getClass().getSimpleName());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();

    // Purge analytics so they don't hold references to this activity
    GoogleAnalyticsTracker.getInstance().dispatch();

    // Need to do this for every activity that uses google analytics
    GoogleAnalyticsSessionManager.getInstance().decrementActivityCount();
  }
}

最後にどうやって導入するかというと

TestActivity.java

public class TestActivity extends GoogleAnalyticsActivity {
  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    // as usual from here ...
  }
}

みたいな感じで実装してください。

onResumeとonDestroyは呼び出さなくてもいいですし、
使う場合はsuper.onResume()かsuper.onDestroy()を必ず呼んでください。