diff --git a/android-core/src/main/java/com/mparticle/MParticle.java b/android-core/src/main/java/com/mparticle/MParticle.java index f1f1785a1..7246d51ee 100644 --- a/android-core/src/main/java/com/mparticle/MParticle.java +++ b/android-core/src/main/java/com/mparticle/MParticle.java @@ -1269,18 +1269,18 @@ public static void removeListener(SdkListener listener) { * @see #logEvent(BaseEvent) */ public enum EventType { + Unknown(0), Navigation(1), Location(2), Search(3), Transaction(4), UserContent(5), UserPreference(6), Social(7), Other(8), Media(9); - Unknown(0), Navigation(1), Location(2), Search(3), Transaction(4), UserContent(5), UserPreference(6), - Social(7), Other(8), Media(9), AddToCart(10), RemoveFromCart(11), Checkout(12), CheckoutOption(13), - Click(14), ViewDetail(15), Purchase(16), Refund(17), PromotionView(18), PromotionClick(19), AddToWishlist(20), - RemoveFromWishlist(21), Impression(22); private final int value; EventType(final int newValue) { value = newValue; } - public int getValue() { return value; } + public int getValue() { + return value; + } + @NonNull public String toString() { return name(); diff --git a/android-core/src/main/java/com/mparticle/internal/MParticleJSInterface.java b/android-core/src/main/java/com/mparticle/internal/MParticleJSInterface.java index bc4898421..07966ee98 100644 --- a/android-core/src/main/java/com/mparticle/internal/MParticleJSInterface.java +++ b/android-core/src/main/java/com/mparticle/internal/MParticleJSInterface.java @@ -479,32 +479,6 @@ EventType convertEventType(int eventType) { return EventType.Social; case 9: return EventType.Media; - case 10: - return EventType.AddToCart; - case 11: - return EventType.RemoveFromCart; - case 12: - return EventType.Checkout; - case 13: - return EventType.CheckoutOption; - case 14: - return EventType.Click; - case 15: - return EventType.ViewDetail; - case 16: - return EventType.Purchase; - case 17: - return EventType.Refund; - case 18: - return EventType.PromotionView; - case 19: - return EventType.PromotionClick; - case 20: - return EventType.AddToWishlist; - case 21: - return EventType.RemoveFromWishlist; - case 22: - return EventType.Impression; default: return EventType.Other; } diff --git a/android-kit-base/src/main/java/com/mparticle/kits/CommerceEventUtils.java b/android-kit-base/src/main/java/com/mparticle/kits/CommerceEventUtils.java index 33085215f..4b07382ad 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/CommerceEventUtils.java +++ b/android-kit-base/src/main/java/com/mparticle/kits/CommerceEventUtils.java @@ -9,8 +9,12 @@ import com.mparticle.commerce.Product; import com.mparticle.commerce.Promotion; import com.mparticle.commerce.TransactionAttributes; +import com.mparticle.internal.Logger; import com.mparticle.internal.MPUtility; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -56,42 +60,7 @@ public static List expandProductAction(CommerceEvent event) { List products = event.getProducts(); if (products != null) { for (int i = 0; i < products.size(); i++) { - MPEvent.Builder itemEvent; - switch (productAction) { - case Product.ADD_TO_CART: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.AddToCart); - break; - case Product.CLICK: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Click); - break; - case Product.ADD_TO_WISHLIST: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.AddToWishlist); - break; - case Product.CHECKOUT: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Checkout); - break; - case Product.CHECKOUT_OPTION: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.CheckoutOption); - break; - case Product.DETAIL: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.ViewDetail); - break; - case Product.PURCHASE: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Purchase); - break; - case Product.REFUND: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Refund); - break; - case Product.REMOVE_FROM_CART: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.RemoveFromCart); - break; - case Product.REMOVE_FROM_WISHLIST: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.RemoveFromWishlist); - break; - default: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Transaction); - } - + MPEvent.Builder itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Transaction); Map attributes = new HashMap(); OnAttributeExtracted attributeExtracted = new StringAttributeExtractor(attributes); extractProductFields(products.get(i), attributeExtracted); @@ -219,41 +188,7 @@ public static List expandPromotionAction(CommerceEvent event) { List promotions = event.getPromotions(); if (promotions != null) { for (int i = 0; i < promotions.size(); i++) { - MPEvent.Builder itemEvent; - switch (promotionAction) { - case Product.ADD_TO_CART: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.AddToCart); - break; - case Product.CLICK: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Click); - break; - case Product.ADD_TO_WISHLIST: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.AddToWishlist); - break; - case Product.CHECKOUT: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Checkout); - break; - case Product.CHECKOUT_OPTION: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.CheckoutOption); - break; - case Product.DETAIL: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.ViewDetail); - break; - case Product.PURCHASE: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Purchase); - break; - case Product.REFUND: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Refund); - break; - case Product.REMOVE_FROM_CART: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.RemoveFromCart); - break; - case Product.REMOVE_FROM_WISHLIST: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.RemoveFromWishlist); - break; - default: - itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Transaction); - } + MPEvent.Builder itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Transaction); Map attributes = new HashMap(); if (event.getCustomAttributeStrings() != null) { attributes.putAll(event.getCustomAttributeStrings()); @@ -390,6 +325,119 @@ public static int getEventType(CommerceEvent filteredEvent) { return Constants.EVENT_TYPE_IMPRESSION; } + public static Map convertCommerceEventToMap(CommerceEvent event) { + + Map map = new HashMap<>(); + + if (event.getProducts()!=null && event.getProducts().size() > 0) { + for (Product product : event.getProducts()) { + if (product.getName() != null) { + map.put(Constants.ATT_PRODUCT_NAME, product.getName()); + } + if (product.getBrand() != null) { + map.put(Constants.ATT_PRODUCT_BRAND, product.getBrand()); + } + if (product.getCategory() != null) { + map.put(Constants.ATT_PRODUCT_CATEGORY, product.getCategory()); + } + if (product.getVariant() != null) { + map.put(Constants.ATT_PRODUCT_VARIANT, product.getVariant()); + } + if (product.getPosition() != null) { + map.put(Constants.ATT_PRODUCT_POSITION, product.getPosition().toString()); + } + if (product.getUnitPrice() > 0.0) { + map.put(Constants.ATT_PRODUCT_PRICE, Double.toString(product.getUnitPrice())); + } + if (product.getQuantity() > 0.0) { + map.put(Constants.ATT_PRODUCT_QUANTITY, Double.toString(product.getQuantity())); + } + if (product.getCouponCode() != null) { + map.put(Constants.ATT_PRODUCT_COUPON_CODE, product.getCouponCode()); + } + if (product.getTotalAmount() > 0.0) { + map.put(Constants.ATT_TOTAL, Double.toString(product.getTotalAmount())); + } + if (product.getCustomAttributes() != null) { + map.putAll(product.getCustomAttributes()); + } + } + } + if (event.getCheckoutStep() != null) { + map.put(Constants.ATT_ACTION_CHECKOUT_STEP, event.getCheckoutStep().toString()); + } + if (event.getCheckoutOptions() != null) { + map.put(Constants.ATT_ACTION_CHECKOUT_OPTIONS, event.getCheckoutOptions()); + } + if (event.getCurrency() != null) { + map.put(Constants.ATT_ACTION_CURRENCY_CODE, event.getCurrency()); + } + if (event.getProductListName() != null) { + map.put(Constants.ATT_ACTION_PRODUCT_ACTION_LIST, event.getProductListName()); + } + if (event.getProductListSource() != null) { + map.put(Constants.ATT_ACTION_PRODUCT_LIST_SOURCE, event.getProductListSource()); + } + TransactionAttributes attributes = event.getTransactionAttributes(); + if (attributes != null) { + if (attributes.getId() != null) { + map.put(Constants.ATT_TRANSACTION_ID, attributes.getId()); + } + if (attributes.getAffiliation() != null) { + map.put(Constants.ATT_AFFILIATION, attributes.getAffiliation()); + } + if (attributes.getRevenue() != null) { + map.put(Constants.ATT_TOTAL, attributes.getRevenue().toString()); + } + if (attributes.getTax() != null) { + map.put(Constants.ATT_TAX, attributes.getTax().toString()); + } + if (attributes.getShipping() != null) { + map.put(Constants.ATT_SHIPPING, attributes.getShipping().toString()); + } + if (attributes.getCouponCode() != null) { + map.put(Constants.ATT_TRANSACTION_COUPON_CODE, attributes.getCouponCode()); + } + } + if (event.getPromotions()!=null && event.getPromotions().size() > 0) { + for (Promotion promotion : event.getPromotions()) { + if (promotion.getId() != null) { + map.put(Constants.ATT_PROMOTION_ID, promotion.getId()); + } + if (promotion.getName() != null) { + map.put(Constants.ATT_PROMOTION_NAME, promotion.getName()); + } + if (promotion.getCreative() != null) { + map.put(Constants.ATT_PROMOTION_CREATIVE, promotion.getCreative()); + } + if (promotion.getPosition() != null) { + map.put(Constants.ATT_PROMOTION_POSITION, promotion.getPosition()); + } + } + } + if (event.getImpressions()!=null && event.getImpressions().size() > 0) { + for (Impression impression : event.getImpressions()) { + if (impression.getListName() != null) { + map.put(com.mparticle.internal.Constants.Commerce.IMPRESSION_LOCATION, impression.getListName()); + } + if (impression.getProducts().size() > 0) { + JSONArray productsJson = new JSONArray(); + try { + for (Product product : impression.getProducts()) { + productsJson.put(new JSONObject(product.toString())); + } + } catch (Exception e) { + Logger.error("Error while parsing event impression data"); + } + if (productsJson.length() > 0) { + map.put(com.mparticle.internal.Constants.Commerce.IMPRESSION_PRODUCT_LIST, productsJson.toString()); + } + } + } + } + return map; + } + public interface Constants { String ATT_AFFILIATION = "Affiliation"; diff --git a/android-kit-base/src/test/kotlin/com/mparticle/kits/CommerceEventUtilsTest.kt b/android-kit-base/src/test/kotlin/com/mparticle/kits/CommerceEventUtilsTest.kt index b2c9faec0..c1d43d8d2 100644 --- a/android-kit-base/src/test/kotlin/com/mparticle/kits/CommerceEventUtilsTest.kt +++ b/android-kit-base/src/test/kotlin/com/mparticle/kits/CommerceEventUtilsTest.kt @@ -13,156 +13,4 @@ class CommerceEventUtilsTest { Assert.assertNotNull(CommerceEventUtils.expand(null)) Assert.assertEquals(0, CommerceEventUtils.expand(null).size.toLong()) } - - @Test - @Throws(Exception::class) - fun testProductExpansion_AddToCart() { - val product = Product.Builder("Double Room - Econ Rate", "econ-1", 100.00) - .quantity(4.0) - .build() - val event = CommerceEvent.Builder(Product.ADD_TO_CART, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.AddToCart, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_CLICK() { - val product = Product.Builder("Double Room - Econ Rate", "econ-1", 100.00) - .quantity(4.0) - .build() - val event = CommerceEvent.Builder(Product.CLICK, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.Click, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_ADD_TO_WISHLIST() { - val product = Product.Builder("Double Room - Econ Rate", "econ-1", 100.00) - .quantity(4.0) - .build() - val event = CommerceEvent.Builder(Product.ADD_TO_WISHLIST, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.AddToWishlist, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_CHECKOUT() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.CHECKOUT, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.Checkout, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_CHECKOUT_OPTION() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.CHECKOUT_OPTION, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.CheckoutOption, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_DETAIL() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.DETAIL, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.ViewDetail, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_PURCHASE() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.PURCHASE, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.Transaction, events.get(0).eventType) - Assert.assertEquals(EventType.Purchase, events.get(1).eventType) - Assert.assertEquals(2, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_REFUND() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.REFUND, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.Transaction, events.get(0).eventType) - Assert.assertEquals(EventType.Refund, events.get(1).eventType) - Assert.assertEquals(2, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_REMOVE_FROM_CART() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.REMOVE_FROM_CART, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.RemoveFromCart, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } - - @Test - @Throws(Exception::class) - fun testProductExpansion_REMOVE_FROM_WISHLIST() { - val product = Product.Builder("Unisex Tee", "128747", 18.00) - .quantity(3.0) - .build() - val event = CommerceEvent.Builder(Product.REMOVE_FROM_WISHLIST, product) - .build() - - val events = CommerceEventUtils.expand(event) - Assert.assertNotNull(CommerceEventUtils.expand(event)) - Assert.assertEquals(EventType.RemoveFromWishlist, events.get(0).eventType) - Assert.assertEquals(1, events.size) - } }