From 5d9ce41e237ff19b16cf8334fd05592c81b03eda Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:53:54 -0500 Subject: [PATCH] feat: Send ProductName to Braze (#174) --- .../kotlin/com/mparticle/kits/AppboyKit.kt | 14 +++- .../com/mparticle/kits/AppboyKitTest.kt | 77 ++++++++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/mparticle/kits/AppboyKit.kt b/src/main/kotlin/com/mparticle/kits/AppboyKit.kt index 5816b7b..793c6f4 100644 --- a/src/main/kotlin/com/mparticle/kits/AppboyKit.kt +++ b/src/main/kotlin/com/mparticle/kits/AppboyKit.kt @@ -613,8 +613,18 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener, } } + var sanitizedProductName: String = product.sku + try { + if (settings[REPLACE_SKU_AS_PRODUCT_NAME] == "True") { + sanitizedProductName = product.name + } + } catch (e: Exception) { + Logger.error(e, "The Braze kit threw an exception while searching for forward sku as product name flag.") + + } + Braze.Companion.getInstance(context).logPurchase( - product.sku, + sanitizedProductName, currencyValue, BigDecimal(product.unitPrice), product.quantity.toInt(), @@ -1076,6 +1086,8 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener, const val HOST = "host" const val PUSH_ENABLED = "push_enabled" const val NAME = "Appboy" + // if this flag is true, kit will send Product name as sku + const val REPLACE_SKU_AS_PRODUCT_NAME = "forwardSkuAsProductName" private const val PREF_KEY_HAS_SYNCED_ATTRIBUTES = "appboy::has_synced_attributes" private const val PREF_KEY_CURRENT_EMAIL = "appboy::current_email" private const val FLUSH_DELAY = 5000 diff --git a/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt b/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt index 75a7cd9..e409aad 100644 --- a/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt +++ b/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt @@ -964,7 +964,7 @@ class AppboyKitTests { eaaObject.put("eaa", mapValue) jsonObject.put("hs", eaaObject) - Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) kit.configuration = kitConfiguration @@ -1002,7 +1002,7 @@ class AppboyKitTests { eaaObject.put("ear", mapValue) jsonObject.put("hs", eaaObject) - Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) kit.configuration = kitConfiguration @@ -1033,7 +1033,7 @@ class AppboyKitTests { eaaObject.put("eas", mapValue) jsonObject.put("hs", eaaObject) - Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) kit.configuration = kitConfiguration @@ -1290,4 +1290,75 @@ class AppboyKitTests { TestCase.assertEquals(0, currentUser.getCustomUserAttribute().size) } + + @Test + fun testPurchase_Forward_product_name() { + var settings = HashMap() + settings[AppboyKit.APPBOY_KEY] = "key" + settings[AppboyKit.REPLACE_SKU_AS_PRODUCT_NAME] = "True" + val kit = MockAppboyKit() + + kit.configuration = + KitConfiguration.createKitConfiguration(JSONObject().put("as", settings)) + kit.onKitCreate(settings, MockContextApplication()) + val product = Product.Builder("product name", "sku1", 4.5) + .build() + val commerceEvent = CommerceEvent.Builder(Product.CHECKOUT, product) + .currency("Moon Dollars") + .build() + kit.logTransaction(commerceEvent, product) + val braze = Braze + val purchases = braze.purchases + Assert.assertEquals(1, purchases.size.toLong()) + val purchase = purchases[0] + Assert.assertEquals("product name", purchase.sku) + Assert.assertNull(purchase.purchaseProperties.properties[CommerceEventUtils.Constants.ATT_ACTION_CURRENCY_CODE]) + } + + @Test + fun testPurchase_Forward_product_name_When_flag_IS_FALSE() { + var settings = HashMap() + settings[AppboyKit.APPBOY_KEY] = "key" + settings[AppboyKit.REPLACE_SKU_AS_PRODUCT_NAME] = "False" + val kit = MockAppboyKit() + + kit.configuration = + KitConfiguration.createKitConfiguration(JSONObject().put("as", settings)) + kit.onKitCreate(settings, MockContextApplication()) + val product = Product.Builder("product name", "sku1", 4.5) + .build() + val commerceEvent = CommerceEvent.Builder(Product.CHECKOUT, product) + .currency("Moon Dollars") + .build() + kit.logTransaction(commerceEvent, product) + val braze = Braze + val purchases = braze.purchases + Assert.assertEquals(1, purchases.size.toLong()) + val purchase = purchases[0] + Assert.assertEquals("sku1", purchase.sku) + Assert.assertNull(purchase.purchaseProperties.properties[CommerceEventUtils.Constants.ATT_ACTION_CURRENCY_CODE]) + } + + @Test + fun testPurchase_Forward_product_name_When_flag_IS_Null() { + var settings = HashMap() + settings[AppboyKit.APPBOY_KEY] = "key" + val kit = MockAppboyKit() + + kit.configuration = + KitConfiguration.createKitConfiguration(JSONObject().put("as", settings)) + kit.onKitCreate(settings, MockContextApplication()) + val product = Product.Builder("product name", "sku1", 4.5) + .build() + val commerceEvent = CommerceEvent.Builder(Product.CHECKOUT, product) + .currency("Moon Dollars") + .build() + kit.logTransaction(commerceEvent, product) + val braze = Braze + val purchases = braze.purchases + Assert.assertEquals(1, purchases.size.toLong()) + val purchase = purchases[0] + Assert.assertEquals("sku1", purchase.sku) + Assert.assertNull(purchase.purchaseProperties.properties[CommerceEventUtils.Constants.ATT_ACTION_CURRENCY_CODE]) + } }