Skip to content

Commit 22a9786

Browse files
authored
feat: Add support to forward SubscriptionGroupIds (#181)
1 parent b3bf5d8 commit 22a9786

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
4747
var bundleCommerceEvents = false
4848
var isMpidIdentityType = false
4949
var identityType: IdentityType? = null
50+
var subscriptionGroupIds: MutableMap<String, String>? = mutableMapOf()
5051
private val dataFlushHandler = Handler()
5152
private var dataFlushRunnable: Runnable? = null
5253
private var forwardScreenViews = false
@@ -85,6 +86,9 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
8586
}
8687
}
8788
forwardScreenViews = settings[FORWARD_SCREEN_VIEWS].toBoolean()
89+
subscriptionGroupIds = settings[SUBSCRIPTION_GROUP_MAPPING]?.let {
90+
getSubscriptionGroupIds(it)
91+
}
8892
if (key != null) {
8993
val config = BrazeConfig.Builder().setApiKey(key)
9094
.setSdkFlavor(SdkFlavor.MPARTICLE)
@@ -318,10 +322,29 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
318322
else value.setGender(Gender.MALE)
319323
}
320324
else -> {
321-
if (key.startsWith("$")) {
322-
key = key.substring(1)
325+
if (subscriptionGroupIds?.containsKey(key) == true) {
326+
val groupId = subscriptionGroupIds?.get(key)
327+
when (attributeValue.lowercase()) {
328+
"true" -> {
329+
groupId?.let { value.addToSubscriptionGroup(it) }
330+
}
331+
332+
"false" -> {
333+
groupId?.let { value.removeFromSubscriptionGroup(it) }
334+
}
335+
336+
else -> {
337+
Logger.warning(
338+
"Unable to set Subscription Group ID for user attribute: $key due to invalid value data type. Expected Boolean."
339+
)
340+
}
341+
}
342+
} else {
343+
if (key.startsWith("$")) {
344+
key = key.substring(1)
345+
}
346+
userAttributeSetter?.parseValue(key, attributeValue)
323347
}
324-
userAttributeSetter?.parseValue(key, attributeValue)
325348
}
326349
}
327350
queueDataFlush()
@@ -966,6 +989,29 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
966989
return promotionArray
967990
}
968991

992+
private fun getSubscriptionGroupIds(subscriptionGroupMap: String): MutableMap<String, String> {
993+
val subscriptionGroupIds = mutableMapOf<String, String>()
994+
995+
if (subscriptionGroupMap.isEmpty()) {
996+
return subscriptionGroupIds
997+
}
998+
999+
val subscriptionGroupsArray = JSONArray(subscriptionGroupMap)
1000+
1001+
return try {
1002+
for (i in 0 until subscriptionGroupsArray.length()) {
1003+
val subscriptionGroup = subscriptionGroupsArray.getJSONObject(i)
1004+
val key = subscriptionGroup.getString("map")
1005+
val value = subscriptionGroup.getString("value")
1006+
subscriptionGroupIds[key] = value
1007+
}
1008+
subscriptionGroupIds
1009+
} catch (e: JSONException) {
1010+
Logger.warning("Braze, unable to parse \"subscriptionGroup\"")
1011+
mutableMapOf()
1012+
}
1013+
}
1014+
9691015
fun getImpressionListParameters(impressionList: List<Impression>): JSONArray {
9701016
val impressionArray = JSONArray()
9711017
for ((i, impression) in impressionList.withIndex()) {
@@ -1083,6 +1129,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
10831129
const val USER_IDENTIFICATION_TYPE = "userIdentificationType"
10841130
const val ENABLE_TYPE_DETECTION = "enableTypeDetection"
10851131
const val BUNDLE_COMMERCE_EVENTS = "bundleCommerceEventData"
1132+
const val SUBSCRIPTION_GROUP_MAPPING = "subscriptionGroupMapping"
10861133
const val HOST = "host"
10871134
const val PUSH_ENABLED = "push_enabled"
10881135
const val NAME = "Appboy"

src/test/kotlin/com/braze/BrazeUser.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class BrazeUser {
5858
return true
5959
}
6060

61+
fun addToSubscriptionGroup(key: String): Boolean {
62+
customUserAttributes[key] = true
63+
return true
64+
}
65+
66+
fun removeFromSubscriptionGroup(key: String): Boolean {
67+
customUserAttributes[key] = false
68+
return true
69+
}
70+
6171
fun getCustomAttribute(): HashMap<String, MutableList<String>> {
6272
return customAttributeArray
6373
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ class AppboyKitTests {
238238
Assert.assertNull(kit.getCalendarMinusYears(-1))
239239
}
240240

241+
@Test
242+
fun testSetSubscriptionGroupIds() {
243+
val settings = HashMap<String, String>()
244+
settings[AppboyKit.APPBOY_KEY] = "key"
245+
settings[AppboyKit.HOST] = hostName
246+
settings["subscriptionGroupMapping"] = "" +
247+
"[{\"jsmap\":null,\"map\":\"test1\",\"maptype\":\"UserAttributeClass.Name\",\"value\":\"00000000-0000-0000-0000-000000000000\"}," +
248+
"{\"jsmap\":null,\"map\":\"test2\",\"maptype\":\"UserAttributeClass.Name\",\"value\":\"00000000-0000-0000-0000-000000000001\"}," +
249+
"{\"jsmap\":null,\"map\":\"test3\",\"maptype\":\"UserAttributeClass.Name\",\"value\":\"00000000-0000-0000-0000-000000000002\"}]"
250+
val kit = MockAppboyKit()
251+
val currentUser = braze.currentUser
252+
253+
kit.onKitCreate(settings, MockContextApplication())
254+
kit.setUserAttribute("test1", "true");
255+
kit.setUserAttribute("test2", "false");
256+
kit.setUserAttribute("test3", "notABoolean");
257+
Assert.assertEquals(2, currentUser.getCustomUserAttribute().size.toLong())
258+
}
259+
241260
// @Test
242261
// fun testSetUserAttributeAge() {
243262
// val currentYear = Calendar.getInstance()[Calendar.YEAR]

0 commit comments

Comments
 (0)