用户属性用于描述用户群细分,例如语言偏好设置或地理位置。Google Analytics 会自动记录一些用户属性。如果要收集其他属性,每个项目最多可以设置 25 个其他用户属性。如需了解如何设置和注册用户属性,请参阅自定义用户属性。
用户属性用于改进用户细分,不过用户属性数据通常仅在服务器端提供。借助 Measurement Protocol,您可以使用服务器端数据增强客户端衡量效果,通常情况下,仅使用客户端解决方案是无法实现这一点的。
预留名称
部分用户属性名称已预留,不能用于衡量:
first_open_timefirst_visit_timelast_deep_link_referreruser_idfirst_open_after_install
此外,用户属性名称不能以下列内容开头:
google_ga_firebase_
用法示例
在下面的示例中,您的 CRM 系统具有一个要在衡量中添加的用户属性 (customer_tier)。customer_tier 可以设置为 premium 或 standard 中的一个。如需在报告中显示此用户属性,您需要执行以下操作:
首先,让客户端发送 add_payment_info 事件并调用可以访问您的 CRM 系统的服务器 API:
客户端代码
FirebaseAnalytics.logEvent("add_payment_info")
ServerAPI.addCustomerTier(
FirebaseAnalytics.getAppInstanceId(),
"[{name: \"add_payment_info\"}"]
);
然后,您的服务器会通过 Measurement Protocol 使用 customer_tier 用户属性增强衡量效果:
服务器代码
const firebaseAppId = "FIREBASE_APP_ID";
const apiSecret = "API_SECRET";
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",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"app_instance_id": "APP_INSTANCE_ID",
"user_properties": {
"customer_tier": {
"value": "CUSTOMER_TIER"
}
},
"events": JSON.parse(events)
})
});
}
此用户属性会报告 premium 和 standard 这两个细分。
如需全面了解如何使用 Measurement Protocol 发送事件,请参阅发送事件。
替换时间戳
对于请求中的每个用户属性,Measurement Protocol 会使用在以下列表中找到的第一个时间戳:
user_properties中相应条目的timestamp_micros。- 请求的
timestamp_micros。 - Measurement Protocol 收到请求的时间。
以下示例发送一个请求级时间戳,该时间戳适用于请求中的所有用户属性。因此,Measurement Protocol 会为 customer_tier 和 customer_group 用户属性分配 requestUnixEpochTimeInMicros 的时间戳。
{
"timestamp_micros": requestUnixEpochTimeInMicros,
"user_properties": {
"customer_tier": {
"value": customerTierValue
},
"customer_group": {
"value": customerGroupValue
}
}
}
以下示例同时发送了请求级时间戳和 customer_tier 用户属性的时间戳。因此,Measurement Protocol 会为 customer_tier 分配 customerTierUnixEpochTimeInMicros 的时间戳,并为 customer_group 分配 requestUnixEpochTimeInMicros 的时间戳。
"timestamp_micros": requestUnixEpochTimeInMicros,
"user_properties": {
"customer_tier": {
"value": customerTierValue,
"timestamp_micros": customerTierUnixEpochTimeInMicros
},
"customer_group": {
"value": customerGroupValue
}
}