Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BranchMetricsKit :
private var isMpidIdentityType = false
var identityType: IdentityType? = IdentityType.CustomerId
private var mSendScreenEvents = false
private var sendSessionId = false
private var branchUtil: BranchUtil? = null

override fun getInstance(): Branch? = branch
Expand All @@ -74,6 +75,7 @@ class BranchMetricsKit :
mSendScreenEvents =
sendScreenEvents != null &&
sendScreenEvents.equals("true", ignoreCase = true)
sendSessionId = settings[SEND_SESSION_ID].equals("true", ignoreCase = true)
setIdentityType(settings)
return emptyList()
}
Expand Down Expand Up @@ -120,7 +122,10 @@ class BranchMetricsKit :
): List<ReportingMessage> = emptyList()

override fun logEvent(event: MPEvent): List<ReportingMessage> {
branchUtil?.createBranchEventFromMPEvent(event)?.logEvent(context)
branchUtil
?.createBranchEventFromMPEvent(event)
?.withSessionId(currentSessionId, event.customAttributeStrings)
?.logEvent(context)
val messages = LinkedList<ReportingMessage>()
messages.add(ReportingMessage.fromEvent(this, event))
return messages
Expand All @@ -134,7 +139,10 @@ class BranchMetricsKit :
): List<ReportingMessage> = emptyList()

override fun logEvent(commerceEvent: CommerceEvent): List<ReportingMessage> {
branchUtil?.createBranchEventFromMPCommerceEvent(commerceEvent)?.logEvent(context)
branchUtil
?.createBranchEventFromMPCommerceEvent(commerceEvent)
?.withSessionId(currentSessionId, commerceEvent.customAttributeStrings)
?.logEvent(context)
val messages: MutableList<ReportingMessage> = LinkedList()
messages.add(ReportingMessage.fromEvent(this, commerceEvent))
return messages
Expand All @@ -151,7 +159,7 @@ class BranchMetricsKit :
eventAttributes = HashMap()
}
branchUtil?.updateBranchEventWithCustomData(logScreenEvent, eventAttributes)
logScreenEvent.logEvent(context)
logScreenEvent.withSessionId(currentSessionId, eventAttributes).logEvent(context)
val messages =
LinkedList<ReportingMessage>()
messages.add(
Expand All @@ -168,6 +176,9 @@ class BranchMetricsKit :
}
}

private val currentSessionId: Long?
get() = if (sendSessionId) MParticle.getInstance()?.currentSession?.sessionID else null

private val branch: Branch?
get() = settings[branchAppKey]?.let { Branch.getInstance() }

Expand Down Expand Up @@ -315,9 +326,25 @@ class BranchMetricsKit :

override fun onApplicationBackground() {}

/**
* Adds the mParticle session ID to the event's custom data. A customer attribute with the same
* key wins, and nothing is added without a session ID.
*/
internal fun BranchEvent.withSessionId(
sessionId: Long?,
attributes: Map<String, String>?,
): BranchEvent =
apply {
if (sessionId != null && sessionId != 0L && attributes?.containsKey(SESSION_ID_KEY) != true) {
addCustomDataProperty(SESSION_ID_KEY, sessionId.toString())
}
}

companion object {
private const val FORWARD_SCREEN_VIEWS = "forwardScreenViews"
private const val USER_IDENTIFICATION_TYPE = "userIdentificationType"
private const val SEND_SESSION_ID = "sendSessionId"
internal const val SESSION_ID_KEY = "mp_session_id"
const val NAME = "Branch Metrics"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mparticle.kits

import android.content.Context
import com.mparticle.MParticleOptions
import io.branch.referral.util.BranchEvent
import org.junit.Assert
import org.junit.Test
import org.mockito.Mockito
Expand Down Expand Up @@ -50,4 +51,59 @@ class BranchKitTests {
}
Assert.fail("$className not found as a known integration.")
}

/**
* Verifies the session ID is added to the Branch event's custom data.
*/
@Test
fun testWithSessionIdAddsSessionId() {
val branchEvent = Mockito.mock(BranchEvent::class.java)

with(BranchMetricsKit()) { branchEvent.withSessionId(-7046029254386353131L, mapOf("color" to "blue")) }

Mockito
.verify(branchEvent)
.addCustomDataProperty(BranchMetricsKit.SESSION_ID_KEY, "-7046029254386353131")
}

/**
* Verifies a customer attribute with the session ID key is not overwritten.
*/
@Test
fun testWithSessionIdKeepsCustomerValue() {
val branchEvent = Mockito.mock(BranchEvent::class.java)

with(BranchMetricsKit()) {
branchEvent.withSessionId(
-7046029254386353131L,
mapOf(BranchMetricsKit.SESSION_ID_KEY to "customer-value"),
)
}

Mockito.verifyZeroInteractions(branchEvent)
}

/**
* Verifies nothing is added when there is no current session.
*/
@Test
fun testWithSessionIdSkipsWithoutSession() {
val branchEvent = Mockito.mock(BranchEvent::class.java)

with(BranchMetricsKit()) { branchEvent.withSessionId(null, null) }

Mockito.verifyZeroInteractions(branchEvent)
}

/**
* Verifies nothing is added when the session has no UUID, which the SDK reports as an ID of 0.
*/
@Test
fun testWithSessionIdSkipsZeroId() {
val branchEvent = Mockito.mock(BranchEvent::class.java)

with(BranchMetricsKit()) { branchEvent.withSessionId(0L, null) }

Mockito.verifyZeroInteractions(branchEvent)
}
}
Loading