Skip to content

Commit

Permalink
Merge pull request #1802 from Adyen/feature/analytics_send_failed_che…
Browse files Browse the repository at this point in the history
…ckout_attempt_id

Analytics - Send failed checkoutAttemptId if fetching fails
  • Loading branch information
araratthehero authored Oct 7, 2024
2 parents b6bbc1b + acf647e commit fcb7d8f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ internal class DefaultAnalyticsManager(
onSuccess = { attemptId ->
checkoutAttemptId = attemptId?.also { startTimer() }
},
onFailure = { adyenLog(AdyenLogLevel.WARN, it) { "Failed to fetch checkoutAttemptId." } },
onFailure = {
adyenLog(AdyenLogLevel.WARN, it) { "Failed to fetch checkoutAttemptId." }
checkoutAttemptId = FAILED_CHECKOUT_ATTEMPT_ID
},
)
}
}
Expand Down Expand Up @@ -104,6 +107,11 @@ internal class DefaultAnalyticsManager(
return
}

if (cannotSendEvents()) {
adyenLog(AdyenLogLevel.DEBUG) { "Not allowed to send events, ignoring." }
return
}

runSuspendCatching {
analyticsRepository.sendEvents(checkoutAttemptId)
}.fold(
Expand All @@ -115,7 +123,8 @@ internal class DefaultAnalyticsManager(
override fun getCheckoutAttemptId(): String? = checkoutAttemptId

private fun cannotSendEvents(): Boolean {
return analyticsParams.level.priority <= AnalyticsParamsLevel.NONE.priority
return analyticsParams.level.priority <= AnalyticsParamsLevel.NONE.priority ||
checkoutAttemptId == FAILED_CHECKOUT_ATTEMPT_ID
}

override fun clear(owner: Any) {
Expand All @@ -135,6 +144,9 @@ internal class DefaultAnalyticsManager(
}

companion object {
@VisibleForTesting
internal val FAILED_CHECKOUT_ATTEMPT_ID = "fetch-checkoutAttemptId-failed"

@VisibleForTesting
internal val DISPATCH_INTERVAL_MILLIS = 10.seconds.inWholeMilliseconds
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -66,12 +65,12 @@ internal class DefaultAnalyticsManagerTest(
}

@Test
fun `fetching checkoutAttemptId fails, then checkoutAttemptId is null`() = runTest {
fun `fetching checkoutAttemptId fails, then checkoutAttemptId is failed checkoutAttemptId`() = runTest {
whenever(analyticsRepository.fetchCheckoutAttemptId()) doAnswer { error("test") }

analyticsManager.initialize(this@InitializeTest, this)

assertNull(analyticsManager.getCheckoutAttemptId())
assertEquals(DefaultAnalyticsManager.FAILED_CHECKOUT_ATTEMPT_ID, analyticsManager.getCheckoutAttemptId())
}

@Test
Expand Down Expand Up @@ -99,6 +98,16 @@ internal class DefaultAnalyticsManagerTest(
verify(analyticsRepository, never()).storeEvent(any())
}

@Test
fun `fetching checkoutAttemptId failed, then events should not be stored`() = runTest {
whenever(analyticsRepository.fetchCheckoutAttemptId()) doAnswer { error("test") }
analyticsManager.initialize(this@TrackEventTest, this)

analyticsManager.trackEvent(GenericEvents.rendered("dropin", false))

verify(analyticsRepository, never()).storeEvent(any())
}

@Test
fun `sending events is enabled, then events should be stored`() = runTest {
analyticsManager.initialize(this@TrackEventTest, this)
Expand Down Expand Up @@ -158,6 +167,36 @@ internal class DefaultAnalyticsManagerTest(
analyticsManager.clear(this@DefaultAnalyticsManagerTest)
}

@Test
fun `when sending events and sending events is disabled, then events are not sent`() = runTest {
analyticsManager = createAnalyticsManager(AnalyticsParamsLevel.NONE)
analyticsManager.initialize(this@DefaultAnalyticsManagerTest, this)
val event = AnalyticsEvent.Info(
component = "test",
shouldForceSend = true,
)

analyticsManager.trackEvent(event)

verify(analyticsRepository, never()).sendEvents(any())
analyticsManager.clear(this@DefaultAnalyticsManagerTest)
}

@Test
fun `when sending events and checkoutAttemptId has failed fetching, then events are not sent`() = runTest {
whenever(analyticsRepository.fetchCheckoutAttemptId()) doAnswer { error("test") }
analyticsManager.initialize(this@DefaultAnalyticsManagerTest, this)
val event = AnalyticsEvent.Info(
component = "test",
shouldForceSend = true,
)

analyticsManager.trackEvent(event)

verify(analyticsRepository, never()).sendEvents(any())
analyticsManager.clear(this@DefaultAnalyticsManagerTest)
}

private fun createAnalyticsManager(
analyticsParamsLevel: AnalyticsParamsLevel = AnalyticsParamsLevel.ALL,
coroutineDispatcher: CoroutineDispatcher = UnconfinedTestDispatcher(),
Expand Down

0 comments on commit fcb7d8f

Please sign in to comment.