Skip to content

Commit

Permalink
Merge pull request #1760 from Adyen/chore/dropin-config-data
Browse files Browse the repository at this point in the history
Track rendered event on drop-in
  • Loading branch information
OscarSpruit authored Aug 23, 2024
2 parents e369c8c + 2822f86 commit 1089910
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by oscars on 21/8/2024.
*/

package com.adyen.checkout.dropin.internal.ui

import com.adyen.checkout.dropin.internal.ui.model.DropInParams

internal class DropInConfigDataGenerator {

fun generate(configuration: DropInParams): Map<String, String> {
return mapOf(
"skipPaymentMethodList" to configuration.skipListWhenSinglePaymentMethod.toString(),
"openFirstStoredPaymentMethod" to configuration.showPreselectedStoredPaymentMethod.toString(),
"isRemovingStoredPaymentMethodsEnabled" to configuration.isRemovingStoredPaymentMethodsEnabled.toString(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.adyen.checkout.components.core.PaymentMethodTypes
import com.adyen.checkout.components.core.PaymentMethodsApiResponse
import com.adyen.checkout.components.core.StoredPaymentMethod
import com.adyen.checkout.components.core.internal.analytics.AnalyticsManager
import com.adyen.checkout.components.core.internal.analytics.GenericEvents
import com.adyen.checkout.components.core.internal.data.api.OrderStatusRepository
import com.adyen.checkout.components.core.internal.ui.model.DropInOverrideParams
import com.adyen.checkout.components.core.internal.util.bufferedChannel
Expand Down Expand Up @@ -58,6 +59,7 @@ internal class DropInViewModel(
private val orderStatusRepository: OrderStatusRepository,
internal val analyticsManager: AnalyticsManager,
private val initialDropInParams: DropInParams,
private val dropInConfigDataGenerator: DropInConfigDataGenerator,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : ViewModel() {

Expand Down Expand Up @@ -233,6 +235,12 @@ internal class DropInViewModel(
private fun initializeAnalytics() {
adyenLog(AdyenLogLevel.VERBOSE) { "initializeAnalytics" }
analyticsManager.initialize(this, viewModelScope)

val event = GenericEvents.rendered(
component = ANALYTICS_COMPONENT,
configData = dropInConfigDataGenerator.generate(configuration = dropInParams),
)
analyticsManager.trackEvent(event)
}

/**
Expand Down Expand Up @@ -475,6 +483,8 @@ internal class DropInViewModel(

companion object {

private const val ANALYTICS_COMPONENT = "dropin"

// These payment methods are either action only or have no UI.
private val SKIP_TO_SINGLE_PM_BLOCK_LIST = listOf(
PaymentMethodTypes.DUIT_NOW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ internal class DropInViewModelFactory(
sessionId = bundleHandler.sessionDetails?.id,
)

val dropInConfigDataGenerator = DropInConfigDataGenerator()

@Suppress("UNCHECKED_CAST")
return DropInViewModel(
bundleHandler,
orderStatusRepository,
analyticsManager,
dropInParams,
dropInConfigDataGenerator,
) as T
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.adyen.checkout.dropin.internal.ui

import com.adyen.checkout.components.core.internal.ui.model.AnalyticsParams
import com.adyen.checkout.components.core.internal.ui.model.AnalyticsParamsLevel
import com.adyen.checkout.core.Environment
import com.adyen.checkout.dropin.internal.ui.model.DropInParams
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.Locale

internal class DropInConfigDataGeneratorTest {

private lateinit var dropInConfigDataGenerator: DropInConfigDataGenerator

@BeforeEach
fun beforeEach() {
dropInConfigDataGenerator = DropInConfigDataGenerator()
}

@Test
fun `when generating config data, then fields are correctly mapped`() {
val result = dropInConfigDataGenerator.generate(createDropInParams())

val expected = mapOf(
"skipPaymentMethodList" to "false",
"openFirstStoredPaymentMethod" to "true",
"isRemovingStoredPaymentMethodsEnabled" to "true",
)
assertEquals(expected, result)
}

private fun createDropInParams() = DropInParams(
shopperLocale = Locale.US,
environment = Environment.TEST,
clientKey = "clientKey",
analyticsParams = AnalyticsParams(AnalyticsParamsLevel.ALL, "clientKey"),
amount = null,
showPreselectedStoredPaymentMethod = true,
skipListWhenSinglePaymentMethod = false,
isRemovingStoredPaymentMethodsEnabled = true,
additionalDataForDropInService = null,
overriddenPaymentMethodInformation = emptyMap(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.adyen.checkout.components.core.PaymentMethod
import com.adyen.checkout.components.core.PaymentMethodTypes
import com.adyen.checkout.components.core.PaymentMethodsApiResponse
import com.adyen.checkout.components.core.StoredPaymentMethod
import com.adyen.checkout.components.core.internal.analytics.GenericEvents
import com.adyen.checkout.components.core.internal.analytics.TestAnalyticsManager
import com.adyen.checkout.components.core.internal.data.api.OrderStatusRepository
import com.adyen.checkout.components.core.internal.ui.model.AnalyticsParams
Expand All @@ -14,6 +15,8 @@ import com.adyen.checkout.dropin.internal.ui.model.DropInParams
import com.adyen.checkout.test.LoggingExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments.arguments
Expand All @@ -29,14 +32,17 @@ import java.util.Locale
internal class DropInViewModelTest(
@Mock private val bundleHandler: DropInSavedStateHandleContainer,
@Mock private val orderStatusRepository: OrderStatusRepository,
@Mock private val dropInConfigDataGenerator: DropInConfigDataGenerator,
) {

private lateinit var analyticsManager: TestAnalyticsManager
private lateinit var viewModel: DropInViewModel

@BeforeEach
fun beforeEach() {
whenever(bundleHandler.checkoutConfiguration) doReturn mock()
whenever(bundleHandler.serviceComponentName) doReturn mock()
analyticsManager = TestAnalyticsManager()
}

@ParameterizedTest
Expand Down Expand Up @@ -68,13 +74,40 @@ internal class DropInViewModelTest(
assertEquals(expected, result)
}

@Nested
inner class AnalyticsTest {

@Test
fun `when drop-in is created, then analytics is initialized`() {
viewModel = createDropInViewModel()

viewModel.onCreated(false)

analyticsManager.assertIsInitialized()
}

@Test
fun `when drop-in is created, then render event is tracked`() {
viewModel = createDropInViewModel()

viewModel.onCreated(false)

val expected = GenericEvents.rendered(
component = "dropin",
configData = emptyMap(),
)
analyticsManager.assertLastEventEquals(expected)
}
}

private fun createDropInViewModel(
dropInParams: DropInParams = createDropInParams(),
) = DropInViewModel(
bundleHandler = bundleHandler,
orderStatusRepository = orderStatusRepository,
analyticsManager = TestAnalyticsManager(),
analyticsManager = analyticsManager,
initialDropInParams = dropInParams,
dropInConfigDataGenerator = dropInConfigDataGenerator,
)

private fun createDropInParams(
Expand Down

0 comments on commit 1089910

Please sign in to comment.