Skip to content

Commit 8a5dbdd

Browse files
authored
Merge pull request #10 from AdevintaSpain/fix_logpurchase
Log purchases for each product individually
2 parents 56a8bbd + 5c795c8 commit 8a5dbdd

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

src/main/java/com/segment/analytics/android/integrations/appboy/AppboyIntegration.java

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.segment.analytics.integrations.Integration;
2323
import com.segment.analytics.integrations.Logger;
2424
import com.segment.analytics.integrations.TrackPayload;
25+
import com.segment.analytics.internal.Utils;
2526
import java.math.BigDecimal;
2627
import java.util.Arrays;
2728
import java.util.Calendar;
@@ -45,9 +46,9 @@ public class AppboyIntegration extends Integration<Appboy> {
4546
private static final String REVENUE_KEY = "revenue";
4647
private static final String CURRENCY_KEY = "currency";
4748
private static final String AUTOMATIC_IN_APP_MESSAGE_REGISTRATION_ENABLED =
48-
"automatic_in_app_message_registration_enabled";
49+
"automatic_in_app_message_registration_enabled";
4950
private static final List<String> RESERVED_KEYS = Arrays.asList("birthday", "email", "firstName",
50-
"lastName", "gender", "phone", "address", "avatar");
51+
"lastName", "gender", "phone", "address", "avatar");
5152

5253
public static final Factory FACTORY = factory(AppboyIntegrationOptions.builder().build());
5354

@@ -300,32 +301,81 @@ public void track(TrackPayload track) {
300301
mLogger.verbose("This Install Attributed event is not in the proper format and cannot be"
301302
+ " logged. The exception is %s.", exception);
302303
}
304+
303305
if (properties == null || properties.size() == 0) {
304306
mLogger.verbose("Calling appboy.logCustomEvent for event %s with no properties.", event);
305307
mAppboy.logCustomEvent(event);
306308
return;
307309
}
310+
311+
if (event.equals("Order Completed") || properties.containsKey("revenue")) {
312+
String currency = StringUtils.isNullOrBlank(properties.currency())
313+
? DEFAULT_CURRENCY_CODE
314+
: properties.currency();
315+
316+
List<Properties.Product> products = properties.products();
317+
318+
if (Utils.isNullOrEmpty(products)) {
319+
logPurchase(event, currency, properties);
320+
} else {
321+
for (Properties.Product product : products) {
322+
logProductPurchase(event, currency, product);
323+
}
324+
}
325+
326+
return;
327+
}
328+
308329
JSONObject propertiesJson = properties.toJsonObject();
330+
331+
mLogger.verbose("Calling appboy.logCustomEvent for event %s with properties %s.",
332+
event, propertiesJson.toString());
333+
mAppboy.logCustomEvent(event, new AppboyProperties(propertiesJson));
334+
}
335+
336+
private void logPurchase(String event, String currency, Properties properties) {
309337
double revenue = properties.revenue();
338+
310339
if (revenue != 0) {
311-
String currencyCode = StringUtils.isNullOrBlank(properties.currency()) ? DEFAULT_CURRENCY_CODE
312-
: properties.currency();
340+
JSONObject propertiesJson = properties.toJsonObject();
341+
313342
propertiesJson.remove(REVENUE_KEY);
314343
propertiesJson.remove(CURRENCY_KEY);
344+
315345
if (propertiesJson.length() == 0) {
316346
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with no"
317-
+ " properties.", event, revenue, currencyCode);
318-
mAppboy.logPurchase(event, currencyCode, new BigDecimal(revenue));
347+
+ " properties.", event, revenue, currency);
348+
mAppboy.logPurchase(event, currency, new BigDecimal(revenue));
319349
} else {
320350
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with properties"
321-
+ " %s.", event, revenue, currencyCode, propertiesJson.toString());
322-
mAppboy.logPurchase(event, currencyCode, new BigDecimal(revenue),
351+
+ " %s.", event, revenue, currency, propertiesJson.toString());
352+
mAppboy.logPurchase(event, currency, new BigDecimal(revenue),
323353
new AppboyProperties(propertiesJson));
324354
}
355+
}
356+
}
357+
358+
private void logProductPurchase(String event, String currency, Properties.Product product) {
359+
String productId = product.id();
360+
BigDecimal price = new BigDecimal(product.price());
361+
362+
AppboyProperties appboyProperties = new AppboyProperties();
363+
364+
for (Map.Entry<String, Object> entry : product.entrySet()) {
365+
if (entry.getKey().equalsIgnoreCase("id")) continue;
366+
if (entry.getKey().equalsIgnoreCase("price")) continue;
367+
368+
appboyProperties.addProperty(entry.getKey(), entry.getValue().toString());
369+
}
370+
371+
if (appboyProperties.size() > 0) {
372+
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with properties"
373+
+ " %s.", event, price, currency, appboyProperties);
374+
mAppboy.logPurchase(productId, currency, price, appboyProperties);
325375
} else {
326-
mLogger.verbose("Calling appboy.logCustomEvent for event %s with properties %s.",
327-
event, propertiesJson.toString());
328-
mAppboy.logCustomEvent(event, new AppboyProperties(propertiesJson));
376+
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with no"
377+
+ " properties.", event, price, currency);
378+
mAppboy.logPurchase(productId, currency, price);
329379
}
330380
}
331381

