Skip to content

Commit

Permalink
Add sessionId to ShopperInsightsClient (#1230)
Browse files Browse the repository at this point in the history
* Add session ID param to client

* Add unit tests

* Add CHANGELOG

* fix lint

* Address PR feedback
sarahkoop authored Dec 4, 2024
1 parent 6921521 commit fb4455d
Showing 6 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -30,6 +30,10 @@ class AnalyticsParamRepository(
_sessionId = uuidHelper.formattedUUID
}

fun setSessionId(sessionId: String) {
_sessionId = sessionId
}

companion object {

/**
Original file line number Diff line number Diff line change
@@ -41,4 +41,10 @@ class AnalyticsParamRepositoryUnitTest {

assertEquals(newUuid, sut.sessionId)
}

@Test
fun `setSessionId sets the session ID with input value`() {
sut.setSessionId("override-session-id")
assertEquals("override-session-id", sut.sessionId)
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
* Make LocalPaymentAuthRequestParams public (fixes #1207)
* ShopperInsights (BETA)
* Add `isPayPalAppInstalled` and `isVenmoAppInstalled` methods
* Add `shopperSessionId` parameter to `ShopperInsightsClient`

## 5.2.0 (2024-10-30)

Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ class ShopperInsightsFragment : BaseFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
shopperInsightsClient = ShopperInsightsClient(requireContext(), authStringArg)
shopperInsightsClient = ShopperInsightsClient(requireContext(), authStringArg, "test-shopper-session-id")

venmoClient = VenmoClient(requireContext(), super.getAuthStringArg(), null)
payPalClient = PayPalClient(
Original file line number Diff line number Diff line change
@@ -34,15 +34,22 @@ class ShopperInsightsClient internal constructor(
),
private val merchantRepository: MerchantRepository = MerchantRepository.instance,
private val deviceInspector: DeviceInspector = DeviceInspector(),
private val shopperSessionId: String? = null
) {

/**
* @param context: an Android context
* @param authorization: a Tokenization Key or Client Token used to authenticate
* @param shopperSessionId: the shopper session ID returned from your server SDK request
*/
constructor(context: Context, authorization: String) : this(
BraintreeClient(context, authorization)
)
constructor(context: Context, authorization: String, shopperSessionId: String? = null) : this(
BraintreeClient(context, authorization),
shopperSessionId = shopperSessionId
) {
if (shopperSessionId != null) {
analyticsParamRepository.setSessionId(sessionId = shopperSessionId)
}
}

/**
* Retrieves recommended payment methods based on the provided shopper insights request.
@@ -59,7 +66,9 @@ class ShopperInsightsClient internal constructor(
experiment: String? = null,
callback: ShopperInsightsCallback
) {
analyticsParamRepository.resetSessionId()
if (shopperSessionId == null) {
analyticsParamRepository.resetSessionId()
}
braintreeClient.sendAnalyticsEvent(
GET_RECOMMENDED_PAYMENTS_STARTED,
AnalyticsEventParams(experiment = experiment)
Original file line number Diff line number Diff line change
@@ -68,12 +68,28 @@ class ShopperInsightsClientUnitTest {
}

@Test
fun `when getRecommendedPaymentMethods is called, session id is reset`() {
fun `when getRecommendedPaymentMethods is called without shopper session id, session id is reset`() {
sut.getRecommendedPaymentMethods(mockk(relaxed = true), "some_experiment", mockk(relaxed = true))

verify { analyticsParamRepository.resetSessionId() }
}

@Test
fun `when getRecommendedPaymentMethods is called with shopper session id, session id is not reset`() {
sut = ShopperInsightsClient(
braintreeClient,
analyticsParamRepository,
api,
merchantRepository,
deviceInspector,
"shopper-session-id"
)

sut.getRecommendedPaymentMethods(mockk(relaxed = true), "some_experiment", mockk(relaxed = true))

verify(exactly = 0) { analyticsParamRepository.resetSessionId() }
}

@Test
fun `when getRecommendedPaymentMethods is called, started event is sent`() {
val experiment = "some_experiment"

0 comments on commit fb4455d

Please sign in to comment.