Record transaction events

Use the transaction event to track successful in-app purchases. This event contains arrays and some special Product objects to capture multiple items and currencies bought in a single transaction. The structure of the transaction event contains multiple Product objects to record productsSpent and productsReceived.

Unsuccessful transactions are tracked under a dedicated transactionFailed event, containing details regarding the purchase transaction and the reason for its failure. This doesn't contribute to the revenue metrics in the dashboards.

You can send the transaction and transactionFailed events manually via the SDK or REST API. If you're using Unity IAP version 4.2 or higher, it automatically sends these events.

Important: Sending transactions manually to Analytics while also using the IAP plug-in results in counting them twice and reporting inflated revenue.

Important: Transaction events received from Apple and Google sandbox purchases can be reviewed in the Analytics > SQL Data Explorer tool, but they don't contribute towards dashboard Revenue reports.

Send events manually

The transaction and transactionFailed events are standard events with a predefined schema that you must match to process and track the event under the revenue KPIs in the dashboard. You can refer to their schema in the Event Manager documentation.

Report successful purchases

In the following example, the transaction event records the player using real US dollars to purchase a treasure chest which contains virtual currency and multiple items. For more information on this event and parameters, see Event Manager.

Use the following code to create the transaction event:

public void SendTransaction()
{
	TransactionEvent transaction = new TransactionEvent
	{
		ProductsReceived = productsReceived,
		ProductsSpent = productsSpent,
		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 = "PREMIUM",
		VirtualCurrencyAmount = 100
	});
	
	transactions.SpentRealCurrency = new TransactionRealCurrency
	{
		RealCurrencyType = "USD",
		RealCurrencyAmount = 499
	};

	AnalyticsService.Instance.RecordEvent(transaction);
}

Report failed purchases

The transactionFailed event captures unsuccessful in-app purchases. Purchases can fail for a number of reasons, including network failure, payment failure, or device settings.

In the following example the transactionFailed event records the player's attempt to use real US dollars to purchase a treasure chest that contains virtual currency and multiple items, where the user cancels the purchase. For more information on this event and parameters, see documentation on the Event Manager.

Use the following code to create the transactionFailed event:

public void SendTransactionFailed()
{
	TransactionFailedEvent transactionFailed = new TransactionFailedEvent
	{
		FailureReason = "cancelled",
		ProductsReceived = productsReceived,
		ProductsSpent = productsSpent,
		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 = "PREMIUM",
		VirtualCurrencyAmount = 100
	});

	transactionFailed.SpentRealCurrency = new TransactionRealCurrency
	{
		RealCurrencyType = "USD",
		RealCurrencyAmount = 499
	};

	AnalyticsService.Instance.RecordEvent(transactionFailed);
}

Record automatically using the IAP plug-in

Transactions made by the Unity IAP are automatically forwarded to Analytics using the same transaction event schema as when you send the events manually. The transaction event contributes towards the revenue KPIs available in Analytics. Unsuccessful transactions are automatically tracked by the IAP plug-in under the transactionFailed event.

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 Cloud Dashboard.
  • Include the receipt validation parameters in the transaction event, to inform the platform that the transaction should be validated (this will vary depending on the store).

The transaction event contains a revenueValidated parameter, used by IAP receipt validation services; it'll populate automatically if you're using revenue validation. The available values are:

ValueResult
0This transaction has not used the revenue validation service and is included in your revenue charts.
1This transaction has passed verification and is included in your revenue charts.
2This transaction has failed verification and is NOT included in your revenue charts.
3This 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:

"transactionServer": "APPLE",
"transactionID":"100000576198248",
"transactionReceipt": "ewok9Ja81............991KS=="

The transactionReceipt is the BASE 64 encoded receipt data returned from Apple. Refer to the Apple developer website for details on Apple Receipt Validation.

Google 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:

"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=="

Important: Since the transaction receipt and a private key generates the transactionReceiptSignature in order to validate the receipt, it must be the exact string provided by Google and passed as a string, not a nested JSON object.