傳送使用者屬性

使用者屬性用來描述使用者區隔,例如語言偏好或地理位置。Analytics 會自動記錄部分使用者屬性。如要收集其他屬性,每個專案最多可額外設定 25 個使用者屬性。如要瞭解如何設定及註冊使用者屬性,請參閱「自訂使用者屬性」。

使用者屬性可提升使用者區隔效果,但使用者屬性資料通常只會顯示在伺服器端。透過 Measurement Protocol,您可以運用伺服器端資料擴增用戶端評估結果,這通常是僅使用用戶端解決方案無法達成的目標。

保留名稱

部分使用者屬性名稱為保留名稱,無法用於評估:

  • first_open_time
  • first_visit_time
  • last_deep_link_referrer
  • user_id
  • first_open_after_install

此外,使用者屬性名稱的開頭不得為:

  • google_
  • ga_
  • firebase_

使用案例:

在下列範例中,您的顧客關係管理系統具有要新增至評估作業的使用者屬性 (customer_tier)。customer_tier 可以設為 premiumstandard。如要在報表中取得這項使用者屬性,請執行下列操作:

首先,請讓用戶端傳送 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)
    })
  });
}

這項使用者屬性會回報 premiumstandard 這兩個區隔。

如要進一步瞭解如何使用 Measurement Protocol 傳送事件,請參閱「傳送事件」。

覆寫時間戳記

Measurement Protocol 會使用下列清單中找到的第一個時間戳記,做為要求中每個使用者屬性的時間戳記:

  1. timestamp_micros 中的項目。user_properties
  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_tier 指派 customerTierUnixEpochTimeInMicros 的時間戳記,並為 customer_group 指派 requestUnixEpochTimeInMicros 的時間戳記。

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