מדידת הרכישות מתוך האפליקציה

בחירת פלטפורמה: iOS+‎ Android Unity


רכישות מתוך האפליקציה (IAP) הן תכונות או תוכן דיגיטלי שאפשר למכור באפליקציה לנייד דרך Apple App Store או Google Play, כך שלא תצטרכו לעבד עסקאות פיננסיות דרך האפליקציה. דוגמאות לרכישות מהאפליקציה כוללות תוכן שמבוסס על מינויים או פריטים מיוחדים במשחקים.

Analytics מציג אירועי IAP בדוח רכישות מתוך האפליקציה.

באפליקציות ל-Unity, ‏ Analytics SDK משולב עם Apple App Store ב-iOS ו-Google Play ב-Android.

ברוב המקרים, Analytics SDK אוסף באופן אוטומטי אירועים של רכישות מתוך האפליקציה בלי שנדרשות קריאות ל-API באפליקציה. דרישות ההגדרה והתנהגות המעקב תלויות בפלטפורמת היעד:

  • Android: המערכת עוקבת אחרי עסקאות Google Play באופן אוטומטי אחרי קישור Google Play. אין צורך לשנות את הקוד.
  • iOS (‏StoreKit 1): המערכת עוקבת באופן אוטומטי אחרי עסקאות ב-App Store שמתבצעות באמצעות ממשקי API של StoreKit 1. אין צורך לשנות את הקוד.
  • iOS (StoreKit 2): צריך לתעד ידנית עסקאות ב-App Store שבוצעו באמצעות StoreKit 2 APIs באמצעות Unity API בהתאמה אישית.

לפני שמתחילים

הטמעה

Android

באפליקציות ל-Android, אפשר למדוד אירועים של רכישות מתוך האפליקציה ברגע שמקשרים אל Google Play. אתם לא צריכים לכתוב קוד בהתאמה אישית בפרויקט Unity כדי למדוד רכישות.

iOS

באפליקציות ל-iOS, אסטרטגיית ההטמעה תלויה בגרסה של StoreKit שבה האפליקציה משתמשת:

StoreKit 1

אם תהליך הרכישה שלכם משתמש ב-StoreKit 1, ‏ Analytics 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

אם הפרויקט שלכם משתמש בחבילת הרכישות מתוך האפליקציה הרגילה של Unity (UnityEngine.Purchasing), השיטה לאחזור מזהה העסקה תלויה בגרסת Unity IAP API שבה אתם משתמשים.

אישור של קריאה חוזרת באמצעות Unity IAP גרסה 5.2 ואילך

ב-Unity IAP מגרסה 5.2 ואילך, אפשר לגשת למידע על טרנזקציות ספציפיות ל-Apple ישירות מאובייקט Order בקריאה החוזרת לאישור. אפשר להעביר את OriginalTransactionID או את מזהה העסקה הנוכחי אל 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 callback מהגרסה הקודמת, אתם יכולים לחלץ את מזהה העסקה מהמוצר שנרכש:

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