傳送使用者屬性

使用者屬性用來描述使用者族群區隔,例如語言偏好設定或地理位置。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 可設為 premium 或 standard。如要在報表中取得這項使用者屬性,請按照下列步驟操作:

首先,請用戶端傳送 add_payment_info 事件,並呼叫可存取客戶關係管理系統的伺服器 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 會使用在下列清單中找到的第一個時間戳記,做為要求中每個使用者屬性的時間戳記:

  1. user_properties 中項目的 timestamp_micros。
  2. 要求的 timestamp_micros。
  3. 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
    }
}