Tracking Monetization Events

In Leanplum, we offer additional capability to can track purchases or other monetization events. You can do this automatically through our In-App purchase integrations with the iOS and Google Playstore. You can also do this manually by sending a Purchase event. Once in Leanplum, you will see the revenue metrics in your Analytics reports. Below are how to implement each.

In-app purchases (iOS)

Leanplum supports receipt validation with the App Store and Google Play. On iOS, you can track in-app purchases automatically. Simply add this line of code before Leanplum starts:

Leanplum.trackInAppPurchases()
[Leanplum trackInAppPurchases];
//For iOS
Leanplum.TrackIOSInAppPurchases();
Leanplum.trackInAppPurchasesIos()

Leanplum will also convert all purchases to USD. To require valid receipts upon purchase or change your preferred currency, update your preferences in App Settings -> Keys & Settings -> In-App Purchases in the dashboard.

📘

While setting up the TrackIOSInAppPurchase for your Unity app, you need to provide info about the following parameters: purchase item name, price for one unit, quantity. e.g. Leanplum.TrackIOSInAppPurchase("test", 2.33, 1);

In-app purchases (Android)

If your app uses Google Play In-App Billing, Leanplum can automatically track those purchases and validate receipts with the Google Play store. First, provide us with your Google Play license key. Next, add the following code sample in your class that implements com.android.vending.billing.util.IabHelper.OnIabPurchaseFinishedListener:

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.android.vending.billing.util.IabHelper.QueryInventoryFinishedListener;
import com.android.vending.billing.util.IabResult;
import com.android.vending.billing.util.Inventory;
import com.android.vending.billing.util.Purchase;
import com.android.vending.billing.util.SkuDetails;

import com.leanplum.Leanplum;
...
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
  final Purchase purchase = info;
  billingHelper.queryInventoryAsync(new QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inv) {
      // Track in-app purchase.
      SkuDetails skuDetails = inv.getSkuDetails(purchase.getSku());
      try {
        JSONObject skuData = new JSONObject(skuDetails.toString()
            .substring(skuDetails.toString().indexOf(":") + 1));
        Leanplum.trackGooglePlayPurchase(
            skuData.getString("title"), // Alternatively, skuData.getString("productId"), if you like.
            skuData.getLong("price_amount_micros"),
            skuData.getString("price_currency_code"),
            purchase.getOriginalJson(), purchase.getSignature()
            /* optionally supply event parameters as an additional argument */);
      } catch (JSONException e) {
        Log.e("Leanplum", "Cannot get purchase price from improperly formatted SKU");
      }

      // Code to consume purchase.
      // It's important that you do not consume while querying inventory, or you'll receive
      // an IllegalStateException.
      // billingHelper.consumeAsync(purchase, null);
    }
  });
}

📘

While setting up the TrackGooglePlayPurchase for your Unity app, you need to provide info about the following parameters: purchase item name, price, currency code. e.g. Leanplum.TrackGooglePlayPurchase("test", 11.99, "EUR");

Manually tracking monetization events

You can manually track your monetization event within the app. To do so you have to pass a purchase event name, a purchase value, the currency code, and parameters with info about the purchase event with each purchase (all the parameters are required).

The purchase event name is defined by default as “Purchase”. Based on the currency code being passed, the value will be converted to USD automatically in the Dashboard.

Leanplum.track(event: LP_PURCHASE_EVENT, value: 5.0, currencyCode: "EUR", params: ["serial" : 12345, "name":"coffee"])
[Leanplum trackPurchase:LP_PURCHASE_EVENT withValue:5.0 andCurrencyCode:@"EUR" andParameters:@{@"serial": @12345, @"name": @"coffee"}];
Map<String, Object> purchaseParams = new HashMap<String, Object>();
...
purchaseParams.put("Item code", 12345);
purchaseParams.put("Name", "Coffee");
Leanplum.trackPurchase(Leanplum.PURCHASE_EVENT_NAME, 5, "EUR", purchaseParams);
// Leanplum JS SDK 1.5.0+
// Leanplum.trackPurchase(value, currencyCode, purchaseParams, purchaseEvent)

Leanplum.trackPurchase(5.00, 'EUR', { serial: 12345, name: 'coffee' })
Leanplum.trackPurchase(value, currencyCode, purchaseParams, purchaseEvent)

Other monetization events

You can also use the standard event tracking for monetization events as well. It works like any other event — just use "Purchase" as the event name and a value.

The only difference is that unlike using trackPurchase, the normal Leanplum.track call will not automatically convert different currencies to USD.

Leanplum.track(event: LP_PURCHASE_EVENT, value:19.99)
[Leanplum track:LP_PURCHASE_EVENT withValue:19.99];
Leanplum.track(Leanplum.PURCHASE_EVENT_NAME, 19.99, item);
Leanplum.track('Purchase', 19.99, { itemsInCart: 4 });
Leanplum.track(PURCHASE_EVENT_NAME, {itemsInCart: 4});

The value should be the revenue for the transaction in a common currency.

If you prefer to use a different event name than "Purchase", you can choose a different name.

  1. Select the [+] tile on the Analytics page for a new metric
  2. In the dialogue box that opens, select the Monetization category and edit the purchase event
  3. The changes will be applied retroactively.

🚧

Whether you choose your own name for purchase events or use the default LP_PURCHASE_EVENT, make sure to stay consistent and always use the same event name.