| เลือกแพลตฟอร์ม: | 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 ที่กำหนดเอง
ก่อนเริ่มต้น
ตั้งค่าโปรเจ็กต์ Firebase และโค้ดเบสของแอปตามที่อธิบายไว้ใน เริ่มต้นใช้งาน Google Analytics
ตรวจสอบว่าแอปของคุณเป็นไปตามข้อกำหนดในการตั้งค่าเฉพาะแพลตฟอร์มต่อไปนี้
Android: ลิงก์แอป Firebase กับ Google Play
iOS (StoreKit 1): ไม่จำเป็นต้องตั้งค่าเพิ่มเติม การติดตาม StoreKit 1 จะทำงานโดยอัตโนมัติ
iOS (StoreKit 2): ตรวจสอบว่าแอปใช้ Firebase Unity SDK v13.12.0 ขึ้นไป
โปรดทำความคุ้นเคยกับ API การซื้อในแอปโดยอ่านเอกสารเฉพาะแพลตฟอร์มสำหรับ Google Play Billing, Apple StoreKit และ Unity In-App Purchasing
การใช้งาน
Android
สำหรับแอป Android คุณจะวัดเหตุการณ์ IAP ได้ทันทีที่ ลิงก์กับ Google Play คุณไม่จําเป็นต้องเขียนโค้ดที่กําหนดเองในโปรเจ็กต์ Unity เพื่อวัดการซื้อ
iOS
สำหรับแอป iOS กลยุทธ์การติดตั้งใช้งานจะขึ้นอยู่กับว่าแอปใช้ StoreKit 1 หรือ StoreKit 2 หรือไม่
StoreKit 1
หากขั้นตอนการซื้อใช้ StoreKit 1 SDK ของ Analytics จะบันทึกเหตุการณ์ 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; }
บันทึกเหตุการณ์การซื้อในแอปด้วยตนเอง
หากต้องการติดตามการซื้อในแอปที่ทำนอก Play Store หรือ App
Store (เช่น ผ่าน 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) ); }