Skip to content

Commit b8cebde

Browse files
author
markvdouw
authored
fix: Manually adding custom attributes for NON PURCHASE EVENTS (#147)
* Manually adding custom attributes for NON PURCHASE EVENTS * Adding commented test
1 parent 274143a commit b8cebde

File tree

2 files changed

+135
-22
lines changed

2 files changed

+135
-22
lines changed

src/main/kotlin/com/mparticle/kits/AppboyKit.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
236236
if (eventList != null) {
237237
for (i in eventList.indices) {
238238
try {
239-
logEvent(eventList[i])
239+
val e = eventList[i]
240+
val map = mutableMapOf<String, String>()
241+
event.customAttributeStrings?.let { map.putAll(it) }
242+
for (pair in map) {
243+
e.customAttributes?.put(pair.key, pair.value)
244+
}
245+
logEvent(e)
240246
messages.add(ReportingMessage.fromEvent(this, event))
241247
} catch (e: Exception) {
242248
Logger.warning("Failed to call logCustomEvent to Appboy kit: $e")
@@ -523,7 +529,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
523529

524530
val eventName = "eCommerce - %s"
525531
if (!KitUtils.isEmpty(event?.productAction) &&
526-
event?.productAction.equals(Product.PURCHASE,true)
532+
event?.productAction.equals(Product.PURCHASE, true)
527533
) {
528534
Braze.Companion.getInstance(context).logPurchase(
529535
String.format(eventName, event?.productAction),
@@ -534,11 +540,14 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
534540
)
535541
} else {
536542
if (!KitUtils.isEmpty(event?.productAction)) {
537-
Braze.getInstance(context).logCustomEvent(String.format(eventName, event?.productAction), properties)
543+
Braze.getInstance(context)
544+
.logCustomEvent(String.format(eventName, event?.productAction), properties)
538545
} else if (!KitUtils.isEmpty(event?.promotionAction)) {
539-
Braze.getInstance(context).logCustomEvent(String.format(eventName, event?.promotionAction), properties)
546+
Braze.getInstance(context)
547+
.logCustomEvent(String.format(eventName, event?.promotionAction), properties)
540548
} else {
541-
Braze.getInstance(context).logCustomEvent(String.format(eventName, "Impression"), properties)
549+
Braze.getInstance(context)
550+
.logCustomEvent(String.format(eventName, "Impression"), properties)
542551
}
543552
}
544553
}
@@ -763,7 +772,10 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
763772
for ((i, promotion) in promotionList.withIndex()) {
764773
val promotionProperties = BrazeProperties()
765774
promotion.creative?.let {
766-
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE, it)
775+
promotionProperties.addProperty(
776+
CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE,
777+
it
778+
)
767779
}
768780
promotion.id?.let {
769781
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_ID, it)
@@ -772,7 +784,10 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
772784
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_NAME, it)
773785
}
774786
promotion.position?.let {
775-
promotionProperties.addProperty(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION, it)
787+
promotionProperties.addProperty(
788+
CommerceEventUtils.Constants.ATT_PROMOTION_POSITION,
789+
it
790+
)
776791
}
777792
promotionArray[i] = promotionProperties
778793
}

