Record transaction events
Track in-app purchases and failed transactions to measure revenue and monitor transaction health.
Read time 4 minutesLast updated 21 hours ago
Use the
transactionProducttransactiontransactionFailedtransactiontransactionFailedSend events manually
The Analytics SDK provides theTransactionEventTransactionFailedEventRecord automatically using the IAP plug-in
Transactions made by Unity IAP are automatically forwarded to Analytics using the sametransactiontransactionFailedReport successful purchases
In the following example, thetransactiontransactionpublic void SendTransaction(){ TransactionEvent transaction = new TransactionEvent { TransactionId = "100000576198248", TransactionName = "IAP - A Large Treasure Chest", TransactionType = TransactionType.PURCHASE, TransactionServer = TransactionServer.APPLE, TransactionReceipt = "ewok9Ja81............991KS==" }; transaction.ReceivedItems.Add(new TransactionItem { ItemName = "Golden Battle Axe", ItemType = "Weapon", ItemAmount = 1 }); transaction.ReceivedItems.Add(new TransactionItem { ItemName = "Flaming Sword", ItemType = "Weapon", ItemAmount = 1 }); transaction.ReceivedItems.Add(new TransactionItem { ItemName = "Jewel Encrusted Shield", ItemType = "Armour", ItemAmount = 1 }); transaction.ReceivedVirtualCurrencies.Add(new TransactionVirtualCurrency { VirtualCurrencyName = "Gold", VirtualCurrencyType = VirtualCurrencyType.PREMIUM, VirtualCurrencyAmount = 100 }); transaction.SpentRealCurrency = new TransactionRealCurrency { RealCurrencyType = "USD", RealCurrencyAmount = AnalyticsService.Instance.ConvertCurrencyToMinorUnits("USD", 4.99) }; AnalyticsService.Instance.RecordEvent(transaction);}
Report failed purchases
ThetransactionFailedtransactionFailedtransactionFailedpublic void SendTransactionFailed(){ TransactionFailedEvent transactionFailed = new TransactionFailedEvent { FailureReason = "cancelled", TransactionId = "100000576198248", TransactionName = "IAP - A Large Treasure Chest", TransactionType = TransactionType.PURCHASE, TransactionServer = TransactionServer.APPLE }; transactionFailed.ReceivedItems.Add(new TransactionItem { ItemName = "Golden Battle Axe", ItemType = "Weapon", ItemAmount = 1 }); transactionFailed.ReceivedItems.Add(new TransactionItem { ItemName = "Flaming Sword", ItemType = "Weapon", ItemAmount = 1 }); transactionFailed.ReceivedItems.Add(new TransactionItem { ItemName = "Jewel Encrusted Shield", ItemType = "Armour", ItemAmount = 1 }); transactionFailed.ReceivedVirtualCurrencies.Add(new TransactionVirtualCurrency { VirtualCurrencyName = "Gold", VirtualCurrencyType = VirtualCurrencyType.PREMIUM, VirtualCurrencyAmount = 100 }); transactionFailed.SpentRealCurrency = new TransactionRealCurrency { RealCurrencyType = "USD", RealCurrencyAmount = AnalyticsService.Instance.ConvertCurrencyToMinorUnits("USD", 4.99) }; AnalyticsService.Instance.RecordEvent(transactionFailed);}
Validate transactions
Transaction validation checks to see if a transaction is a legitimate purchase. The platform can undertake transaction receipt validation with iOS and Android stores in order to ensure that any revenue displayed in your dashboards is genuine revenue and not the result of a hacked or jailbroken game. To use transaction validation you'll need to:- Provide the validation key for the relevant store so the Analytics platform can validate the receipt against the store. You can do this under Administration in the Unity Dashboard.
- Include the receipt validation parameters in the event, to inform the platform that the transaction should be validated (this will vary depending on the store).
transaction
revenueValidatedValue | Result |
|---|---|
| 0 | This transaction has not used the revenue validation service and is included in your revenue charts. |
| 1 | This transaction has passed verification and is included in your revenue charts. |
| 2 | This transaction has failed verification and is NOT included in your revenue charts. |
| 3 | This transaction could not be verified due to an error and is NOT included in your revenue charts. |
Apple Store
Analytics automatically attempts receipt validation on iOS transactions. Add the following to iOS transactions to validate the receipt:TheTransactionEvent transaction = new TransactionEvent{ TransactionServer = TransactionServer.APPLE, TransactionId = "100000576198248", TransactionReceipt = "ewok9Ja81............991KS=="};
transactionReceiptGoogle Play
You must provide the Google public key. To update the Google key go to Project Settings > In-app purchase (IAP) settings and populate the Google License Key. Add the following to the transaction event to Android transactions to validate the receipt:TransactionEvent transaction = new TransactionEvent { TransactionServer = TransactionServer.GOOGLE, TransactionReceipt = "{ \"orderId\":\"GPA.1234-5678-9012-34567\", \"packageName\":\"com.example.app\", \"productId\":\"exampleSku\", \"purchaseTime\":1345678900000, \"purchaseState\":0, \"developerPayload\":\"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ\", \"purchaseToken\":\"opaque-token-up-to-1000-characters\"}", TransactionReceiptSignature = "rNvoHwiBdLSW+........VncbYVJ61fmfsQ==" }