Skip to content

Commit

Permalink
Custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
markvdouw committed Jun 28, 2023
1 parent 274143a commit 037a0f1
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 22 deletions.
29 changes: 22 additions & 7 deletions src/main/kotlin/com/mparticle/kits/AppboyKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
if (eventList != null) {
for (i in eventList.indices) {
try {
logEvent(eventList[i])
val e = eventList[i]
val map = mutableMapOf<String, String>()
event.customAttributeStrings?.let { map.putAll(it) }
for (pair in map) {
e.customAttributes?.put(pair.key, pair.value)
}
logEvent(e)
messages.add(ReportingMessage.fromEvent(this, event))
} catch (e: Exception) {
Logger.warning("Failed to call logCustomEvent to Appboy kit: $e")
Expand Down Expand Up @@ -523,7 +529,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,

val eventName = "eCommerce - %s"
if (!KitUtils.isEmpty(event?.productAction) &&
event?.productAction.equals(Product.PURCHASE,true)
event?.productAction.equals(Product.PURCHASE, true)
) {
Braze.Companion.getInstance(context).logPurchase(
String.format(eventName, event?.productAction),
Expand All @@ -534,11 +540,14 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
)
} else {
if (!KitUtils.isEmpty(event?.productAction)) {
Braze.getInstance(context).logCustomEvent(String.format(eventName, event?.productAction), properties)
Braze.getInstance(context)
.logCustomEvent(String.format(eventName, event?.productAction), properties)
} else if (!KitUtils.isEmpty(event?.promotionAction)) {
Braze.getInstance(context).logCustomEvent(String.format(eventName, event?.promotionAction), properties)
Braze.getInstance(context)
.logCustomEvent(String.format(eventName, event?.promotionAction), properties)
} else {
Braze.getInstance(context).logCustomEvent(String.format(eventName, "Impression"), properties)
Braze.getInstance(context)
.logCustomEvent(String.format(eventName, "Impression"), properties)
}
}
}
Expand Down Expand Up @@ -763,7 +772,10 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
for ((i, promotion) in promotionList.withIndex()) {
val promotionProperties = BrazeProperties()
promotion.creative?.let {
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE, it)
promotionProperties.addProperty(
CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE,
it
)
}
promotion.id?.let {
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_ID, it)
Expand All @@ -772,7 +784,10 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_NAME, it)
}
promotion.position?.let {
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION, it)
promotionProperties.addProperty(
CommerceEventUtils.Constants.ATT_PROMOTION_POSITION,
it
)
}
promotionArray[i] = promotionProperties
}
Expand Down
128 changes: 113 additions & 15 deletions src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,26 @@ class AppboyKitTests {
val productBrazeProperties = productArray[0]
if (productBrazeProperties is BrazeProperties) {
val productProperties = productBrazeProperties.properties
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_TOTAL_AMOUNT), 22.5)
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE), 4.5)
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_QUANTITY), 5.0)
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_ID), "sku1")
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_NAME), "product name")
Assert.assertEquals(
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_TOTAL_AMOUNT),
22.5
)
Assert.assertEquals(
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE),
4.5
)
Assert.assertEquals(
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_QUANTITY),
5.0
)
Assert.assertEquals(
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_ID),
"sku1"
)
Assert.assertEquals(
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_NAME),
"product name"
)
Assert.assertEquals(emptyAttributes, productProperties)
}
}
Expand Down Expand Up @@ -584,7 +599,7 @@ class AppboyKitTests {
id = "my_promo_1"
creative = "sale_banner_1"
name = "App-wide 50% off sale"
position ="dashboard_bottom"
position = "dashboard_bottom"
}
val commerceEvent = CommerceEvent.Builder(Promotion.VIEW, promotion)
.customAttributes(customAttributes)
Expand All @@ -604,10 +619,22 @@ class AppboyKitTests {
val promotionBrazeProperties = promotionArray[0]
if (promotionBrazeProperties is BrazeProperties) {
val promotionProperties = promotionBrazeProperties.properties
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_ID), "my_promo_1")
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_NAME), "App-wide 50% off sale")
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION), "dashboard_bottom")
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE), "sale_banner_1")
Assert.assertEquals(
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_ID),
"my_promo_1"
)
Assert.assertEquals(
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_NAME),
"App-wide 50% off sale"
)
Assert.assertEquals(
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION),
"dashboard_bottom"
)
Assert.assertEquals(
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE),
"sale_banner_1"
)
Assert.assertEquals(emptyAttributes, promotionProperties)
}
}
Expand Down Expand Up @@ -697,7 +724,10 @@ class AppboyKitTests {
val impressionBrazeProperties = impressionArray[0]
if (impressionBrazeProperties is BrazeProperties) {
val impressionProperties = impressionBrazeProperties.properties
Assert.assertEquals(impressionProperties.remove("Product Impression List"), "Suggested Products List")
Assert.assertEquals(
impressionProperties.remove("Product Impression List"),
"Suggested Products List"
)
val productArray = impressionProperties.remove(AppboyKit.PRODUCT_KEY)
Assert.assertTrue(productArray is Array<*>)
if (productArray is Array<*>) {
Expand Down Expand Up @@ -725,11 +755,19 @@ class AppboyKitTests {
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE),
4.5
)
val brazeProductCustomAttributesDictionary = productProperties.remove(AppboyKit.CUSTOM_ATTRIBUTES_KEY)
val brazeProductCustomAttributesDictionary =
productProperties.remove(AppboyKit.CUSTOM_ATTRIBUTES_KEY)
if (brazeProductCustomAttributesDictionary is BrazeProperties) {
val customProductAttributesDictionary = brazeProductCustomAttributesDictionary.properties
Assert.assertEquals(customProductAttributesDictionary.remove("key1"), "value1")
Assert.assertEquals(customProductAttributesDictionary.remove("key #2"), "value #3")
val customProductAttributesDictionary =
brazeProductCustomAttributesDictionary.properties
Assert.assertEquals(
customProductAttributesDictionary.remove("key1"),
"value1"
)
Assert.assertEquals(
customProductAttributesDictionary.remove("key #2"),
"value #3"
)
Assert.assertEquals(emptyAttributes, customProductAttributesDictionary)
}
Assert.assertEquals(emptyAttributes, productProperties)
Expand Down Expand Up @@ -797,6 +835,66 @@ class AppboyKitTests {
// Assert.assertEquals(0, properties.size.toLong())
// }

// @Test
// fun testLogCommerceEvent() {
// val kit = MockAppboyKit()
//
// val product: Product = Product.Builder("La Enchilada", "13061043670", 12.5)
// .quantity(1.0)
// .build()
//
// val txAttributes = TransactionAttributes()
// .setRevenue(product.getTotalAmount())
//
// kit.configuration = MockKitConfiguration()
// val customAttributes: MutableMap<String, String> = HashMap()
// customAttributes["currentLocationLongitude"] = "2.1811267"
// customAttributes["country"] = "ES"
// customAttributes["deliveryLocationLatitude"] = "41.4035798"
// customAttributes["appVersion"] = "5.201.0"
// customAttributes["city"] = "BCN"
// customAttributes["deviceId"] = "1104442582"
// customAttributes["platform"] = "android"
// customAttributes["isAuthorized"] = "true"
// customAttributes["productSelectionOrigin"] = "Catalogue"
// customAttributes["currentLocationLatitude"] = "41.4035798"
// customAttributes["collectionId"] = "1180889389"
// customAttributes["multiplatformVersion"] = "1.0.288"
// customAttributes["deliveryLocationTimestamp"] = "1675344636685"
// customAttributes["productId"] = "13061043670"
// customAttributes["storeAddressId"] = "300482"
// customAttributes["currentLocationAccuracy"] = "19.278"
// customAttributes["productAddedOrigin"] = "Item Detail Add to Order"
// customAttributes["deliveryLocationLongitude"] = "2.1811267"
// customAttributes["currentLocationTimestamp"] = "1675344636685"
// customAttributes["dynamicSessionId"] = "67f8fb8d-8d14-4f0e-bf1a-73fb8e6eed95"
// customAttributes["deliveryLocationAccuracy"] = "19.278"
// customAttributes["categoryId"] = "1"
// customAttributes["isSponsored"] = "false"
//
// val commerceEvent: CommerceEvent = CommerceEvent.Builder(Product.ADD_TO_CART, product)
// .currency("EUR")
// .customAttributes(customAttributes)
// .transactionAttributes(txAttributes)
// .build()
// kit.logEvent(commerceEvent)
//
// val braze = Braze
// val events = braze.events
// Assert.assertEquals(1, events.size.toLong())
// val event = events.values.iterator().next()
// Assert.assertNotNull(event.properties)
// val properties = event.properties
//
// Assert.assertEquals(properties.remove("Name"), "La Enchilada")
// Assert.assertEquals(properties.remove("Total Product Amount"), "12.5")
// Assert.assertEquals(properties.remove("Id"), "13061043670")
// for (item in customAttributes) {
// Assert.assertTrue(properties.containsKey(item.key))
// Assert.assertTrue(properties.containsValue(item.value))
// }
// }

// @Test
// fun testEventStringTypeNotEnabled() {
// val kit = MockAppboyKit()
Expand Down

0 comments on commit 037a0f1

Please sign in to comment.