Skip to content

Commit 3ada269

Browse files
feat: Add all identities from mp user to rokt attributes
2 parents cc0144c + 37aa371 commit 3ada269

File tree

2 files changed

+143
-150
lines changed

2 files changed

+143
-150
lines changed

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.content.pm.PackageManager
77
import android.graphics.Typeface
88
import android.os.Build
99
import com.mparticle.MParticle
10+
import com.mparticle.MParticle.IdentityType
1011
import com.mparticle.commerce.CommerceEvent
1112
import com.mparticle.identity.MParticleUser
1213
import com.mparticle.internal.Logger
@@ -139,14 +140,13 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
139140
finalAttributes[key] = value.toString()
140141
}
141142
}
142-
filterAttributes(finalAttributes, configuration).let {
143-
finalAttributes.putAll(it)
144-
}
143+
145144
filterUser?.id?.let { mpid ->
146145
finalAttributes.put(MPID, mpid.toString())
147146
} ?: run {
148147
Logger.warning("RoktKit: No user ID available for placement")
149148
}
149+
addIdentityAttributes(finalAttributes, filterUser)
150150

151151
Rokt.execute(
152152
viewName,
@@ -158,15 +158,49 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
158158
)
159159
}
160160

161-
private fun filterAttributes(attributes: Map<String, String>, kitConfiguration: KitConfiguration): Map<String, String> {
162-
val userAttributes: MutableMap<String, String> = HashMap<String, String>()
163-
for ((key, value) in attributes) {
164-
val hashKey = KitUtils.hashForFiltering(key)
165-
if (!kitConfiguration.mAttributeFilters.get(hashKey)) {
166-
userAttributes[key] = value
161+
162+
private fun addIdentityAttributes(attributes: MutableMap<String, String>?, filterUser: FilteredMParticleUser?): MutableMap<String, String> {
163+
val identityAttributes = mutableMapOf<String, String>()
164+
if (filterUser != null) {
165+
for ((identityNumberKey, identityValue) in filterUser.userIdentities) {
166+
val identityType = getStringForIdentity(identityNumberKey)
167+
identityAttributes[identityType] = identityValue
167168
}
168169
}
169-
return userAttributes
170+
if (attributes != null) {
171+
attributes.putAll(identityAttributes)
172+
return attributes
173+
} else {
174+
return identityAttributes
175+
}
176+
}
177+
178+
private fun getStringForIdentity(identityType: IdentityType): String {
179+
return when (identityType) {
180+
IdentityType.Other -> "other"
181+
IdentityType.CustomerId -> "customerid"
182+
IdentityType.Facebook -> "facebook"
183+
IdentityType.Twitter -> "twitter"
184+
IdentityType.Google -> "google"
185+
IdentityType.Microsoft -> "microsoft"
186+
IdentityType.Yahoo -> "yahoo"
187+
IdentityType.Email -> "email"
188+
IdentityType.Alias -> "alias"
189+
IdentityType.FacebookCustomAudienceId -> "facebookcustomaudienceid"
190+
IdentityType.Other2 -> "other2"
191+
IdentityType.Other3 -> "other3"
192+
IdentityType.Other4 -> "other4"
193+
IdentityType.Other5 -> "other5"
194+
IdentityType.Other6 -> "other6"
195+
IdentityType.Other7 -> "other7"
196+
IdentityType.Other8 -> "other8"
197+
IdentityType.Other9 -> "other9"
198+
IdentityType.Other10 -> "other10"
199+
IdentityType.MobileNumber -> "mobilenumber"
200+
IdentityType.PhoneNumber2 -> "phonenumber2"
201+
IdentityType.PhoneNumber3 -> "phonenumber3"
202+
else -> ""
203+
}
170204
}
171205

172206
companion object {

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

Lines changed: 99 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.os.Build.VERSION
77
import com.mparticle.AttributionError
88
import com.mparticle.AttributionResult
99
import com.mparticle.MParticle
10+
import com.mparticle.MParticle.IdentityType
1011
import com.mparticle.MParticleOptions
1112
import com.mparticle.MParticleOptions.DataplanOptions
1213
import com.mparticle.identity.IdentityApi
@@ -98,186 +99,144 @@ class RoktKitTests {
9899
}
99100

100101
@Test
101-
fun testFilterAttributes() {
102-
103-
// Create test attributes
102+
fun test_addIdentityAttributes_When_userIdentities_IS_Null(){
103+
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
104+
val userIdentities = HashMap<IdentityType, String>()
105+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
104106
val attributes: Map<String, String> = mapOf(
105-
"ShouldFilter" to "shoudl_filter_value",
106-
"ShouldFilter_key_2" to "ShouldFilter_value",
107-
"allowed_key" to "allowed_value"
107+
"key1" to "value1",
108+
"key2" to "value2",
109+
"key3" to "value3"
108110
)
109-
110-
// Get the private filterAttributes method using reflection
111111
val method: Method = RoktKit::class.java.getDeclaredMethod(
112-
"filterAttributes",
112+
"addIdentityAttributes",
113113
Map::class.java,
114-
KitConfiguration::class.java
114+
FilteredMParticleUser::class.java
115115
)
116116
method.isAccessible = true
117+
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
118+
assertEquals(3, result.size)
117119

118-
// Set up the configuration with our test filters
119-
val jsonObject = JSONObject()
120-
try {
121-
val filteredKey:String =KitUtils.hashForFiltering("ShouldFilter").toString()
122-
val allowedKey:String = KitUtils.hashForFiltering("ShouldFilter_key_2").toString()
123-
jsonObject.put(filteredKey, 0)
124-
jsonObject.put(allowedKey, 1)
125-
} catch (e: Exception) {
126-
println("Exception occurred: ${e.message}")
127-
}
128-
129-
val json = JSONObject()
130-
json.put("ea", jsonObject)
131-
132-
133-
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs",json))
134-
135-
// Invoke the method and get the result
136-
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>
137-
138-
// Verify the results
139-
assertEquals(1, result.size)
120+
assertTrue(result.containsKey("key1"))
121+
assertTrue(result.containsKey("key2"))
122+
assertTrue(result.containsKey("key3"))
140123

141-
assertFalse(result.containsKey("ShouldFilter"))
142-
assertFalse(result.containsKey("ShouldFilter_key_2"))
143-
assertTrue(result.containsKey("allowed_key"))
144-
assertEquals("allowed_value", result["allowed_key"])
145124
}
146125

147126
@Test
148-
fun testFilterAttributes_When_kitConfig_Attributes_IS_NULL() {
149-
150-
// Create test attributes
127+
fun test_addIdentityAttributes_When_userIdentities_Contain_value(){
128+
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
129+
val userIdentities = HashMap<IdentityType, String>()
130+
userIdentities.put(IdentityType.Email,"[email protected]")
131+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
151132
val attributes: Map<String, String> = mapOf(
152-
"filtered_key" to "filtered_value",
153-
"allowed_key" to "allowed_value",
154-
"another_allowed_key" to "another_allowed_value"
133+
"key1" to "value1",
134+
"key2" to "value2",
135+
"key3" to "value3"
155136
)
156-
157-
// Get the private filterAttributes method using reflection
158137
val method: Method = RoktKit::class.java.getDeclaredMethod(
159-
"filterAttributes",
138+
"addIdentityAttributes",
160139
Map::class.java,
161-
KitConfiguration::class.java
140+
FilteredMParticleUser::class.java
162141
)
163142
method.isAccessible = true
143+
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
144+
assertEquals(4, result.size)
164145

165-
// Set up the configuration with our test filters
166-
val jsonObject = JSONObject()
167-
try {
168-
val filteredKey:String =KitUtils.hashForFiltering("filtered_key").toString()
169-
val allowedKey:String = KitUtils.hashForFiltering("allowed_key").toString()
170-
jsonObject.put(filteredKey, 0)
171-
jsonObject.put(allowedKey, 1)
172-
} catch (e: Exception) {
173-
println("Exception occurred: ${e.message}")
174-
}
175-
176-
val json = JSONObject()
177-
//here is invalid json key for filtering
178-
json.put("aaa", jsonObject)
146+
assertTrue(result.containsKey("key1"))
147+
assertTrue(result.containsKey("key2"))
148+
assertTrue(result.containsKey("key3"))
149+
assertTrue(result.containsKey("email"))
179150

180-
181-
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs",json))
182-
183-
// Invoke the method and get the result
184-
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>
185-
186-
assertEquals(3, result.size)
187-
188-
assertTrue(result.containsKey("allowed_key"))
189-
assertTrue(result.containsKey("filtered_key"))
190-
assertTrue(result.containsKey("another_allowed_key"))
191-
assertEquals("another_allowed_value", result["another_allowed_key"])
192151
}
193152

194153
@Test
195-
fun testFilterAttributes_When_Attributes_IS_Empty() {
196-
197-
// Create test attributes
198-
val emptyAttributes: Map<String, String> = emptyMap()
199-
200-
201-
// Get the private filterAttributes method using reflection
154+
fun test_addIdentityAttributes_When_userIdentities_And_attributes_contains_same_key(){
155+
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
156+
val userIdentities = HashMap<IdentityType, String>()
157+
userIdentities.put(IdentityType.Email,"[email protected]")
158+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
159+
val attributes: Map<String, String> = mapOf(
160+
"key1" to "value1",
161+
"key2" to "value2",
162+
"key3" to "value3",
163+
"email" to "[email protected]"
164+
)
202165
val method: Method = RoktKit::class.java.getDeclaredMethod(
203-
"filterAttributes",
166+
"addIdentityAttributes",
204167
Map::class.java,
205-
KitConfiguration::class.java
168+
FilteredMParticleUser::class.java
206169
)
207170
method.isAccessible = true
171+
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
172+
assertEquals(4, result.size)
173+
174+
assertTrue(result.containsKey("key1"))
175+
assertTrue(result.containsKey("key2"))
176+
assertTrue(result.containsKey("key3"))
177+
assertTrue(result.containsKey("email"))
178+
assertEquals(
179+
mapOf(
180+
"key1" to "value1",
181+
"key2" to "value2",
182+
"key3" to "value3",
183+
"email" to "[email protected]"
184+
),
185+
result
186+
)
187+
}
208188

209-
// Set up the configuration with our test filters
210-
val jsonObject = JSONObject()
211-
try {
212-
val filteredKey:String =KitUtils.hashForFiltering("filtered_key").toString()
213-
val allowedKey:String = KitUtils.hashForFiltering("allowed_key").toString()
214-
jsonObject.put(filteredKey, 0)
215-
jsonObject.put(allowedKey, 1)
216-
} catch (e: Exception) {
217-
println("Exception occurred: ${e.message}")
218-
}
219-
220-
val json = JSONObject()
221-
json.put("aaa", jsonObject)
222-
223-
224-
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs",json))
225-
226-
// Invoke the method and get the result
227-
val result = method.invoke(roktKit, emptyAttributes, roktKit.configuration) as Map<String, String>
228-
229-
assertEquals(0, result.size)
189+
@Test
190+
fun testAddIdentityAttributes_bothNull() {
191+
val method: Method = RoktKit::class.java.getDeclaredMethod(
192+
"addIdentityAttributes",
193+
Map::class.java,
194+
FilteredMParticleUser::class.java
195+
)
196+
method.isAccessible = true
197+
val result = method.invoke(roktKit, null, null) as Map<String, String>
198+
assertTrue(result.isEmpty())
230199
}
231200

232201
@Test
233-
fun testFilterAttributes_When_attribute_different_value() {
202+
fun testAddIdentityAttributes_nullAttributes_validUser() {
203+
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
204+
val userIdentities = HashMap<IdentityType, String>()
205+
userIdentities.put(IdentityType.Email,"[email protected]")
206+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
207+
val method: Method = RoktKit::class.java.getDeclaredMethod(
208+
"addIdentityAttributes",
209+
Map::class.java,
210+
FilteredMParticleUser::class.java
211+
)
212+
method.isAccessible = true
213+
val result = method.invoke(roktKit, null, mockFilterUser) as Map<String, String>
214+
assertEquals(1, result.size)
215+
assertEquals(mapOf("email" to "[email protected]"), result)
216+
}
234217

235-
// Create test attributes
218+
@Test
219+
fun testAddIdentityAttributes_nullUser_returnsSameAttributes() {
236220
val attributes: Map<String, String> = mapOf(
237-
"filtered_key" to "filtered_value",
238-
"allowed_key" to "allowed_value",
239-
"another_allowed_key" to "another_allowed_value"
221+
"key1" to "value1",
222+
"key2" to "value2",
223+
"key3" to "value3"
240224
)
241-
242-
// Get the private filterAttributes method using reflection
243225
val method: Method = RoktKit::class.java.getDeclaredMethod(
244-
"filterAttributes",
226+
"addIdentityAttributes",
245227
Map::class.java,
246-
KitConfiguration::class.java
228+
FilteredMParticleUser::class.java
247229
)
248230
method.isAccessible = true
249-
250-
// Set up the configuration with our test filters
251-
val jsonObject = JSONObject()
252-
try {
253-
val filteredKey:String =KitUtils.hashForFiltering("Test1").toString()
254-
val allowedKey:String = KitUtils.hashForFiltering("Test2").toString()
255-
jsonObject.put(filteredKey, 0)
256-
jsonObject.put(allowedKey, 1)
257-
} catch (e: Exception) {
258-
println("Exception occurred: ${e.message}")
259-
}
260-
261-
val json = JSONObject()
262-
json.put("ea", jsonObject)
263-
264-
265-
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs",json))
266-
267-
// Invoke the method and get the result
268-
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>
269-
270-
// Verify the results
231+
val result = method.invoke(roktKit, attributes, null) as Map<String, String>
271232
assertEquals(3, result.size)
272-
273-
assertTrue(result.containsKey("filtered_key"))
274-
assertTrue(result.containsKey("allowed_key"))
275-
assertTrue(result.containsKey("another_allowed_key"))
276-
assertEquals("another_allowed_value", result["another_allowed_key"])
277-
assertEquals("filtered_value", result["filtered_key"])
278-
assertEquals("allowed_value", result["allowed_key"])
233+
assertTrue(result.containsKey("key1"))
234+
assertTrue(result.containsKey("key2"))
235+
assertTrue(result.containsKey("key3"))
279236
}
280237

238+
239+
281240
internal inner class TestCoreCallbacks : CoreCallbacks {
282241
override fun isBackgrounded(): Boolean = false
283242
override fun getUserBucket(): Int = 0

0 commit comments

Comments
 (0)