# 거래 이벤트 기록

> Track in-app purchases and failed transactions to measure revenue and monitor transaction health.

성사된 인앱 구매를 트래킹하려면 `transaction` 이벤트를 사용합니다. 이 이벤트에는 배열과 일부 특수 `Product` 오브젝트가 포함되어 단일 거래로 구매한 여러 아이템과 재화를 캡처합니다. 거래 이벤트의 구조에는 `productsSpent`와 `productsReceived`를 기록하는 여러 `Product` 오브젝트가 포함됩니다.

실패한 거래는 구매 거래와 실패 이유에 대한 세부 정보가 포함된 전용 `transactionFailed` 이벤트로 트래킹됩니다. 대시보드의 매출(Revenue) 지표에는 영향을 미치지 않습니다.

SDK 또는 REST API를 통해 `transaction`과 `transactionFailed` 이벤트를 수동으로 전송할 수 있습니다. [Unity IAP](https://docs.unity3d.com/Packages/com.unity.purchasing@latest?subfolder=/manual/Overview.html) 버전 4.2 이상을 사용하는 경우에는 Unity IAP가 자동으로 이러한 이벤트를 전송합니다.

**중요**: IAP 플러그인을 사용하면서 거래를 수동으로 Analytics에 전송하면 거래를 두 번 계산하여 부풀려진 매출이 리포트될 수 있습니다.

> **Warning:**
>
> **중요**: Apple 및 Google **샌드박스 구매**를 통해 받은 거래 이벤트의 경우 **Analytics** > **SQL 데이터 탐색기** 툴에서 검토할 수는 있지만 대시보드 매출 리포트에는 영향을 미치지 않습니다.

### 이벤트 수동 전송##record-automatically-using-the-iap-plug-in

`transaction`과 `transactionFailed` 이벤트는 사전 정의된 스키마가 포함된 스탠다드 이벤트로, 대시보드에서 매출 KPI에 따라 이벤트를 처리하고 트래킹하려면 두 스키마가 일치해야 합니다. [이벤트 관리자](event-manager) 기술 자료에서 해당 스키마를 참고할 수 있습니다.

## 구매 성공 리포트##send-events-manually

다음 예시에서 `transaction` 이벤트는 플레이어가 실제 미국 달러를 사용하여 가상 화폐와 여러 아이템이 담긴 보물 상자를 구매하는 상황을 기록합니다. 이 이벤트와 파라미터에 대한 자세한 내용은 [이벤트 관리자](event-manager)를 참고하십시오.

다음 코드를 사용하여 `transaction` 이벤트를 생성합니다.

```cs
public void SendTransaction()
{
	var productsReceived = new Product()
	{
		Items = new List<Item>()
		{
			new Item(){ ItemName = "Golden Battle Axe"      , ItemType = "Weapon", ItemAmount = 1},
			new Item(){ ItemName = "Flaming Sword"          , ItemType = "Weapon", ItemAmount = 1},
			new Item(){ ItemName = "Jewel Encrusted Shield" , ItemType = "Armour", ItemAmount = 1}
		},
		VirtualCurrencies = new List<VirtualCurrency>()
		{
			new VirtualCurrency()
			{
				VirtualCurrencyName = "Gold",
				VirtualCurrencyType = "PREMIUM",
				VirtualCurrencyAmount = 100
			}
		}
	};

	var productsSpent = new Product()
	{
		RealCurrency = new RealCurrency()
		{
			RealCurrencyType = "USD",
			RealCurrencyAmount = 499
		}
	};

	AnalyticsService.Instance.Transaction(new TransactionParameters()
	{
		ProductsReceived = productsReceived,
		ProductsSpent = productsSpent,
		TransactionID = "100000576198248",
		TransactionName = "IAP - A Large Treasure Chest",
		TransactionType = TransactionType.PURCHASE,
		TransactionServer = TransactionServer.APPLE,
		TransactionReceipt = "ewok9Ja81............991KS=="
	});
}
```

## 구매 실패 리포트##validate-transactions

`transactionFailed` 이벤트는 실패한 인앱 구매를 캡처합니다. 네트워크 오류, 결제 오류 또는 기기 설정 등 다양한 이유로 구매가 실패할 수 있습니다.

다음 예시에서 `transactionFailed` 이벤트는 플레이어가 실제 미국 달러를 사용하여 가상 화폐와 여러 아이템이 담긴 보물 상자를 구매하려고 시도하다가 구매를 취소하는 상황을 기록합니다. 이 이벤트와 파라미터에 대한 자세한 내용은 [이벤트 관리자](event-manager) 기술 자료를 참고하십시오.

다음 코드를 사용하여 `transactionFailed` 이벤트를 생성합니다.

```cs
public void SendTransactionFailed()
{
	var productsReceived = new Product()
	{
		items = new List<Item>()
		{
			new Item(){ ItemName = "Golden Battle Axe"      , ItemType = "Weapon", ItemAmount = 1},
			new Item(){ ItemName = "Flaming Sword"          , ItemType = "Weapon", ItemAmount = 1},
			new Item(){ ItemName = "Jewel Encrusted Shield" , ItemType = "Armour", ItemAmount = 1}
		},
		VirtualCurrencies = new List<VirtualCurrency>()
		{
			new VirtualCurrency()
			{
				VirtualCurrencyName = "Gold",
				VirtualCurrencyType = "PREMIUM",
				VirtualCurrencyAmount = 100
			}
		}
	};

	var productsSpent = new Product()
	{
		RealCurrency = new RealCurrency()
		{
			RealCurrencyType = "USD",
			RealCurrencyAmount = 499
		}
	};

	AnalyticsService.Instance.TransactionFailed(new TransactionFailedParameters 
	{
		FailureReason = "cancelled",
		ProductsReceived = productsReceived,
		ProductsSpent = productsSpent,
		TransactionID = "100000576198248",
		TransactionName = "IAP - A Large Treasure Chest",
		TransactionType = TransactionType.PURCHASE,
		TransactionServer = TransactionServer.APPLE
	});
}
```

## IAP 플러그인을 사용하여 자동 기록

[Unity IAP](https://docs.unity3d.com/Packages/com.unity.purchasing@latest?subfolder=/manual/Overview.html)에 의해 이루어진 거래는 수동으로 이벤트를 전송할 때와 동일한 `transaction` 이벤트 스키마를 사용하여 Analytics로 자동 전달됩니다. 이 거래 이벤트는 Analytics에서 제공하는 매출 KPI에 영향을 미칩니다. 실패한 거래는 IAP 플러그인에 의해 `transactionFailed` 이벤트로 자동 트래킹됩니다.

## 거래 확인

거래 확인을 통해 거래가 적법한 구매인지 확인합니다.

플랫폼에서는 대시보드에 표시되는 모든 매출이 해킹되거나 탈옥된 게임의 결과가 아닌 실제 매출임을 보장하기 위해 iOS 및 Android 스토어의 영수증을 확인할 수 있습니다.

거래 확인을 사용하려면 다음 조건을 충족해야 합니다.

* Analytics 플랫폼이 해당 스토어의 영수증을 확인할 수 있도록 관련 스토어의 확인 키를 제공합니다. [대시보드](https://dashboard.unity3d.com/)의 **Project Settings**에서 이 작업을 수행할 수 있습니다.
* `transaction` 이벤트에 영수증 확인 파라미터를 포함하여 플랫폼에 거래를 확인해야 한다고 알립니다. 이때 알리는 방법은 스토어에 따라 다릅니다.

거래 이벤트에는 IAP 영수증 확인 서비스에서 사용되는 `revenueValidated` 파라미터가 포함되어 있으며, 매출 확인을 사용하면 이 파라미터가 자동으로 채워집니다. 사용 가능한 값은 다음과 같습니다.

| 값 | 결과                                       |
| - | ---------------------------------------- |
| 0 | 이 거래가 매출 확인 서비스를 사용하지 않았으며 매출 차트에 포함됩니다. |
| 1 | 이 거래가 확인을 통과했으며 매출 차트에 포함됩니다.            |
| 2 | 이 거래가 확인에 실패했으며 매출 차트에 포함되지 않습니다.        |
| 3 | 오류로 인해 이 거래를 확인하지 못했으며 매출 차트에 포함되지 않습니다. |

### Apple 스토어##report-successful-purchases

Analytics에서는 iOS 거래에 자동으로 영수증 확인을 시도합니다. iOS 거래에 다음 내용을 추가하여 영수증을 확인합니다.

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

`transactionReceipt `는 Apple에서 반환되는 BASE 64로 인코딩된 영수증 데이터입니다. Apple 개발자 웹사이트에서 [Apple 영수증 확인](https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html)(영문)에 대해 자세히 알아보십시오.

### Google Play##report-failed-purchases

Google 공개 키를 제공해야 합니다. Google 키를 업데이트하려면 **Project Settings** > **In-app purchase (IAP) settings**로 이동하여 Google 라이선스 키를 입력합니다.

Android 거래에 대한 거래 이벤트에 다음 내용을 추가하여 영수증을 확인합니다.

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

**중요**: 거래 영수증과 비공개 키가 `transactionReceiptSignature`를 생성하므로, 영수증을 확인하려면 이 서명이 Google에서 제공하는 정확한 문자열이어야 하고 중첩된 JSON 객체가 아닌 문자열로 전달되어야 합니다.