src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,26 @@ class AppboyKitTests {
494494
val productBrazeProperties = productArray[0]
495495
if (productBrazeProperties is BrazeProperties) {
496496
val productProperties = productBrazeProperties.properties
497-
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_TOTAL_AMOUNT), 22.5)
498-
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE), 4.5)
499-
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_QUANTITY), 5.0)
500-
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_ID), "sku1")
501-
Assert.assertEquals(productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_NAME), "product name")
497+
Assert.assertEquals(
498+
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_TOTAL_AMOUNT),
499+
22.5
500+
)
501+
Assert.assertEquals(
502+
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE),
503+
4.5
504+
)
505+
Assert.assertEquals(
506+
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_QUANTITY),
507+
5.0
508+
)
509+
Assert.assertEquals(
510+
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_ID),
511+
"sku1"
512+
)
513+
Assert.assertEquals(
514+
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_NAME),
515+
"product name"
516+
)
502517
Assert.assertEquals(emptyAttributes, productProperties)
503518
}
504519
}
@@ -584,7 +599,7 @@ class AppboyKitTests {
584599
id = "my_promo_1"
585600
creative = "sale_banner_1"
586601
name = "App-wide 50% off sale"
587-
position ="dashboard_bottom"
602+
position = "dashboard_bottom"
588603
}
589604
val commerceEvent = CommerceEvent.Builder(Promotion.VIEW, promotion)
590605
.customAttributes(customAttributes)
@@ -604,10 +619,22 @@ class AppboyKitTests {
604619
val promotionBrazeProperties = promotionArray[0]
605620
if (promotionBrazeProperties is BrazeProperties) {
606621
val promotionProperties = promotionBrazeProperties.properties
607-
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_ID), "my_promo_1")
608-
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_NAME), "App-wide 50% off sale")
609-
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION), "dashboard_bottom")
610-
Assert.assertEquals(promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE), "sale_banner_1")
622+
Assert.assertEquals(
623+
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_ID),
624+
"my_promo_1"
625+
)
626+
Assert.assertEquals(
627+
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_NAME),
628+
"App-wide 50% off sale"
629+
)
630+
Assert.assertEquals(
631+
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_POSITION),
632+
"dashboard_bottom"
633+
)
634+
Assert.assertEquals(
635+
promotionProperties.remove(CommerceEventUtils.Constants.ATT_PROMOTION_CREATIVE),
636+
"sale_banner_1"
637+
)
611638
Assert.assertEquals(emptyAttributes, promotionProperties)
612639
}
613640
}
@@ -697,7 +724,10 @@ class AppboyKitTests {
697724
val impressionBrazeProperties = impressionArray[0]
698725
if (impressionBrazeProperties is BrazeProperties) {
699726
val impressionProperties = impressionBrazeProperties.properties
700-
Assert.assertEquals(impressionProperties.remove("Product Impression List"), "Suggested Products List")
727+
Assert.assertEquals(
728+
impressionProperties.remove("Product Impression List"),
729+
"Suggested Products List"
730+
)
701731
val productArray = impressionProperties.remove(AppboyKit.PRODUCT_KEY)
702732
Assert.assertTrue(productArray is Array<*>)
703733
if (productArray is Array<*>) {
@@ -725,11 +755,19 @@ class AppboyKitTests {
725755
productProperties.remove(CommerceEventUtils.Constants.ATT_PRODUCT_PRICE),
726756
4.5
727757
)
728-
val brazeProductCustomAttributesDictionary = productProperties.remove(AppboyKit.CUSTOM_ATTRIBUTES_KEY)
758+
val brazeProductCustomAttributesDictionary =
759+
productProperties.remove(AppboyKit.CUSTOM_ATTRIBUTES_KEY)
729760
if (brazeProductCustomAttributesDictionary is BrazeProperties) {
730-
val customProductAttributesDictionary = brazeProductCustomAttributesDictionary.properties
731-
Assert.assertEquals(customProductAttributesDictionary.remove("key1"), "value1")
732-
Assert.assertEquals(customProductAttributesDictionary.remove("key #2"), "value #3")
761+
val customProductAttributesDictionary =
762+
brazeProductCustomAttributesDictionary.properties
763+
Assert.assertEquals(
764+
customProductAttributesDictionary.remove("key1"),
765+
"value1"
766+
)
767+
Assert.assertEquals(
768+
customProductAttributesDictionary.remove("key #2"),
769+
"value #3"
770+
)
733771
Assert.assertEquals(emptyAttributes, customProductAttributesDictionary)
734772
}
735773
Assert.assertEquals(emptyAttributes, productProperties)
@@ -797,6 +835,66 @@ class AppboyKitTests {
797835
// Assert.assertEquals(0, properties.size.toLong())
798836
// }
799837

838+
// @Test
839+
// fun testLogCommerceEvent() {
840+
// val kit = MockAppboyKit()
841+
//
842+
// val product: Product = Product.Builder("La Enchilada", "13061043670", 12.5)
843+
// .quantity(1.0)
844+
// .build()
845+
//
846+
// val txAttributes = TransactionAttributes()
847+
// .setRevenue(product.getTotalAmount())
848+
//
849+
// kit.configuration = MockKitConfiguration()
850+
// val customAttributes: MutableMap<String, String> = HashMap()
851+
// customAttributes["currentLocationLongitude"] = "2.1811267"
852+
// customAttributes["country"] = "ES"
853+
// customAttributes["deliveryLocationLatitude"] = "41.4035798"
854+
// customAttributes["appVersion"] = "5.201.0"
855+
// customAttributes["city"] = "BCN"
856+
// customAttributes["deviceId"] = "1104442582"
857+
// customAttributes["platform"] = "android"
858+
// customAttributes["isAuthorized"] = "true"
859+
// customAttributes["productSelectionOrigin"] = "Catalogue"
860+
// customAttributes["currentLocationLatitude"] = "41.4035798"
861+
// customAttributes["collectionId"] = "1180889389"
862+
// customAttributes["multiplatformVersion"] = "1.0.288"
863+
// customAttributes["deliveryLocationTimestamp"] = "1675344636685"
864+
// customAttributes["productId"] = "13061043670"
865+
// customAttributes["storeAddressId"] = "300482"
866+
// customAttributes["currentLocationAccuracy"] = "19.278"
867+
// customAttributes["productAddedOrigin"] = "Item Detail Add to Order"
868+
// customAttributes["deliveryLocationLongitude"] = "2.1811267"
869+
// customAttributes["currentLocationTimestamp"] = "1675344636685"
870+
// customAttributes["dynamicSessionId"] = "67f8fb8d-8d14-4f0e-bf1a-73fb8e6eed95"
871+
// customAttributes["deliveryLocationAccuracy"] = "19.278"
872+
// customAttributes["categoryId"] = "1"
873+
// customAttributes["isSponsored"] = "false"
874+
//
875+
// val commerceEvent: CommerceEvent = CommerceEvent.Builder(Product.ADD_TO_CART, product)
876+
// .currency("EUR")
877+
// .customAttributes(customAttributes)
878+
// .transactionAttributes(txAttributes)
879+
// .build()
880+
// kit.logEvent(commerceEvent)
881+
//
882+
// val braze = Braze
883+
// val events = braze.events
884+
// Assert.assertEquals(1, events.size.toLong())
885+
// val event = events.values.iterator().next()
886+
// Assert.assertNotNull(event.properties)
887+
// val properties = event.properties
888+
//
889+
// Assert.assertEquals(properties.remove("Name"), "La Enchilada")
890+
// Assert.assertEquals(properties.remove("Total Product Amount"), "12.5")
891+
// Assert.assertEquals(properties.remove("Id"), "13061043670")
892+
// for (item in customAttributes) {
893+
// Assert.assertTrue(properties.containsKey(item.key))
894+
// Assert.assertTrue(properties.containsValue(item.value))
895+
// }
896+
// }
897+
800898
// @Test
801899
// fun testEventStringTypeNotEnabled() {
802900
// val kit = MockAppboyKit()

0 commit comments

Comments
 (0)