Sending in-app purchases on iOS

For the AppMetrica SDK version 4.0 and higher, automatic collection of data about purchases within the app is available. To enable and disable automatic collection, use the revenueAutoTrackingEnabled property.

Testing and manually sending purchase information

AppMetrica doesn't let you segment between "test" and "non-test" revenue. If you use the main API key for debugging purchases, the test purchases are included in general statistics. Therefore, to debug Revenue sending, use a reporter to send statistics to the additional API key. To learn more about reporters, see Usage examples.

Step 1. Create a test app in AppMetrica

Specify the app parameters: link in the app store (if the app isn't published yet, leave the field empty), name, category, and time zone for generating reports.

To add another app, click Add app in the drop-down list in AppMetrica.

Step 2. (Optional) Enable validation

Purchases on iOS are validated using iTunes API resources.

To enable 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.

Paid subscriptions on iOS are validated using iTunes API resources and a Shared Secret key.

To enable validation, create a key and enter it in the settings:

  1. In the App Store Connect interface, generate a Shared Secret key. For more information about generating keys, see the Apple documentation.
  2. In the AppMetrica interface, go to the app settings from the menu on the left.
  3. Go to the Revenue tab.
  4. Click Automatically renewed subscriptions.
  5. Under Validate purchases for App Store, specify the Shared Secret.
  6. Click Validate subscriptions.

Alert

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 outlines the steps for sending Revenue to the additional API key:

To validate purchases on iOS, configure sending the transactionID field and receiptData where you implement the transaction completion:

  • Objective-C

    - (void)completeTransaction:(SKPaymentTransaction *)transaction
    {
        ...
        NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"2100.5"];
        // Initializing the Revenue instance.
        YMMMutableRevenueInfo *revenueInfo = [[YMMMutableRevenueInfo alloc] initWithPriceDecimal:price currency:@"BYN"];
        revenueInfo.productID = @"ru.yandex.nonconsumable.large-pack";
        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];
    }
    
  • Swift

    func completeTransaction(_ transaction: SKPaymentTransaction) {
        ...
        let price = NSDecimalNumber(string: "2100.5")
        // Initializing the Revenue instance.
        let revenueInfo = YMMMutableRevenueInfo.init(priceDecimal: price, currency: "BYN")
        revenueInfo.productID = "ru.yandex.nonconsumable.large-pack"
        revenueInfo.quantity = 2
        revenueInfo.payload = ["source": "AppStore"]
        // Set purchase information for validation.
        if let url = Bundle.main.appStoreReceiptURL, let data = try? Data(contentsOf: url), let transactionID = transaction.transactionIdentifier {
            revenueInfo.transactionID = transactionID
            revenueInfo.receiptData = data
        }
        // Sending the Revenue instance using reporter.
        let reporter = YMMYandexMetrica.reporterForApiKey("API_key")
        reporter.reportRevenue(revenueInfo, onFailure: { (error) in
            print("REPORT ERROR: \(error.localizedDescription)")
        })
        // Remove the transaction from the payment queue.
        SKPaymentQueue.default().finishTransaction(transaction)
    }
    

To send information about a purchase:

  • Objective-C

    NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"2100.5"];
    // Initializing the Revenue instance.
    YMMMutableRevenueInfo *revenueInfo = [[YMMMutableRevenueInfo alloc] initWithPriceDecimal:price currency:@"BYN"];
    revenueInfo.productID = @"ru.yandex.nonconsumable.large-pack";
    revenueInfo.quantity = 2;
    // Setting the OrderID parameter in the payload property to group purchases
    revenueInfo.payload = @{ @"OrderID": @"Identifier", @"source": @"AppStore" };
    // 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);
    }];
    
  • Swift

    let price = NSDecimalNumber(string: "2100.5")
    // Initializing the Revenue instance.
    let revenueInfo = YMMMutableRevenueInfo.init(priceDecimal: price, currency: "BYN")
    revenueInfo.productID = "ru.yandex.nonconsumable.large-pack"
    revenueInfo.quantity = 2
    // To group purchases, set the OrderID parameter in the payload property.
    revenueInfo.payload = ["OrderID": "Identifier", "source": "AppStore"]
    // Sending the Revenue instance using reporter.
    let reporter = YMMYandexMetrica.reporterForApiKey("API_key")
    reporter.reportRevenue(revenueInfo, onFailure: { (error) in
        print("REPORT ERROR: \(error.localizedDescription)")
    })
    

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

  1. Make in-app test 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.
    • Information about the purchase was not sent.
  3. If there is no data in the report, export all purchases using the Logs API resource:

    curl -X GET \
    'https://api.appmetrica.yandex.ru/logs/v1/export/revenue_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'
    

    If there are events in the export and the is_revenue_verified field is set to false, the purchases weren't 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/ reportRevenue(_:onFailure:) method of the YMMYandexMetrica class.

// ...
// Sending the Revenue instance.
YMMYandexMetrica.reportRevenue(revenueInfo, onFailure: { error in
    print("Revenue error: \(error)")
})
// ...
// Sending the Revenue instance.
YMMYandexMetrica.reportRevenue(revenueInfo, onFailure: { (error) in
    print("Revenue error: \(error)")
})

If you didn't find the answer you were looking for, you can use the feedback form to submit your question. Please describe the problem in as much detail as possible. Attach a screenshot if possible.

Contact support