用户 ID - Android SDK

本开发者指南将演示如何使用 Android 版 Google Analytics(分析)SDK v3.x 来实现 User ID。

概览

User ID 功能可让您在 Google Analytics(分析)中衡量跨多种设备的用户活动,例如将在某台移动设备上与某个营销广告系列的某次互动归因于在另一台移动设备上或浏览器中发生的某次转化。

如果您使用 userId 字段随 Google Analytics(分析)命中一起发送 User ID,您的报告中的唯一身份用户数将更加准确,并且会提供新的跨设备报告选项。 详细了解使用 User-ID 的好处

本指南介绍了如何使用 userId 字段和 Android 版 Google Analytics(分析)SDK 将 User-ID 发送到 Google Analytics(分析)。

前提条件

在将 User ID 发送到 Google Analytics(分析)之前:

实现

当您的 Android 应用能够识别某位用户的身份时,您应当随您的所有 Google Analytics(分析)命中(例如网页浏览、事件、电子商务交易等)一起,使用 userId 字段发送代表该用户的 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()
  );
}