发送用户属性

用户属性用于描述用户群细分,例如语言偏好设置或地理位置。Google Analytics 会自动记录一些用户属性。如果要收集其他属性,每个项目最多可以设置 25 个其他用户属性。如需了解如何设置和注册用户属性,请参阅自定义用户属性

用户属性用于改进用户细分,不过用户属性数据通常仅在服务器端提供。利用 Measurement Protocol,您可以 使用服务器端数据进行客户端衡量,这通常不可行 仅使用客户端解决方案

部分用户属性名称已预留,不能用于衡量:

  • first_open_time
  • first_visit_time
  • last_deep_link_referrer
  • user_id
  • first_open_after_install

此外,用户属性名称不能以下列内容开头:

  • google_
  • ga_
  • firebase_

用法示例

在下面的示例中,您的 CRM 系统具有一个要在衡量中添加的用户属性 (customer_tier)。customer_tier 可以设置为 premiumstandard 中的一个。如需在报告中显示此用户属性,您需要执行以下操作:

首先,让客户端发送 add_payment_info 事件并调用 有权访问您的客户关系管理系统的服务器 API:

客户端代码

FirebaseAnalytics.logEvent("add_payment_info")
ServerAPI.addCustomerTier(
  FirebaseAnalytics.getAppInstanceId(),
  "[{name: \"add_payment_info\"}"]
);

然后,您的服务器会使用 customer_tier 用户属性增强衡量 使用 Measurement Protocol:

服务器代码

const firebaseAppId = 'X:XX:XX:XX';
const apiSecret = '<secret_value>';

function addCustomerTier(appInstanceId, events) {

  // Request the customer tier from the CRM.
  const customerTier = getCustomerTier(appInstanceId);

  const queryParams = `?firebase_app_id=${firebaseAppId}&api_secret=${apiSecret}`;
  fetch(`https://www.google-analytics.com/mp/collect${queryParams}`, {
    method: "POST",
    body: JSON.stringify({
      "app_instance_id": appInstanceId,
      "user_properties": {
        "customer_tier": {
          "value": customerTier
        }
      },
      "events": JSON.parse(events)
    })
  });
}

此用户属性会报告 premiumstandard 这两个细分。

请参阅发送事件,详细了解如何使用 Measurement Protocol。

替换时间戳

Measurement Protocol 使用在以下代码中发现的第一个时间戳 每个用户属性的列表:

  1. user_properties 中条目的 timestamp_micros
  2. 请求的 timestamp_micros
  3. Measurement Protocol 收到请求的时间。

以下示例会发送一个请求级时间戳,该时间戳应用于 请求中的用户属性因此,Measurement Protocol 分配给 customer_tiercustomer_group 用户属性的时间戳均为 requestUnixEpochTimeInMicros

{
  "timestamp_micros": requestUnixEpochTimeInMicros,
  "user_properties": {
      "customer_tier": {
        "value": customerTierValue
      },
      "customer_group": {
        "value": customerGroupValue
      }
  }
}

以下示例同时发送请求级时间戳和 customer_tier 用户属性。因此,Measurement Protocol 分配给 customer_tiercustomerTierUnixEpochTimeInMicros 时间戳,以及 customer_grouprequestUnixEpochTimeInMicros 的时间戳。

"timestamp_micros": requestUnixEpochTimeInMicros,
"user_properties": {
    "customer_tier": {
      "value": customerTierValue,
      "timestamp_micros": customerTierUnixEpochTimeInMicros
    },
    "customer_group": {
      "value": customerGroupValue
    }
}