Measure in-app purchases

Select platform: iOS+ Android Unity


In-app purchases (IAP) are digital content or features that you can sell in a mobile app through the Apple App Store or Google Play so your app doesn't have to process financial transactions. Examples of in-app purchases include subscription-based content or special game pieces.

Analytics shows IAP events in the In-app purchases report.

For Unity apps, the Analytics SDK integrates with the Apple App Store on iOS and Google Play on Android.

In most cases, the Analytics SDK automatically collects IAP events without requiring API calls in your app. The setup requirements and tracking behavior depend on the target platform:

  • Android: Google Play transactions are tracked automatically after linking Google Play. No code changes are required.
  • iOS (StoreKit 1): App Store transactions made using StoreKit 1 APIs are tracked automatically. No code changes are required.
  • iOS (StoreKit 2): App Store transactions made using StoreKit 2 APIs must be logged manually using the custom Unity API.

Before you begin

Implementation

Android

For Android apps, you can measure IAP events as soon as you link to Google Play. You don't need to write any custom code in your Unity project to measure purchases.

iOS

For iOS apps, the implementation strategy depends on whether your app uses StoreKit 1 or StoreKit 2:

StoreKit 1

If your purchase flow uses StoreKit 1, the Analytics SDK automatically logs IAP events. You don't need to add any custom code to track these purchases.

StoreKit 2

If your purchase flow uses StoreKit 2, you must manually log verified transactions by passing the transaction identifier string to 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");
        }
    });
}
Retrieve the transaction ID using Unity IAP

If your project uses Unity's standard In-App Purchasing package (UnityEngine.Purchasing), the method for retrieving the transaction ID depends on the Unity IAP API version you are using.

Confirmation callback using Unity IAP v5.2+

In Unity IAP v5.2 and higher, you can access Apple-specific transaction information directly from the Order object in the confirmation callback. You can pass either the OriginalTransactionID or the current transaction ID to 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
}
Legacy ProcessPurchase callback

If you are using the legacy ProcessPurchase callback, you can extract the transaction identifier from the purchased product:

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;
}