Skip to content

Commit

Permalink
feat: Send ProductName to Braze (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored and einsteinx2 committed Dec 12, 2024
1 parent 7812b65 commit 5d9ce41
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/main/kotlin/com/mparticle/kits/AppboyKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
77 changes: 74 additions & 3 deletions src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1290,4 +1290,75 @@ class AppboyKitTests {
TestCase.assertEquals(0, currentUser.getCustomUserAttribute().size)

}

@Test
fun testPurchase_Forward_product_name() {
var settings = HashMap<String, String?>()
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<String, String?>()
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<String, String?>()
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])
}
}

0 comments on commit 5d9ce41

Please sign in to comment.