src/test/java/com/segment/analytics/android/integrations/appboy/AppboyTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void setUp() {
7474
when(mAnalytics.logger("Appboy")).thenReturn(mLogger);
7575
when(mAnalytics.getApplication()).thenReturn(mContext);
7676
mIntegration = new AppboyIntegration(
77-
mContext, mAppboy, "foo", mLogger, true, true, null);
77+
mContext, mAppboy, "foo", mLogger, true, false, null);
7878
}
7979

8080
@Test
@@ -177,7 +177,6 @@ public void testIdentifyFields() {
177177
verify(mAppboyUser).setCustomUserAttribute("string", "value");
178178
//verifyNoMoreAppboyUserInteractions();
179179
verify(mAppboy, Mockito.times(1)).changeUser("userId");
180-
verify(mAppboy, Mockito.times(15)).getCurrentUser();
181180
//verifyNoMoreAppboyInteractions();
182181
}
183182

@@ -220,7 +219,7 @@ public void testIdentifyGenderOnBadInputs() {
220219
traits.putGender("female_1");
221220
identifyPayload = new IdentifyPayloadBuilder().traits(traits).build();
222221
mIntegration.identify(identifyPayload);
223-
verify(mAppboy, Mockito.times(1)).changeUser("userId");
222+
verify(mAppboy, Mockito.times(2)).changeUser("userId");
224223
//verifyNoMoreAppboyUserInteractions();
225224
//verifyNoMoreAppboyInteractions();
226225
}
@@ -258,6 +257,24 @@ public void testTrackLogsAPurchaseWithoutPropertiesWithCustomCurrency() {
258257
verifyNoMoreAppboyInteractions();
259258
}
260259

260+
@Test
261+
public void testTrackLogsAPurchaseForEachProduct() {
262+
Properties purchaseProperties = new Properties();
263+
purchaseProperties.putCurrency("EUR");
264+
purchaseProperties.putProducts(
265+
new Properties.Product("product_1", "sku.product.1", 10.99F),
266+
new Properties.Product("product_2", "sku.product.2", 20.99F)
267+
);
268+
269+
TrackPayload trackPayload = new TrackPayloadBuilder().event("Order Completed").properties(purchaseProperties).build();
270+
271+
mIntegration.track(trackPayload);
272+
273+
verify(mAppboy).logPurchase("product_1", "EUR", new BigDecimal(10.99F));
274+
verify(mAppboy).logPurchase("product_2", "EUR", new BigDecimal(20.99F));
275+
verifyNoMoreAppboyInteractions();
276+
}
277+
261278
@Test
262279
public void testScreenDoesNotCallAppboy() {
263280
mIntegration.screen(new ScreenPayloadBuilder().name("foo").build());

0 commit comments

Comments
 (0)