| 選取平台: | iOS+ Android Unity |
應用程式內購是指可透過 Apple App Store 或 Google Play 在行動應用程式中銷售的數位內容或功能,您的應用程式無需處理金融交易。應用程式內購項目包括訂閱型內容或特殊遊戲商品。
Analytics 顯示應用程式內購報表中的應用程式內購事件。
如果是 Unity 應用程式,Analytics SDK 會與 iOS 上的 Apple App Store 整合,以及與 Android 上的 Google Play 整合。
在大多數情況下,Analytics SDK 會自動收集應用程式內購事件,不需要在應用程式中進行 API 呼叫。設定規定和追蹤行為取決於目標廣告平台:
- Android:連結 Google Play 後,系統會自動追蹤 Google Play 交易。這不需要修改任何程式碼。
- iOS (StoreKit 1):系統會自動追蹤使用 StoreKit 1 API 進行的 App Store 交易。這不需要修改任何程式碼。
- iOS (StoreKit 2):使用 StoreKit 2 API 進行的 App Store 交易必須使用自訂 Unity API 手動記錄。
事前準備
按照「開始使用 Google Analytics」一文所述,設定 Firebase 專案和應用程式的程式碼集。
請確認應用程式符合下列平台專屬設定規定:
Android:將 Firebase 應用程式連結至 Google Play。
iOS (StoreKit 1):無須進行額外設定。系統會自動追蹤 StoreKit 1。
iOS (StoreKit 2):請確認應用程式使用的是 Firebase Unity SDK 13.12.0 以上版本。
請詳閱 Google Play 帳款服務、Apple StoreKit 和 Unity 應用程式內購的平台專屬說明文件,確保您熟悉應用程式內購 API。
實作
Android
如果是 Android 應用程式,只要連結至 Google Play,即可評估應用程式內購事件。您不需要在 Unity 專案中撰寫任何自訂程式碼,即可評估購買交易。
iOS
如果是 iOS 應用程式,實作策略取決於應用程式使用的是 StoreKit 1 或 StoreKit 2:
StoreKit 1
如果您的購買流程使用 StoreKit 1,Analytics SDK 會自動記錄應用程式內購事件。您不需要加入任何自訂程式碼,即可追蹤這些購買交易。
StoreKit 2
如果購買流程使用 StoreKit 2,您必須將交易 ID 字串傳遞至 LogAppleTransactionAsync(),手動記錄已驗證的交易。
using Firebase.Analytics; // Call this method once you have completed a StoreKit 2 transaction // and have the transaction identifier. public void LogPurchase(string transactionId) { FirebaseAnalytics.LogAppleTransactionAsync(transactionId).ContinueWith(task => { if (task.IsFaulted || task.IsCanceled) { Debug.LogError("Failed to log Apple purchase transaction: " + task.Exception); } else { Debug.Log("Successfully logged Apple purchase transaction"); } }); }
使用 Unity IAP 擷取交易 ID
如果您的專案使用 Unity 的標準應用程式內購套件 (UnityEngine.Purchasing),交易 ID 的擷取方法取決於您使用的 Unity IAP API 版本。
使用 Unity IAP 5.2 以上版本的回呼確認
在 Unity IAP 5.2 以上版本中,您可以直接從確認回呼中的 Order 物件存取 Apple 專屬交易資訊。您可以將 OriginalTransactionID 或目前的交易 ID 傳遞至 LogAppleTransactionAsync():
using UnityEngine.Purchasing; using Firebase.Analytics; using Firebase.Extensions; // Example using Unity IAP v5.2+ Order confirmation void OnPurchaseConfirmed(Order order) { #if UNITY_IOS if (order is ConfirmedOrder confirmedOrder) { var appleInfo = confirmedOrder.Info.Apple; if (appleInfo != null) { string transactionId = appleInfo.OriginalTransactionID; if (!string.IsNullOrEmpty(transactionId)) { // Log the StoreKit 2 transaction with Firebase Analytics FirebaseAnalytics.LogAppleTransactionAsync(transactionId).ContinueWithOnMainThread(task => { if (task.IsFaulted || task.IsCanceled) { Debug.LogError($"Failed to log Apple purchase transaction: {task.Exception}"); } else { Debug.Log("Successfully logged Apple purchase transaction"); } }); } else { Debug.LogWarning("Apple OriginalTransactionID is null or empty. Skipping Firebase logging."); } } } else if (order is FailedOrder failedOrder) { Debug.Log($"Purchase confirmation bypassed due to failure: {failedOrder.FailureReason}"); } #endif }
舊版 ProcessPurchase 回呼
如果您使用舊版 ProcessPurchase 回呼,可以從購買的產品中擷取交易 ID:
using UnityEngine.Purchasing; using Firebase.Analytics; using Firebase.Extensions; public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { // Extract the transaction ID from the purchased product string transactionId = args.purchasedProduct.transactionID; #if UNITY_IOS // Log the StoreKit 2 transaction with Firebase Analytics FirebaseAnalytics.LogAppleTransactionAsync(transactionId).ContinueWithOnMainThread(task => { if (task.IsFaulted || task.IsCanceled) { Debug.LogError($"Failed to log Apple purchase transaction: {task.Exception}"); } else { Debug.Log("Successfully logged Apple purchase transaction"); } }); #endif return PurchaseProcessingResult.Complete; }