Skip to content

Commit e2aa996

Browse files
authored
feat(api): adding purchase track method to manually track purchases o… (#77)
* feat(api): adding purchase track method to manually track purchases outside IAPs.
1 parent 4e6a8b4 commit e2aa996

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,35 @@ public static void track(final String event, double value, String info,
15521552
LeanplumInternal.track(event, value, info, params, null);
15531553
}
15541554

1555+
/**
1556+
* Manually track purchase event with currency code in your application. It is advised to use
1557+
* {@link Leanplum#trackGooglePlayPurchase} instead for in-app purchases.
1558+
*
1559+
* @param event Name of the event.
1560+
* @param value The value of the event. Can be price.
1561+
* @param currencyCode The currency code corresponding to the price.
1562+
* @param params Key-value pairs with metrics or data associated with the event. Parameters can be
1563+
* strings or numbers. You can use up to 200 different parameter names in your app.
1564+
*/
1565+
public static void trackPurchase(final String event, double value, String currencyCode,
1566+
Map<String, ?> params) {
1567+
try {
1568+
if (TextUtils.isEmpty(event)) {
1569+
Log.w("trackPurchase - Empty event parameter provided.");
1570+
}
1571+
1572+
final Map<String, String> requestArgs = new HashMap<>();
1573+
if (!TextUtils.isEmpty(currencyCode)) {
1574+
requestArgs.put(Constants.Params.IAP_CURRENCY_CODE, currencyCode);
1575+
}
1576+
1577+
LeanplumInternal.track(event, value, null, params, requestArgs);
1578+
} catch (Throwable t) {
1579+
Log.e("trackPurchase - Failed to track purchase event.");
1580+
Util.handleException(t);
1581+
}
1582+
}
1583+
15551584
/**
15561585
* Tracks an in-app purchase as a Purchase event.
15571586
*

AndroidSDKTests/src/test/java/com/leanplum/LeanplumTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.List;
5757
import java.util.Locale;
5858
import java.util.Map;
59+
import java.util.Objects;
5960
import java.util.concurrent.CountDownLatch;
6061
import java.util.concurrent.Semaphore;
6162
import java.util.concurrent.TimeUnit;
@@ -603,7 +604,7 @@ public void onRequest(String httpMethod, String apiMethod, Map<String, Object> p
603604
});
604605
Leanplum.trackGooglePlayPurchase(eventName, 10, "USD", "data", "signature");
605606

606-
// Validate request for purchae.
607+
// Validate request for purchase.
607608
RequestHelper.addRequestHandler(new RequestHelper.RequestHandler() {
608609
@Override
609610
public void onRequest(String httpMethod, String apiMethod, Map<String, Object> params) {
@@ -621,6 +622,23 @@ public void onRequest(String httpMethod, String apiMethod, Map<String, Object> p
621622
}
622623
});
623624
Leanplum.trackGooglePlayPurchase(eventName, 10, "USD", "data", "signature", new HashMap<String, Object>());
625+
626+
// Validate request for manual purchase.
627+
RequestHelper.addRequestHandler(new RequestHelper.RequestHandler() {
628+
@Override
629+
public void onRequest(String httpMethod, String apiMethod, Map<String, Object> params) {
630+
assertEquals(Constants.Methods.TRACK, apiMethod);
631+
632+
String requestEventName = (String) params.get("event");
633+
String requestEventValue = (String) params.get("value");
634+
String requestCurrencyCode = (String) params.get("currencyCode");
635+
636+
assertEquals(eventName, requestEventName);
637+
assertEquals("1.99", requestEventValue);
638+
assertEquals("USD", requestCurrencyCode);
639+
}
640+
});
641+
Leanplum.trackPurchase(eventName, 1.99, "USD", new HashMap<String, Objects>());
624642
}
625643

626644
@Test

0 commit comments

Comments
 (0)