2222import com .segment .analytics .integrations .Integration ;
2323import com .segment .analytics .integrations .Logger ;
2424import com .segment .analytics .integrations .TrackPayload ;
25+ import com .segment .analytics .internal .Utils ;
2526import java .math .BigDecimal ;
2627import java .util .Arrays ;
2728import 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
0 commit comments