アプリ内購入を測定する

プラットフォームを選択: iOS+ Android Unity


アプリ内購入(IAP)とは、Apple App Store または Google Play 経由で販売できる、モバイルアプリ内のデジタルコンテンツまたは機能であり、販売する際にアプリ内で決済を処理する必要がありません。アプリ内購入の例としては、定期購入コンテンツや特別なゲーム コンテンツが挙げられます。

Analytics では、 アプリ内購入レポートに IAP イベントが表示されます。

Unity アプリの場合、Analytics SDK は iOS の Apple App Store と Android の Google Play に統合されています。

ほとんどの場合、Analytics SDK は自動的に IAP イベントを収集するため、アプリ内で API 呼び出しを行う必要はありません。設定要件とトラッキングの動作は、ターゲット プラットフォームによって異なります。

  • Android: Google Play トランザクションは、Google Play にリンクすると自動的にトラッキングされます。コードを変更する必要はありません。
  • iOS(StoreKit 1): StoreKit 1 API を使用して行われた App Store のトランザクションが自動的にトラッキングされます。コードを変更する必要はありません。
  • iOS(StoreKit 2): StoreKit 2 API を使用して行われた App Store のトランザクションは、カスタム Unity API を使用して手動でログに記録する必要があります。

始める前に

実装

Android

Android アプリの場合は、Google Play に リンクするとすぐに IAP イベントを測定できます。Google Play 購入を測定するために Unity プロジェクトでカスタム コードを記述する必要はありません。

iOS

iOS アプリの場合、実装戦略は、アプリで StoreKit 1 と StoreKit 2 のどちらを使用するかによって異なります。

StoreKit 1

購入フローで StoreKit 1 を使用している場合、アナリティクス SDK は自動的に IAP イベントをロギングします。これらの購入をトラッキングするためにカスタム コードを追加する必要はありません。

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's standard In-App Purchasing package (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 コールバックを使用している場合は、購入した商品からトランザクション 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;
}