使用者 ID - Android SDK

本開發人員指南說明如何使用 Android 專用的 Google Analytics (分析) SDK v3.x 導入 User ID。

總覽

使用「User ID」功能,即可在 Google Analytics (分析) 中評估橫跨不同裝置的使用者活動,例如將某行動裝置上的行銷廣告活動互動,歸給其他行動裝置或瀏覽器中發生的轉換。

使用 userId 欄位傳送 User-ID 與 Google Analytics (分析) 命中後,報表就會顯示更準確的不重複使用者人數,並提供新的跨裝置報表選項。進一步瞭解使用 User-ID 的好處。

本指南說明如何使用 userId 欄位和 Android 適用的 Google Analytics (分析) SDK,將使用者 ID 傳送至 Google Analytics (分析)。

必要條件

傳送 User ID 至 Google Analytics (分析) 前,請先:

導入作業

針對已知的 Android 應用程式使用者,您應該使用 userId 欄位傳送代表該名使用者的 ID,並一併傳送所有 Google Analytics (分析) 命中 (例如網頁瀏覽、事件、電子商務交易等)。

如要傳送 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()
  );
}