ユーザー ID - Android SDK

このデベロッパー ガイドでは、Android 向け Google アナリティクス SDK v3.x を使って User ID を実装する方法を説明します。

概要

User ID 機能を使用して、デバイスをまたいで発生するユーザー アクティビティを Google アナリティクスで測定することができます。たとえば、あるモバイル デバイスで実施しているマーケティング キャンペーンで発生した操作を、別のモバイル デバイスやブラウザで発生したコンバージョンに結び付けて貢献度を割り当てる(アトリビューション)ことが可能です。

Google アナリティクスのヒットに userId フィールドを使って User ID を送出すると、レポートにユニーク ユーザー数がより正確に反映され、新しいクロスデバイス レポート オプションを利用できるようになります。User ID を使用するメリットの詳細をご確認ください。

このガイドでは、userId フィールドと Android 向け Google アナリティクス SDK を使ってユーザー ID を Google アナリティクスに送信する方法を説明します。

前提条件

Google アナリティクスに User ID を送る前に、次の準備が必要です、

実装

Android アプリで既知のユーザーである場合、userId フィールドを使って、そのユーザーを表す Google アナリティクスのすべてのヒット(ページビュー、イベント、e コマース トランザクションなど)で、そのユーザーを表す ID を送信する必要があります。

User ID を送るには、 Measurement Protocol のアンパサンド構文Fields.USER_ID パラメータ名を使って userId フィールドを次のように設定します。

/**
 * An example method called when a user signs in to an authentication system.
 *
 * @param User user represents a generic User object returned by an authentication system on sign in.
 */
public void onUserSignIn(User user) {

  Tracker t = GoogleAnalytics.getInstance(context).getTracker("UA-XXXX-Y");

  // You only need to set User ID on a tracker once. By setting it on the tracker, the ID will be
  // sent with all subsequent hits.
  t.set(Fields.USER_ID, user.getId());

  // This hit will be sent with the User ID value and be visible in User-ID-enabled views (profiles).
  t.send(MapBuilder
      .createEvent("UX",       // Event category (required)
                   "Sign In",  // Event action (required)
                   null,       // Event label
                   null)       // Event value
      .build()
  );
}