diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsClient.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsClient.kt index 6f5a893139..884f3cf1b6 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsClient.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsClient.kt @@ -45,7 +45,8 @@ class AnalyticsClient internal constructor( endpoint = analyticsEventParams.endpoint, experiment = analyticsEventParams.experiment, paymentMethodsDisplayed = analyticsEventParams.paymentMethodsDisplayed, - appSwitchUrl = analyticsEventParams.appSwitchUrl + appSwitchUrl = analyticsEventParams.appSwitchUrl, + shopperSessionId = analyticsEventParams.shopperSessionId ) configurationLoader.loadConfiguration { result -> if (result is ConfigurationLoaderResult.Success) { @@ -242,6 +243,7 @@ class AnalyticsClient internal constructor( .putOpt(FPTI_KEY_MERCHANT_PAYMENT_METHODS_DISPLAYED, event.paymentMethodsDisplayed.ifEmpty { null }) .putOpt(FPTI_KEY_URL, event.appSwitchUrl) + .putOpt(FPTI_KEY_SHOPPER_SESSION_ID, event.shopperSessionId) return json.toString() } @@ -295,6 +297,7 @@ class AnalyticsClient internal constructor( private const val FPTI_KEY_MERCHANT_EXPERIMENT = "experiment" private const val FPTI_KEY_MERCHANT_PAYMENT_METHODS_DISPLAYED = "payment_methods_displayed" private const val FPTI_KEY_URL = "url" + private const val FPTI_KEY_SHOPPER_SESSION_ID = "shopper_session_id" private const val FPTI_BATCH_KEY_VENMO_INSTALLED = "venmo_installed" private const val FPTI_BATCH_KEY_PAYPAL_INSTALLED = "paypal_installed" diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEvent.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEvent.kt index f4278b9816..082327f824 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEvent.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEvent.kt @@ -16,5 +16,6 @@ internal data class AnalyticsEvent( val endpoint: String? = null, val experiment: String? = null, val paymentMethodsDisplayed: List = emptyList(), - val appSwitchUrl: String? = null + val appSwitchUrl: String? = null, + val shopperSessionId: String? = null ) diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEventParams.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEventParams.kt index fa309fb55b..705b00728e 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEventParams.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsEventParams.kt @@ -17,6 +17,8 @@ import androidx.annotation.RestrictTo * the experiment, as a JSON string, that the merchant sent to the us. * @property paymentMethodsDisplayed A ShopperInsights module specific event that indicates the * order of payment methods displayed to the shopper by the merchant. + * @property shopperSessionId The Shopper Insights customer session ID created by a merchant's + * server SDK or graphQL integration. */ @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) data class AnalyticsEventParams @JvmOverloads constructor( @@ -28,5 +30,6 @@ data class AnalyticsEventParams @JvmOverloads constructor( var endpoint: String? = null, val experiment: String? = null, val paymentMethodsDisplayed: List = emptyList(), - val appSwitchUrl: String? = null + val appSwitchUrl: String? = null, + val shopperSessionId: String? = null ) diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsParamRepository.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsParamRepository.kt index 7463bdf75e..7a9b5e969f 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsParamRepository.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/core/AnalyticsParamRepository.kt @@ -30,10 +30,6 @@ class AnalyticsParamRepository( _sessionId = uuidHelper.formattedUUID } - fun setSessionId(sessionId: String) { - _sessionId = sessionId - } - companion object { /** diff --git a/BraintreeCore/src/test/java/com/braintreepayments/api/core/AnalyticsParamRepositoryUnitTest.kt b/BraintreeCore/src/test/java/com/braintreepayments/api/core/AnalyticsParamRepositoryUnitTest.kt index dab8b2e2c4..ef009f2d27 100644 --- a/BraintreeCore/src/test/java/com/braintreepayments/api/core/AnalyticsParamRepositoryUnitTest.kt +++ b/BraintreeCore/src/test/java/com/braintreepayments/api/core/AnalyticsParamRepositoryUnitTest.kt @@ -41,10 +41,4 @@ 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) - } } diff --git a/PayPal/src/main/java/com/braintreepayments/api/paypal/PayPalClient.kt b/PayPal/src/main/java/com/braintreepayments/api/paypal/PayPalClient.kt index 3af75e73d8..3b0121332f 100644 --- a/PayPal/src/main/java/com/braintreepayments/api/paypal/PayPalClient.kt +++ b/PayPal/src/main/java/com/braintreepayments/api/paypal/PayPalClient.kt @@ -5,7 +5,6 @@ import android.net.Uri import android.text.TextUtils import com.braintreepayments.api.BrowserSwitchOptions import com.braintreepayments.api.core.AnalyticsEventParams -import com.braintreepayments.api.core.AnalyticsParamRepository import com.braintreepayments.api.core.BraintreeClient import com.braintreepayments.api.core.BraintreeException import com.braintreepayments.api.core.BraintreeRequestCodes @@ -25,8 +24,7 @@ import org.json.JSONObject class PayPalClient internal constructor( private val braintreeClient: BraintreeClient, private val internalPayPalClient: PayPalInternalClient = PayPalInternalClient(braintreeClient), - private val merchantRepository: MerchantRepository = MerchantRepository.instance, - private val analyticsParamRepository: AnalyticsParamRepository = AnalyticsParamRepository.instance + private val merchantRepository: MerchantRepository = MerchantRepository.instance ) { /** * Used for linking events from the client to server side request @@ -44,6 +42,11 @@ class PayPalClient internal constructor( */ private var isVaultRequest = false + /** + * Used for sending Shopper Insights session ID provided by merchant to FPTI + */ + private var shopperSessionId: String? = null + /** * Initializes a new [PayPalClient] instance * @@ -74,11 +77,7 @@ class PayPalClient internal constructor( payPalRequest: PayPalRequest, callback: PayPalPaymentAuthCallback ) { - // The shopper insights server SDK integration - payPalRequest.shopperSessionId?.let { - analyticsParamRepository.setSessionId(it) - } - + shopperSessionId = payPalRequest.shopperSessionId isVaultRequest = payPalRequest is PayPalVaultRequest braintreeClient.sendAnalyticsEvent(PayPalAnalytics.TOKENIZATION_STARTED, analyticsParams) @@ -337,7 +336,8 @@ class PayPalClient internal constructor( return AnalyticsEventParams( payPalContextId = payPalContextId, linkType = linkType?.stringValue, - isVaultRequest = isVaultRequest + isVaultRequest = isVaultRequest, + shopperSessionId = shopperSessionId ) } diff --git a/PayPal/src/test/java/com/braintreepayments/api/paypal/PayPalClientUnitTest.java b/PayPal/src/test/java/com/braintreepayments/api/paypal/PayPalClientUnitTest.java index e264dd541b..a77ae202ab 100644 --- a/PayPal/src/test/java/com/braintreepayments/api/paypal/PayPalClientUnitTest.java +++ b/PayPal/src/test/java/com/braintreepayments/api/paypal/PayPalClientUnitTest.java @@ -33,6 +33,8 @@ import org.robolectric.RobolectricTestRunner; import org.skyscreamer.jsonassert.JSONAssert; +import java.util.Collections; + @RunWith(RobolectricTestRunner.class) public class PayPalClientUnitTest { @@ -44,11 +46,9 @@ public class PayPalClientUnitTest { private PayPalPaymentAuthCallback paymentAuthCallback; private MerchantRepository merchantRepository; - private AnalyticsParamRepository analyticsParamRepository; @Before public void beforeEach() throws JSONException { - analyticsParamRepository = mock(AnalyticsParamRepository.class); activity = mock(FragmentActivity.class); merchantRepository = mock(MerchantRepository.class); payPalEnabledConfig = Configuration.fromJson(Fixtures.CONFIGURATION_WITH_LIVE_PAYPAL); @@ -64,6 +64,7 @@ public void beforeEach() throws JSONException { public void createPaymentAuthRequest_callsBackPayPalResponse_sendsStartedAnalytics() throws JSONException { PayPalVaultRequest payPalVaultRequest = new PayPalVaultRequest(true); payPalVaultRequest.setMerchantAccountId("sample-merchant-account-id"); + payPalVaultRequest.setShopperSessionId("test-shopper-session-id"); PayPalPaymentAuthRequestParams paymentAuthRequest = new PayPalPaymentAuthRequestParams( payPalVaultRequest, @@ -80,7 +81,7 @@ public void createPaymentAuthRequest_callsBackPayPalResponse_sendsStartedAnalyti BraintreeClient braintreeClient = new MockBraintreeClientBuilder().configuration(payPalEnabledConfig).build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalVaultRequest, paymentAuthCallback); ArgumentCaptor captor = @@ -108,7 +109,7 @@ public void createPaymentAuthRequest_callsBackPayPalResponse_sendsStartedAnalyti verify(braintreeClient).sendAnalyticsEvent( PayPalAnalytics.TOKENIZATION_STARTED, - new AnalyticsEventParams(null, null, true, null, null, null) + new AnalyticsEventParams(null, null, true, null, null, null, null, Collections.emptyList(), null, "test-shopper-session-id") ); } @@ -134,7 +135,7 @@ public void createPaymentAuthRequest_whenLaunchesBrowserSwitchAsNewTaskEnabled_s new MockBraintreeClientBuilder().configuration(payPalEnabledConfig) .launchesBrowserSwitchAsNewTask(true).build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalVaultRequest, paymentAuthCallback); ArgumentCaptor captor = @@ -169,7 +170,7 @@ public void createPaymentAuthRequest_setsAppLinkReturnUrl() { BraintreeClient braintreeClient = new MockBraintreeClientBuilder().configuration(payPalEnabledConfig) .build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalVaultRequest, paymentAuthCallback); ArgumentCaptor captor = @@ -189,7 +190,7 @@ public void createPaymentAuthRequest_whenPayPalNotEnabled_returnsError() { BraintreeClient braintreeClient = new MockBraintreeClientBuilder().configuration(payPalDisabledConfig).build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, new PayPalCheckoutRequest("1.00", true), paymentAuthCallback); @@ -218,7 +219,7 @@ public void createPaymentAuthRequest_whenCheckoutRequest_whenConfigError_forward .configurationError(authError) .build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, new PayPalCheckoutRequest("1.00", true), paymentAuthCallback); ArgumentCaptor captor = @@ -243,7 +244,7 @@ public void requestBillingAgreement_whenConfigError_forwardsErrorToListener() { .configurationError(authError) .build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, new PayPalVaultRequest(true), paymentAuthCallback); ArgumentCaptor captor = @@ -269,7 +270,7 @@ public void createPaymentAuthRequest_whenVaultRequest_sendsPayPalRequestViaInter PayPalVaultRequest payPalRequest = new PayPalVaultRequest(true); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalRequest, paymentAuthCallback); verify(payPalInternalClient).sendRequest(same(activity), same(payPalRequest), @@ -285,7 +286,7 @@ public void createPaymentAuthRequest_whenCheckoutRequest_sendsPayPalRequestViaIn PayPalCheckoutRequest payPalRequest = new PayPalCheckoutRequest("1.00", true); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalRequest, paymentAuthCallback); verify(payPalInternalClient).sendRequest(same(activity), same(payPalRequest), @@ -317,7 +318,7 @@ public void createPaymentAuthRequest_whenVaultRequest_sendsAppSwitchStartedEvent BraintreeClient braintreeClient = new MockBraintreeClientBuilder().configuration(payPalEnabledConfig).build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.createPaymentAuthRequest(activity, payPalVaultRequest, paymentAuthCallback); ArgumentCaptor captor = @@ -362,7 +363,7 @@ public void tokenize_withBillingAgreement_tokenizesResponseOnSuccess() throws JS PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -406,7 +407,7 @@ public void tokenize_withOneTimePayment_tokenizesResponseOnSuccess() throws JSON PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -451,7 +452,7 @@ public void tokenize_whenCancelUriReceived_notifiesCancellationAndSendsAnalytics PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -491,7 +492,7 @@ public void tokenize_whenPayPalInternalClientTokenizeResult_callsBackResult() PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -533,7 +534,7 @@ public void tokenize_whenPayPalInternalClientTokenizeResult_sendsAppSwitchSuccee PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -572,7 +573,7 @@ public void tokenize_whenPayPalNotEnabled_sendsAppSwitchFailedEvents() throws JS PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -604,7 +605,7 @@ public void tokenize_whenCancelUriReceived_sendsAppSwitchCanceledEvents() PayPalPaymentAuthResult.Success payPalPaymentAuthResult = new PayPalPaymentAuthResult.Success( browserSwitchResult); BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build(); - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); + PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository); sut.tokenize(payPalPaymentAuthResult, payPalTokenizeCallback); @@ -618,60 +619,4 @@ public void tokenize_whenCancelUriReceived_sendsAppSwitchCanceledEvents() verify(braintreeClient).sendAnalyticsEvent(PayPalAnalytics.BROWSER_LOGIN_CANCELED, params); verify(braintreeClient).sendAnalyticsEvent(PayPalAnalytics.APP_SWITCH_CANCELED, params); } - - @Test - public void test_vaultRequest_shopperSessionId_sets_repository() throws JSONException { - PayPalVaultRequest payPalVaultRequest = new PayPalVaultRequest(true); - - payPalVaultRequest.setShopperSessionId("test-shopper-id"); - - PayPalPaymentAuthRequestParams paymentAuthRequest = new PayPalPaymentAuthRequestParams( - payPalVaultRequest, - null, - "https://example.com/approval/url", - "sample-client-metadata-id", - null, - "https://example.com/success/url" - ); - - PayPalInternalClient payPalInternalClient = - new MockPayPalInternalClientBuilder().sendRequestSuccess(paymentAuthRequest) - .build(); - - BraintreeClient braintreeClient = - new MockBraintreeClientBuilder().configuration(payPalEnabledConfig).build(); - - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); - sut.createPaymentAuthRequest(activity, payPalVaultRequest, paymentAuthCallback); - - verify(analyticsParamRepository).setSessionId("test-shopper-id"); - } - - @Test - public void test_checkoutRequest_shopperSessionId_sets_repository() throws JSONException { - PayPalCheckoutRequest request = new PayPalCheckoutRequest("2.00", true); - - request.setShopperSessionId("test-shopper-id"); - - PayPalPaymentAuthRequestParams paymentAuthRequest = new PayPalPaymentAuthRequestParams( - request, - null, - "https://example.com/approval/url", - "sample-client-metadata-id", - null, - "https://example.com/success/url" - ); - - PayPalInternalClient payPalInternalClient = - new MockPayPalInternalClientBuilder().sendRequestSuccess(paymentAuthRequest) - .build(); - - BraintreeClient braintreeClient = - new MockBraintreeClientBuilder().configuration(payPalEnabledConfig).build(); - - PayPalClient sut = new PayPalClient(braintreeClient, payPalInternalClient, merchantRepository, analyticsParamRepository); - sut.createPaymentAuthRequest(activity, request, paymentAuthCallback); - - verify(analyticsParamRepository).setSessionId("test-shopper-id"); - } } \ No newline at end of file diff --git a/ShopperInsights/src/main/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClient.kt b/ShopperInsights/src/main/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClient.kt index f5dc255ab7..356a798cb6 100644 --- a/ShopperInsights/src/main/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClient.kt +++ b/ShopperInsights/src/main/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClient.kt @@ -45,11 +45,7 @@ class ShopperInsightsClient internal constructor( 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. @@ -66,12 +62,13 @@ class ShopperInsightsClient internal constructor( experiment: String? = null, callback: ShopperInsightsCallback ) { - if (shopperSessionId == null) { - analyticsParamRepository.resetSessionId() - } + analyticsParamRepository.resetSessionId() braintreeClient.sendAnalyticsEvent( GET_RECOMMENDED_PAYMENTS_STARTED, - AnalyticsEventParams(experiment = experiment) + AnalyticsEventParams( + experiment = experiment, + shopperSessionId = shopperSessionId + ) ) if (request.email == null && request.phone == null) { @@ -147,7 +144,7 @@ class ShopperInsightsClient internal constructor( callback: ShopperInsightsCallback, error: Exception ) { - braintreeClient.sendAnalyticsEvent(GET_RECOMMENDED_PAYMENTS_FAILED) + braintreeClient.sendAnalyticsEvent(GET_RECOMMENDED_PAYMENTS_FAILED, analyticsParams) callback.onResult(ShopperInsightsResult.Failure(error)) } @@ -157,7 +154,7 @@ class ShopperInsightsClient internal constructor( isPayPalRecommended: Boolean, isVenmoRecommended: Boolean, ) { - braintreeClient.sendAnalyticsEvent(GET_RECOMMENDED_PAYMENTS_SUCCEEDED) + braintreeClient.sendAnalyticsEvent(GET_RECOMMENDED_PAYMENTS_SUCCEEDED, analyticsParams) callback.onResult( ShopperInsightsResult.Success( ShopperInsightsInfo( @@ -185,7 +182,8 @@ class ShopperInsightsClient internal constructor( PAYPAL_PRESENTED, AnalyticsEventParams( experiment = experiment, - paymentMethodsDisplayed = paymentMethodsDisplayed + paymentMethodsDisplayed = paymentMethodsDisplayed, + shopperSessionId = shopperSessionId ) ) } @@ -195,7 +193,7 @@ class ShopperInsightsClient internal constructor( * This method sends analytics to help improve the Shopper Insights feature experience. */ fun sendPayPalSelectedEvent() { - braintreeClient.sendAnalyticsEvent(PAYPAL_SELECTED) + braintreeClient.sendAnalyticsEvent(PAYPAL_SELECTED, analyticsParams) } /** @@ -214,7 +212,8 @@ class ShopperInsightsClient internal constructor( VENMO_PRESENTED, AnalyticsEventParams( experiment = experiment, - paymentMethodsDisplayed = paymentMethodsDisplayed + paymentMethodsDisplayed = paymentMethodsDisplayed, + shopperSessionId = shopperSessionId ) ) } @@ -224,7 +223,7 @@ class ShopperInsightsClient internal constructor( * This method sends analytics to help improve the Shopper Insights feature experience. */ fun sendVenmoSelectedEvent() { - braintreeClient.sendAnalyticsEvent(VENMO_SELECTED) + braintreeClient.sendAnalyticsEvent(VENMO_SELECTED, analyticsParams) } /** @@ -241,6 +240,10 @@ class ShopperInsightsClient internal constructor( return deviceInspector.isVenmoInstalled(context) } + private val analyticsParams: AnalyticsEventParams get() { + return AnalyticsEventParams(shopperSessionId = shopperSessionId) + } + companion object { // Default values private const val countryCode = "US" diff --git a/ShopperInsights/src/test/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClientUnitTest.kt b/ShopperInsights/src/test/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClientUnitTest.kt index 15c14615e3..3186d6dd42 100644 --- a/ShopperInsights/src/test/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClientUnitTest.kt +++ b/ShopperInsights/src/test/java/com/braintreepayments/api/shopperinsights/ShopperInsightsClientUnitTest.kt @@ -44,6 +44,7 @@ class ShopperInsightsClientUnitTest { private lateinit var merchantRepository: MerchantRepository private lateinit var context: Context private lateinit var deviceInspector: DeviceInspector + private var shopperSessionId = "test-shopper-session-id" private val clientToken = mockk() @@ -62,7 +63,8 @@ class ShopperInsightsClientUnitTest { analyticsParamRepository, api, merchantRepository, - deviceInspector + deviceInspector, + shopperSessionId = shopperSessionId ) context = ApplicationProvider.getApplicationContext() } @@ -74,28 +76,14 @@ class ShopperInsightsClientUnitTest { 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" sut.getRecommendedPaymentMethods(mockk(relaxed = true), experiment, mockk(relaxed = true)) - verifyStartedAnalyticsEvent(AnalyticsEventParams(experiment = experiment)) + verifyStartedAnalyticsEvent(AnalyticsEventParams( + experiment = experiment, + shopperSessionId = shopperSessionId)) } @Test @@ -451,25 +439,29 @@ class ShopperInsightsClientUnitTest { @Test fun `test paypal presented analytics event`() { sut.sendPayPalPresentedEvent() - verify { braintreeClient.sendAnalyticsEvent("shopper-insights:paypal-presented") } + verify { braintreeClient.sendAnalyticsEvent("shopper-insights:paypal-presented", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } @Test fun `test paypal selected analytics event`() { sut.sendPayPalSelectedEvent() - verify { braintreeClient.sendAnalyticsEvent("shopper-insights:paypal-selected") } + verify { braintreeClient.sendAnalyticsEvent("shopper-insights:paypal-selected", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } @Test fun `test venmo presented analytics event`() { sut.sendVenmoPresentedEvent() - verify { braintreeClient.sendAnalyticsEvent("shopper-insights:venmo-presented") } + verify { braintreeClient.sendAnalyticsEvent("shopper-insights:venmo-presented", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } @Test fun `test venmo selected analytics event`() { sut.sendVenmoSelectedEvent() - verify { braintreeClient.sendAnalyticsEvent("shopper-insights:venmo-selected") } + verify { braintreeClient.sendAnalyticsEvent("shopper-insights:venmo-selected", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } @Test @@ -520,14 +512,16 @@ class ShopperInsightsClientUnitTest { private fun verifySuccessAnalyticsEvent() { verify { braintreeClient - .sendAnalyticsEvent("shopper-insights:get-recommended-payments:succeeded") + .sendAnalyticsEvent("shopper-insights:get-recommended-payments:succeeded", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } private fun verifyFailedAnalyticsEvent() { verify { braintreeClient - .sendAnalyticsEvent("shopper-insights:get-recommended-payments:failed") + .sendAnalyticsEvent("shopper-insights:get-recommended-payments:failed", + AnalyticsEventParams(shopperSessionId = shopperSessionId)) } } }