Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
84 changes: 58 additions & 26 deletions src/main/kotlin/com/mparticle/kits/AppboyKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
): List<ReportingMessage> = emptyList()

override fun logEvent(event: MPEvent): List<ReportingMessage> {
if (event.customAttributes != null) {
event.customAttributeStrings?.let {
changeUserArray(
it,
event.eventType.value,
event.eventName, false
)
}
}
return logBrazeEvent(event)
}

private fun logBrazeEvent(event: MPEvent): List<ReportingMessage> {
val newAttributes: MutableMap<String, Any?> = HashMap()
if (event.customAttributes == null) {
Braze.getInstance(context).logCustomEvent(event.eventName)
Expand All @@ -142,40 +155,55 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
val brazePropertiesSetter = BrazePropertiesSetter(properties, enableTypeDetection)
event.customAttributeStrings?.let { it ->
for ((key, value) in it) {
newAttributes[key] = brazePropertiesSetter.parseValue(key, value)
try {
newAttributes[key] = brazePropertiesSetter.parseValue(key, value)
} catch (e: Exception) {
Logger.warning("Exception while parsing custom attributes $e")
}
}
}
Braze.getInstance(context).logCustomEvent(event.eventName, properties)
Braze.getInstance(context).getCurrentUser(object : IValueCallback<BrazeUser> {
override fun onSuccess(value: BrazeUser) {
val userAttributeSetter = UserAttributeSetter(value, enableTypeDetection)
event.customAttributeStrings?.let { it ->
for ((key, attributeValue) in it) {
val hashedKey =
KitUtils.hashForFiltering(event.eventType.value.toString() + event.eventName + key)

configuration.eventAttributesAddToUser?.get(hashedKey)?.let {
value.addToCustomAttributeArray(it, attributeValue)
}
configuration.eventAttributesRemoveFromUser?.get(hashedKey)?.let {
value.removeFromCustomAttributeArray(it, attributeValue)
}
configuration.eventAttributesSingleItemUser?.get(hashedKey)?.let {
userAttributeSetter.parseValue(it, attributeValue)
}
queueDataFlush()
return listOf(ReportingMessage.fromEvent(this, event).setAttributes(newAttributes))
}

private fun changeUserArray(
customAttributes: Map<String, String>,
eventType: Int,
eventName: String?,
isCommerceEvent: Boolean
) {
Braze.getInstance(context).getCurrentUser(object : IValueCallback<BrazeUser> {
override fun onSuccess(value: BrazeUser) {
val userAttributeSetter = UserAttributeSetter(value, enableTypeDetection)
customAttributes?.let { it ->
for ((key, attributeValue) in it) {
//for commerce event, event name is not required for generate hash
val hashedKey =
if (isCommerceEvent) {
KitUtils.hashForFiltering(eventType.toString() + key)
} else {
KitUtils.hashForFiltering(eventType.toString() + eventName + key)
}
configuration.eventAttributesAddToUser?.get(hashedKey)?.let {
value.addToCustomAttributeArray(it, attributeValue)
}
configuration.eventAttributesRemoveFromUser?.get(hashedKey)?.let {
value.removeFromCustomAttributeArray(it, attributeValue)
}
configuration.eventAttributesSingleItemUser?.get(hashedKey)?.let {
userAttributeSetter.parseValue(it, attributeValue)
}
}
}
}

override fun onError() {
Logger.warning("unable to acquire user to add or remove custom user attributes from events")
}
})
}
queueDataFlush()
return listOf(ReportingMessage.fromEvent(this, event).setAttributes(newAttributes))
override fun onError() {
Logger.warning("unable to acquire user to add or remove custom user attributes from events")
}
})
}

override fun logScreen(
screenName: String,
screenAttributes: Map<String, String>?
Expand Down Expand Up @@ -216,6 +244,10 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,

override fun logEvent(event: CommerceEvent): List<ReportingMessage> {
val messages: MutableList<ReportingMessage> = LinkedList()
//For CommerceEvent, Event Name is not required to generate hash. So, it will be always null.
event.products?.get(0)?.customAttributes?.let {

Choose a reason for hiding this comment

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

you're missing most of what is mappable for a commerce event:

  • event.getTransactionAttributes()
  • event.customAttributes
  • properties of every product object (eg price, quantity) - you're just doing customAttributes of the 1st product here?

and - does ios or web support mapping properties of impressions and promotions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • event.getTransactionAttributes() (adding this changes)
  • event.customAttributes(adding this changes)
  • properties of every product object (eg price, quantity) - you're just doing customAttributes of the 1st product here? missed this by mistake, will move to loop for all the products.

Web doesn't support mapping properties of impressions and promotions; and for IOS I think currently it only supports custom events not commerce events.

https://github.com/mparticle-integrations/mparticle-apple-integration-appboy/blob/[…]cd60edc2751c10129f168d8a/Sources/mParticle-Appboy/MPKitAppboy.m

changeUserArray(it, CommerceEventUtils.getEventType(event), null, true)
}
if (!KitUtils.isEmpty(event.productAction) &&
event.productAction.equals(
Product.PURCHASE,
Expand Down Expand Up @@ -249,7 +281,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
for (pair in map) {
e.customAttributes?.put(pair.key, pair.value)
}
logEvent(e)
logBrazeEvent(e)
messages.add(ReportingMessage.fromEvent(this, event))
} catch (e: Exception) {
Logger.warning("Failed to call logCustomEvent to Appboy kit: $e")
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/com/braze/Braze.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Braze {
events.clear()
}

fun clearBrazeUser(){
currentUser.customUserAttributes.clear()
currentUser.customAttributeArray.clear()
}
val currentUser = BrazeUser()

@JvmStatic
Expand Down
Loading