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

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


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

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

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

ในกรณีส่วนใหญ่ Analytics SDK จะรวบรวมเหตุการณ์ 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 เวอร์ชันที่คุณใช้

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

ใน Unity IAP v5.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 แบบเดิม คุณสามารถแยกตัวระบุธุรกรรมออกจากผลิตภัณฑ์ที่ซื้อได้

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