| 选择平台: | iOS+ Android Unity |
应用内购买 (IAP) 是可以通过 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 v13.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,您必须通过将交易标识符字符串传递给 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 v5.2+ 的确认回调
在 Unity IAP v5.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 回调,则可以从购买的产品中提取交易标识符:
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; }
手动记录应用内购买事件
如果您需要跟踪在 Play 商店或 App Store 之外进行的应用内购买(例如通过替代商店或自定义付款流程或其他市场),可以使用 IN_APP_PURCHASE 事件手动记录这些交易。
using Firebase.Analytics; public void LogCustomInAppPurchase(string productName, double value, string currencyCode, int quantity = 1) { FirebaseAnalytics.LogEvent( FirebaseAnalytics.EventInAppPurchase, new Parameter(FirebaseAnalytics.ParameterProductName, productName), new Parameter(FirebaseAnalytics.ParameterValue, value), new Parameter(FirebaseAnalytics.ParameterCurrency, currencyCode), new Parameter(FirebaseAnalytics.ParameterQuantity, quantity) ); }