Sending information about a purchase on iOS

AppMetrica doesn't allow you to create segments of the “test” and “not test” revenue. If you use the main API key for debugging purchases, the test purchases will be included in general statistics. Therefore, to debug revenue sending, use a reporter to send statistics to the additional API key. For more information about reporters, see Usage examples.
  1. Step 1. Create a test app in AppMetrica
  2. Step 2. (Optional) Enable validation
  3. Step 3. Test sending revenue
  4. Step 4. Make sure that purchases are shown in the reports.
  5. Step 5. Configure sending revenue to the main API key

Step 1. Create a test app in AppMetrica

  1. In the AppMetrica interface, click Add application.

  2. Fill in the fields in the form.
  3. Click Create. By clicking the button, you accept the terms of user agreement.
Note. Different versions of the same app can be grouped in a single folder. To do this, create a folder using the button and move the apps to it.

Step 2. (Optional) Enable validation

Purchases on iOS are validated using the iTunes API resources.

To enable the validation:
  1. In the AppMetrica interface, go to the app settings from the menu on the left.
  2. Go to the Revenue tab.
  3. Under Validate purchases for App Store, click Enable validation.
Attention. If validation is enabled, the Revenue report displays purchases that were validated or were sent without the transactionID and receiptData fields.

Step 3. Test sending revenue

This section explains the steps for sending revenue to the additional API key:

To validate purchases on iOS, configure sending the transactionID and receiptData fields in the implementation of transaction completion:
  1. Initialize the YMMMutableRevenueInfo instance.
  2. To validate a purchase, specify transactionID and receiptData. You should receive them before invoking [[SKPaymentQueue defaultQueue] finishTransaction:transaction].
  3. Send the YMMMutableRevenueInfo instance to the test API key using the YMMYandexMetricaReporting reporter. For more information about reporters, see Usage examples.
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
    ...
    NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"2100.5"];
    // Initializing the Revenue instance.
    YMMMutableRevenueInfo *revenueInfo = [[YMMMutableRevenueInfo alloc] initWithPriceDecimal:price currency:@"BYN"];
    revenueInfo.productID = @"TV soundbar";
    revenueInfo.quantity = 2;
    revenueInfo.payload = @{ @"source": @"AppStore" };
    // Set purchase information for validation.
    revenueInfo.transactionID = transaction.transactionIdentifier;
    revenueInfo.receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
    // Sending the Revenue instance using reporter.
    id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
    [reporter reportRevenue:[revenueInfo copy] onFailure:^(NSError *error) {
        NSLog(@"Revenue error: %@", error);
    }];
    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
Copied to clipboard

Step 4. Make sure that purchases are shown in the reports.

  1. Make test in-app purchases.
  2. Make sure that the Revenue report shows the same number of purchases and total revenue as the sent ones.

    Information in the report may be missing if:

    • Validation is enabled and the purchase failed it.
    • Information about the purchase was not sent.
  3. If there is no data in the report, export all purchases using the Logs API:

    curl -X GET \
      'https://api.appmetrica.yandex.ru/logs/v1/export/events.json?application_id=1111&date_since=2018-10-10&date_until=2018-10-11&fields=revenue_order_id,revenue_quantity,revenue_price,revenue_currency,is_revenue_verified' \
      -H 'Authorization: OAuth oauth_token'
    Copied to clipboard
    If there are events in the export and the is_revenue_verified field is set to false, the purchases were not validated.

Step 5. Configure sending revenue to the main API key

After debugging, repeat steps 2-4 for the main API key.

To send the YMMMutableRevenueInfo instance to the main API key, use the +reportRevenue:onFailure: method of the YMMYandexMetrica class.

...
// Sending the Revenue instance.
[YMMYandexMetrica reportRevenue:[revenueInfo copy] onFailure:^(NSError *error) {
    NSLog(@"Revenue error: %@", error);
}];
Copied to clipboard