Sending in-app purchases on Android

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 SDK method withRevenueAutoTrackingEnabled.

Note

Automatic collection of in-app purchase data when using Google Play Billing Library version 3.x is available in the AppMetrica SDK version 4.0.0 and higher. Automatic collection for Google Play Billing Library 4.0.0 is available in the AppMetrica SDK version 4.2.0 and higher.

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. For more information 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 hasn't been 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 the AppMetrica interface.

Step 2. (Optional) Enable validation

Local validation with a public key is used to validate purchases on Android.

To enable validation, create a public key and specify it in the settings.

  1. In the Google interface, get a license key. For more information, see the Google documentation.
  2. In the AppMetrica interface, go to the app settings from the menu on the left.
  3. Go to the Revenue tab.
  4. Under Revenue validation for Google Play, enter the license key.
  5. Click Enable validation.

Alert

If validation is enabled, the Revenue report displays purchases that were validated or were sent without the Revenue.Receipt instance.

Step 3. Test sending Revenue

This section outlines the steps for sending Revenue to the additional API key:

To validate purchases on Android, configure sending the Revenue.Receipt instance along with the Revenue:

  1. Create the Revenue.Receipt instance with information about the purchase and signature. You must use it when creating the Revenue instance in Revenue.Builder.withReceipt (Revenue.Receipt receipt).
  2. Create the Revenue instance using the Revenue.Builder constructor.
  3. Send the Revenue instance to the test API key using the IReporter. For more information about reporters, see Usage examples.
void handlePurchase(Purchase purchase) {
    ...
    // Creating the Revenue.Receipt instance.
    // It is used for checking purchases in Google Play.
    Revenue.Receipt revenueReceipt = Revenue.Receipt.newBuilder()
            .withData(purchase.getOriginalJson())
            .withSignature(purchase.getSignature())
            .build();
    // Creating the Revenue instance.
    Revenue revenue = Revenue.newBuilder(99000000, Currency.getInstance("RUB"))
            .withProductID("com.yandex.service.299")
            .withQuantity(2)
            .withReceipt(revenueReceipt)
            .withPayload("{\"source\":\"Google Play\"}")
            .build();
    // Sending the Revenue instance using reporter.
    AppMetrica.getReporter(getApplicationContext(), "Testing API key").reportRevenue(revenue);
}

To send information about a purchase without validation:

  1. Create the Revenue instance using the Revenue.Builder constructor.

  2. (Optional) To group purchases by OrderID, specify it in the Revenue.Builder.withPayload(String payload) method.

    Note

    If the OrderID is not specified, AppMetrica generates the ID automatically.

  3. Send the Revenue instance to the test API key using the IReporter. For more information about reporters, see Usage examples.

// Creating the Revenue instance.
Revenue revenue = Revenue.newBuilder(99000000, Currency.getInstance("RUB"))
        .withProductID("com.yandex.service.299")
        .withQuantity(2)
        // Passing the OrderID parameter in the .withPayload(String payload) method to group purchases.
        .withPayload("{\"OrderID\":\"Identifier\", \"source\":\"Google Play\"}")
        .build();
// Sending the Revenue instance using reporter.
AppMetrica.getReporter(getApplicationContext(), "Testing API key").reportRevenue(revenue);

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 Revenue instance to the main API key, use the AppMetrica.reportRevenue(Revenue revenue) method.

...
// Sending the Revenue instance.
AppMetrica.reportRevenue(revenue);

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