Skip to content

Commit

Permalink
fix: countly general implementation fixes (WPB-14941) 🍒 (#3733)
Browse files Browse the repository at this point in the history
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
github-actions[bot] and yamilmedina authored Dec 13, 2024
1 parent 375b8ca commit 635523f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ object AnonymousAnalyticsManagerImpl : AnonymousAnalyticsManager {
private val mutex = Mutex()
private lateinit var coroutineScope: CoroutineScope

// TODO: Sync with product, when we want to enable view tracking, var for testing purposes
internal var VIEW_TRACKING_ENABLED: Boolean = false

override fun <T> init(
context: Context,
analyticsSettings: AnalyticsSettings,
Expand Down Expand Up @@ -172,6 +175,10 @@ object AnonymousAnalyticsManagerImpl : AnonymousAnalyticsManager {
}

override fun recordView(screen: String) {
if (!VIEW_TRACKING_ENABLED) {
Log.d(TAG, "View tracking is disabled for this build.")
return
}
coroutineScope.launch {
mutex.withLock {
if (!isAnonymousUsageDataEnabled) return@withLock
Expand All @@ -181,6 +188,10 @@ object AnonymousAnalyticsManagerImpl : AnonymousAnalyticsManager {
}

override fun stopView(screen: String) {
if (!VIEW_TRACKING_ENABLED) {
Log.d(TAG, "View tracking is disabled for this build.")
return
}
coroutineScope.launch {
mutex.withLock {
if (!isAnonymousUsageDataEnabled) return@withLock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class AnonymousAnalyticsRecorderImpl : AnonymousAnalyticsRecorder {

override fun halt() {
isConfigured = false
Countly.sharedInstance().halt()
Countly.sharedInstance().consent().removeConsentAll()
}

override suspend fun setTrackingIdentifierWithMerge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,37 @@ class AnonymousAnalyticsManagerTest {
}
}

@Test
fun givenManagerInitialized_whenRecordingViewAndFlagDisabled_thenScreenIsNOTRecorded() = runTest(dispatcher) {
// given
val (arrangement, manager) = Arrangement()
.withAnonymousAnalyticsRecorderConfigure()
.arrange(shouldTrackViews = false)

val screen = "screen"
arrangement.withAnalyticsResult(Arrangement.existingIdentifierResult)

// when
manager.init(
context = arrangement.context,
analyticsSettings = Arrangement.analyticsSettings,
analyticsResultFlow = arrangement.analyticsResultChannel.consumeAsFlow(),
anonymousAnalyticsRecorder = arrangement.anonymousAnalyticsRecorder,
migrationHandler = arrangement.migrationHandler,
propagationHandler = arrangement.propagationHandler,
dispatcher = dispatcher
)
advanceUntilIdle()

manager.recordView(screen)
advanceUntilIdle()

// then
verify(exactly = 0) {
arrangement.anonymousAnalyticsRecorder.recordView(eq(screen))
}
}

@Test
fun givenManagerInitialized_whenStoppingView_thenScreenIsStoppedToRecord() = runTest(dispatcher) {
// given
Expand Down Expand Up @@ -387,7 +418,7 @@ class AnonymousAnalyticsManagerTest {
AnonymousAnalyticsManagerImpl
}

fun arrange() = this to manager
fun arrange(shouldTrackViews: Boolean = true) = this to manager.apply { VIEW_TRACKING_ENABLED = shouldTrackViews }

fun withAnonymousAnalyticsRecorderConfigure() = apply {
every { anonymousAnalyticsRecorder.configure(any(), any()) } returns Unit
Expand Down

0 comments on commit 635523f

Please sign in to comment.