使用者 ID - iOS SDK

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

總覽

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

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

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

必要條件

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

導入作業

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

如要傳送 User-ID,請使用 Measurement Protocol 與語法kGAIUserId 參數名稱設定 userId 欄位,如以下範例所示:

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

  id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

  // 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.
  [tracker set:kGAIUserId
         value:user.id];

  // This hit will be sent with the User ID value and be visible in User-ID-enabled views (profiles).
  [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"UX"            // Event category (required)
                                                        action:@"User Sign In"  // Event action (required)
                                                         label:nil              // Event label
                                                         value:nil] build]];    // Event value
}

以下範例說明如何取得 User ID:

NSString *userId = [tracker get:kGAIUserId];