アプリ内購入を測定する

プラットフォームを選択: 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 イベントを測定できます。購入を測定するために Unity プロジェクトでカスタムコードを記述する必要はありません。

iOS

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

StoreKit 1

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

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

アプリ内購入イベントを手動でログに記録する

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