Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add enum types for commerce event #498

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions android-core/src/main/java/com/mparticle/MParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be an incorrect public api change that'd likely cause a lot of pain and bugs :-) please revert

this enum is the one for custom events. it isn't meant to be used for all event types. ios does it this way and it's been a bug and complaint for years - customers for example log regular custom events with incorrect event types.

create some other private enum if we need it internally? these enum values shouldn't be exposed to customers.

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,32 @@ 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,42 @@ public static List<MPEvent> expandProductAction(CommerceEvent event) {
List<Product> products = event.getProducts();
if (products != null) {
for (int i = 0; i < products.size(); i++) {
MPEvent.Builder itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.Transaction);
MPEvent.Builder itemEvent;
switch (productAction) {
case Product.ADD_TO_CART:
itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, productAction), MParticle.EventType.AddToCart);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this is exactly what customers do and is not correct. then we end up with weird data, broken filtering, integrations, etc.

MPEvent is custom event and cannot have AddToCart (or any of the others below) as a custom event type in our schema.

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);
}

Map<String, String> attributes = new HashMap<String, String>();
OnAttributeExtracted attributeExtracted = new StringAttributeExtractor(attributes);
extractProductFields(products.get(i), attributeExtracted);
Expand Down Expand Up @@ -184,7 +219,41 @@ public static List<MPEvent> expandPromotionAction(CommerceEvent event) {
List<Promotion> promotions = event.getPromotions();
if (promotions != null) {
for (int i = 0; i < promotions.size(); i++) {
MPEvent.Builder itemEvent = new MPEvent.Builder(String.format(ITEM_NAME, promotionAction), MParticle.EventType.Transaction);
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);
}
Map<String, String> attributes = new HashMap<String, String>();
if (event.getCustomAttributeStrings() != null) {
attributes.putAll(event.getCustomAttributeStrings());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mparticle.kits

import com.mparticle.MParticle.EventType
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Product
import org.junit.Assert
import org.junit.Test

Expand All @@ -10,4 +13,156 @@ 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)
}
}
Loading