透過 Cloud Functions 擴充 Google Analytics (分析)

Google Analytics 提供事件報表 以及使用者與應用程式的互動情形使用 Cloud Functions (第 1 代) 後 可以存取您從 Apple 和 Android 裝置記錄的轉換事件 並根據這些事件觸發函式

觸發 Google Analytics 函式

Cloud Functions 支援 Google Analytics AnalyticsEvent。 每當使用者活動產生轉換事件時,就會觸發這個事件。 舉例來說,您可以編寫函式 in_app_purchase 事件產生時就會觸發,這表示 已完成應用程式內購交易。 您必須指定 Analytics 事件 您想要使用 functions.analytics.event()敬上 方法,然後處理 onLog() 事件處理常式:

exports.sendCouponOnPurchase = functions.analytics.event('in_app_purchase').onLog((event) => {
  // ...
});

存取事件屬性

在每個 Analytics 事件中,您都可以存取 參數和使用者屬性其中包含使用者、 裝置、應用程式和事件的地理位置資訊。 如需參數和使用者屬性的完整清單,請參閱 functions.analytics 參考資料。

適用於購買觸發函式,如 這個範例 建議您根據使用者的語言 事件值 (valueInUSD)。 這個第二個屬性可讓範例函式測試是否為 高價值轉換事件,以便傳送價值更高的優待券給有價值的客戶。

/**
 * After a user has completed a purchase, send them a coupon via FCM valid on their next purchase.
 */
exports.sendCouponOnPurchase = functions.analytics.event('in_app_purchase').onLog((event) => {
  const user = event.user;
  const uid = user.userId; // The user ID set via the setUserId API.
  const purchaseValue = event.valueInUSD; // Amount of the purchase in USD.
  const userLanguage = user.deviceInfo.userDefaultLanguage; // The user language in language-country format.

  // For purchases above 500 USD, we send a coupon of higher value.
  if (purchaseValue > 500) {
    return sendHighValueCouponViaFCM(uid, userLanguage);
  }
  return sendCouponViaFCM(uid, userLanguage);
});

後續步驟

如要進一步瞭解如何在 Cloud Functions 中處理數據分析事件, 請參閱 Google Analytics 說明文件functions.analytics 參考資料, 並嘗試執行 每次購買優待券