วัดการซื้อในแอป

เลือกแพลตฟอร์ม iOS+ Android Unity


การซื้อในแอป (IAP) คือเนื้อหาหรือฟีเจอร์ดิจิทัลที่คุณขายได้ในแอปบนอุปกรณ์เคลื่อนที่ผ่าน Apple App Store หรือ Google Play เพื่อให้แอปของคุณไม่ต้องประมวลผลธุรกรรมทางการเงิน ตัวอย่างการซื้อในแอป ได้แก่ เนื้อหาที่ต้องสมัครใช้บริการหรือไอเทมพิเศษในเกม

Analytics แสดงเหตุการณ์ IAP ในรายงานการซื้อในแอป

สำหรับแอป Unity, SDK Analytics จะผสานรวมกับ Apple App Store บน iOS และ Google Play บน Android

ในกรณีส่วนใหญ่ SDK ของ Analytics จะรวบรวมเหตุการณ์ IAP โดยอัตโนมัติโดยไม่ต้องเรียกใช้ API ในแอป ข้อกำหนดในการตั้งค่าและลักษณะการทำงานของการติดตามจะขึ้นอยู่กับแพลตฟอร์มเป้าหมาย ดังนี้

  • Android: ระบบจะติดตามธุรกรรม Google Play รายการโดยอัตโนมัติหลังจากลิงก์ Google Play โดยที่ไม่ต้องเปลี่ยนแปลงโค้ดใดๆ
  • iOS (StoreKit 1): ระบบจะติดตามธุรกรรมใน App Store ที่ทำโดยใช้ StoreKit 1 API โดยอัตโนมัติ โดยที่ไม่ต้องเปลี่ยนแปลงโค้ดใดๆ
  • iOS (StoreKit 2): คุณต้องบันทึกธุรกรรมใน App Store ที่ทำโดยใช้ StoreKit 2 API ด้วยตนเองโดยใช้ Unity API ที่กำหนดเอง

ก่อนเริ่มต้น

การใช้งาน

Android

สําหรับแอป Android คุณสามารถวัดเหตุการณ์ IAP ได้ทันทีที่ลิงก์กับ Google Play คุณไม่จำเป็นต้องเขียนโค้ดที่กำหนดเองในโปรเจ็กต์ Unity เพื่อวัดการซื้อ

iOS

สำหรับแอป iOS กลยุทธ์การติดตั้งใช้งานจะขึ้นอยู่กับว่าแอปใช้ StoreKit 1 หรือ StoreKit 2 ดังนี้

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 ที่คุณใช้

Callback การยืนยันโดยใช้ Unity IAP v5.2 ขึ้นไป

ใน Unity IAP v5.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 เวอร์ชันเดิม

หากใช้ Callback 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;
}

บันทึกเหตุการณ์การซื้อในแอปด้วยตนเอง

หากต้องการติดตามการซื้อในแอปที่ดำเนินการนอก Play Store หรือ App Store (เช่น ผ่าน 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)
    );
}