Skip to content

Commit 475ec95

Browse files
feat: Add custom event logging for selectplacements (#113)
1 parent 5070710 commit 475ec95

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 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.BuildConfig
10+
import com.mparticle.MPEvent
1011
import com.mparticle.MParticle
1112
import com.mparticle.MParticle.IdentityType
1213
import com.mparticle.MpRoktEventCallback
@@ -231,6 +232,8 @@ class RoktKit :
231232
addIdentityAttributes(finalAttributes, filterUser)
232233

233234
verifyHashedEmail(finalAttributes)
235+
236+
logSelectPlacementEvent(finalAttributes)
234237
return finalAttributes
235238
}
236239

@@ -380,6 +383,13 @@ class RoktKit :
380383
}
381384
}
382385

386+
private fun logSelectPlacementEvent(attributes: Map<String, String>) {
387+
val event = MPEvent.Builder(EVENT_NAME_SELECT_PLACEMENTS, MParticle.EventType.Other)
388+
.customAttributes(attributes)
389+
.build()
390+
MParticle.getInstance()?.logEvent(event)
391+
}
392+
383393
private fun getStringForIdentity(identityType: IdentityType): String = when (identityType) {
384394
IdentityType.Other -> "other"
385395
IdentityType.CustomerId -> "customerid"
@@ -419,6 +429,7 @@ class RoktKit :
419429
const val ROKT_ACCOUNT_ID = "accountId"
420430
const val HASHED_EMAIL_USER_IDENTITY_TYPE = "hashedEmailUserIdentityType"
421431
const val MPID = "mpid"
432+
const val EVENT_NAME_SELECT_PLACEMENTS = "selectPlacements"
422433
const val NO_ROKT_ACCOUNT_ID = "No Rokt account ID provided, can't initialize kit."
423434
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
424435
}

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.net.Uri
66
import android.os.Build.VERSION
77
import com.mparticle.AttributionError
88
import com.mparticle.AttributionResult
9+
import com.mparticle.MPEvent
910
import com.mparticle.MParticle
1011
import com.mparticle.MParticle.IdentityType
1112
import com.mparticle.MParticleOptions
@@ -39,6 +40,7 @@ import org.junit.Assert.assertFalse
3940
import org.junit.Assert.assertTrue
4041
import org.junit.Before
4142
import org.junit.Test
43+
import org.mockito.ArgumentCaptor
4244
import org.mockito.Mockito
4345
import org.mockito.Mockito.mock
4446
import java.lang.ref.WeakReference
@@ -1282,6 +1284,94 @@ class RoktKitTests {
12821284
}
12831285
}
12841286

1287+
@Test
1288+
fun test_prepareFinalAttributes_logs_attributes_event() {
1289+
val mParticleMock = MParticle.getInstance()!!
1290+
val method: Method = RoktKit::class.java.getDeclaredMethod(
1291+
"prepareFinalAttributes",
1292+
FilteredMParticleUser::class.java,
1293+
Map::class.java,
1294+
)
1295+
method.isAccessible = true
1296+
1297+
val mockFilterUser = mock(FilteredMParticleUser::class.java)
1298+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(HashMap())
1299+
Mockito.`when`(mockFilterUser.id).thenReturn(9876L)
1300+
val userAttributes = hashMapOf<String, Any?>("user_key" to "user_val")
1301+
Mockito.`when`(mockFilterUser.userAttributes).thenReturn(userAttributes)
1302+
1303+
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", JSONObject()))
1304+
val attributes: Map<String, String> = mapOf("attr1" to "val1")
1305+
1306+
method.invoke(roktKit, mockFilterUser, attributes)
1307+
1308+
val eventCaptor = ArgumentCaptor.forClass(MPEvent::class.java)
1309+
Mockito.verify(mParticleMock).logEvent(eventCaptor.capture())
1310+
1311+
val loggedEvent = eventCaptor.value
1312+
assertEquals("selectPlacements", loggedEvent.eventName)
1313+
assertEquals(MParticle.EventType.Other, loggedEvent.eventType)
1314+
assertEquals(
1315+
mapOf(
1316+
"user_key" to "user_val",
1317+
"attr1" to "val1",
1318+
"mpid" to "9876",
1319+
),
1320+
loggedEvent.customAttributes,
1321+
)
1322+
}
1323+
1324+
@Test
1325+
fun test_execute_logsMPEventWhenMParticleInstanceIsNull() {
1326+
// Arrange
1327+
mockkObject(Rokt)
1328+
every {
1329+
Rokt.execute(
1330+
any<String>(),
1331+
any<Map<String, String>>(),
1332+
any<Rokt.RoktCallback>(),
1333+
any(),
1334+
any(),
1335+
any(),
1336+
)
1337+
} just runs
1338+
1339+
val mockFilterUser = mock(FilteredMParticleUser::class.java)
1340+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(HashMap())
1341+
Mockito.`when`(mockFilterUser.id).thenReturn(12345L)
1342+
Mockito.`when`(mockFilterUser.userAttributes).thenReturn(HashMap<String, Any?>())
1343+
1344+
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", JSONObject()))
1345+
1346+
val testAttributes = mapOf("key1" to "value1")
1347+
1348+
// Set MParticle instance to null
1349+
MParticle.setInstance(null)
1350+
1351+
roktKit.execute(
1352+
viewName = "test_view",
1353+
attributes = testAttributes,
1354+
mpRoktEventCallback = null,
1355+
placeHolders = null,
1356+
fontTypefaces = null,
1357+
filterUser = mockFilterUser,
1358+
mpRoktConfig = null,
1359+
)
1360+
1361+
verify(exactly = 1) {
1362+
Rokt.execute(
1363+
any<String>(),
1364+
any<Map<String, String>>(),
1365+
any<Rokt.RoktCallback>(),
1366+
any(),
1367+
any(),
1368+
any(),
1369+
)
1370+
}
1371+
1372+
unmockkObject(Rokt)
1373+
}
1374+
12851375
private var emptyCoreCallbacks: CoreCallbacks = object : CoreCallbacks {
12861376
var activity = Activity()
12871377
override fun isBackgrounded(): Boolean = false

0 commit comments

Comments
 (0)