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

בחירת פלטפורמה: 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): מערכת StoreKit 1 עוקבת באופן אוטומטי אחרי עסקאות ב-App Store שמתבצעות באמצעות ממשקי API. אין צורך לשנות את הקוד.
  • 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 בפונקציית ה-callback של האישור. אפשר להעביר את 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;
}