Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve logical issues with analytics triggering #99

Open
wants to merge 3 commits into
base: 2U/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/src/main/java/org/openedx/app/AppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
installSplashScreen()
binding = ActivityAppBinding.inflate(layoutInflater)
lifecycle.addObserver(viewModel)
viewModel.logAppLaunchEvent()
setContentView(binding.root)
val container = binding.rootLayout

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/org/openedx/app/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ class AppViewModel(
val isBranchEnabled get() = config.getBranchConfig().enabled
private val canResetAppDirectory get() = preferencesManager.canResetAppDirectory

init {
logAppLaunchEvent()
setUserId(preferencesManager.user)
}

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)

val user = preferencesManager.user

setUserId(user)

if (user != null && preferencesManager.pushToken.isNotEmpty()) {
SyncFirebaseTokenWorker.schedule(context)
}
Expand All @@ -76,7 +78,7 @@ class AppViewModel(
}
}

fun logAppLaunchEvent() {
private fun logAppLaunchEvent() {
analytics.logEvent(
event = AppAnalyticsEvent.LAUNCH.eventName,
params = buildMap {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/org/openedx/app/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class MainViewModel(
val isDiscoveryTypeWebView get() = config.getDiscoveryConfig().isViewTypeWebView()
val getDiscoveryFragment get() = DiscoveryNavigator(isDiscoveryTypeWebView).getDiscoveryFragment()

init {
logNotificationPermissionStatusEvent()
}

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
notifier.notifier
Expand All @@ -48,7 +52,6 @@ class MainViewModel(
}
.distinctUntilChanged()
.launchIn(viewModelScope)
logNotificationPermissionStatusEvent()
}

fun enableBottomBar(enable: Boolean) {
Expand Down
11 changes: 5 additions & 6 deletions app/src/test/java/org/openedx/AppViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.openedx.app.AppAnalytics
import org.openedx.app.deeplink.DeepLinkRouter
import org.openedx.app.AppViewModel
import org.openedx.app.data.storage.PreferencesManager
import org.openedx.app.deeplink.DeepLinkRouter
import org.openedx.app.room.AppDatabase
import org.openedx.core.system.notifier.app.LogoutEvent
import org.openedx.core.config.Config
import org.openedx.core.config.FirebaseConfig
import org.openedx.core.data.model.User
import org.openedx.core.system.notifier.app.AppNotifier
import org.openedx.core.system.notifier.app.LogoutEvent
import org.openedx.core.utils.FileUtil

@ExperimentalCoroutinesApi
Expand All @@ -55,6 +55,8 @@ class AppViewModelTest {
@Before
fun before() {
Dispatchers.setMain(dispatcher)
every { analytics.logEvent(any(), any()) } returns Unit
every { preferencesManager.user } returns user
}

@After
Expand All @@ -65,7 +67,6 @@ class AppViewModelTest {
@Test
fun setIdSuccess() = runTest {
every { analytics.setUserIdForSession(any()) } returns Unit
every { preferencesManager.user } returns user
every { notifier.notifier } returns flow { }
every { preferencesManager.canResetAppDirectory } returns false
every { preferencesManager.pushToken } returns ""
Expand Down Expand Up @@ -98,7 +99,6 @@ class AppViewModelTest {
}
every { preferencesManager.clear() } returns Unit
every { analytics.setUserIdForSession(any()) } returns Unit
every { preferencesManager.user } returns user
every { room.clearAllTables() } returns Unit
every { analytics.logoutEvent(true) } returns Unit
every { preferencesManager.canResetAppDirectory } returns false
Expand Down Expand Up @@ -135,7 +135,6 @@ class AppViewModelTest {
}
every { preferencesManager.clear() } returns Unit
every { analytics.setUserIdForSession(any()) } returns Unit
every { preferencesManager.user } returns user
every { room.clearAllTables() } returns Unit
every { analytics.logoutEvent(true) } returns Unit
every { preferencesManager.canResetAppDirectory } returns false
Expand Down Expand Up @@ -163,7 +162,7 @@ class AppViewModelTest {
verify(exactly = 1) { analytics.logoutEvent(true) }
verify(exactly = 1) { preferencesManager.clear() }
verify(exactly = 1) { analytics.setUserIdForSession(any()) }
verify(exactly = 1) { preferencesManager.user }
verify(exactly = 2) { preferencesManager.user }
verify(exactly = 1) { room.clearAllTables() }
verify(exactly = 1) { analytics.logoutEvent(true) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,19 @@ fun CourseDashboard(
val refreshing by viewModel.refreshing.collectAsState(true)
val courseImage by viewModel.courseImage.collectAsState()
val uiMessage by viewModel.uiMessage.collectAsState(null)
val requiredTab = when (openTab.uppercase()) {
CourseContainerTab.HOME.name -> CourseContainerTab.HOME
CourseContainerTab.VIDEOS.name -> CourseContainerTab.VIDEOS
CourseContainerTab.DATES.name -> CourseContainerTab.DATES
CourseContainerTab.DISCUSSIONS.name -> CourseContainerTab.DISCUSSIONS
CourseContainerTab.MORE.name -> CourseContainerTab.MORE
else -> CourseContainerTab.HOME
}
val requiredTabIndex = CourseContainerTab.entries.indexOf(
when (openTab.uppercase()) {
CourseContainerTab.HOME.name -> CourseContainerTab.HOME
CourseContainerTab.VIDEOS.name -> CourseContainerTab.VIDEOS
CourseContainerTab.DATES.name -> CourseContainerTab.DATES
CourseContainerTab.DISCUSSIONS.name -> CourseContainerTab.DISCUSSIONS
CourseContainerTab.MORE.name -> CourseContainerTab.MORE
else -> CourseContainerTab.HOME
}
)

val pagerState = rememberPagerState(
initialPage = CourseContainerTab.entries.indexOf(requiredTab),
initialPage = requiredTabIndex,
pageCount = { CourseContainerTab.entries.size }
)
val dataReady = viewModel.dataReady.observeAsState()
Expand All @@ -388,6 +390,7 @@ fun CourseDashboard(

LaunchedEffect(pagerState.currentPage) {
tabState.animateScrollToItem(pagerState.currentPage)
viewModel.courseContainerTabClickedEvent(pagerState.currentPage)
}

Column(
Expand Down Expand Up @@ -450,7 +453,7 @@ fun CourseDashboard(
rowState = tabState,
pagerState = pagerState,
withPager = true,
onTabClicked = viewModel::courseContainerTabClickedEvent
onTabClicked = { }
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,11 @@ class CourseContainerViewModel(
}
}
}.distinctUntilChanged().launchIn(viewModelScope)

courseDashboardViewed()
}

fun fetchCourseDetails(isIAPFlow: Boolean = false, isExpiredCoursePurchase: Boolean = false) {
if (isIAPFlow.not()) {
courseDashboardViewed()
}
_showProgress.value = true
viewModelScope.launch {
try {
Expand Down Expand Up @@ -673,7 +672,6 @@ class CourseContainerViewModel(

private fun courseDashboardViewed() {
logCourseContainerEvent(CourseAnalyticsEvent.DASHBOARD)
courseTabClickedEvent()
}

private fun courseTabClickedEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
Expand Down Expand Up @@ -233,6 +232,7 @@ class CourseContainerViewModelTest {
coEvery { interactor.getEnrollmentDetails(any()) } returns courseDetails
every { imageProcessor.loadImage(any(), any(), any()) } returns Unit
every { imageProcessor.applyBlur(any(), any()) } returns mockBitmap
every { courseAnalytics.logScreenEvent(any(), any()) } returns Unit
}

@After
Expand Down Expand Up @@ -279,18 +279,6 @@ class CourseContainerViewModelTest {
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getEnrollmentDetails(any()) }
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
}
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
}
assert(!viewModel.refreshing.value)
assert(viewModel.courseAccessStatus.value == CourseAccessError.UNKNOWN)
}
Expand Down Expand Up @@ -318,34 +306,11 @@ class CourseContainerViewModelTest {
)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getEnrollmentDetails(any()) } returns enrollmentDetails
every {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
} returns Unit
every {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
} returns Unit

viewModel.fetchCourseDetails()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getEnrollmentDetails(any()) }
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
}
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
}
assert(viewModel.errorMessage.value == null)
assert(!viewModel.refreshing.value)
assert(viewModel.courseAccessStatus.value != null)
Expand Down Expand Up @@ -389,18 +354,6 @@ class CourseContainerViewModelTest {
viewModel.fetchCourseDetails()
advanceUntilIdle()
coVerify(exactly = 0) { courseApi.getEnrollmentDetails(any()) }
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
}
verify(exactly = 1) {
courseAnalytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
}

assert(viewModel.errorMessage.value == null)
assert(!viewModel.refreshing.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,23 @@ class LearnViewModel(
val getProgramFragment get() = dashboardRouter.getProgramFragment()

init {
viewModelScope.launch {
_uiState.collect { uiState ->
if (uiState.learnType == LearnType.COURSES) {
logMyCoursesTabClickedEvent()
} else {
logMyProgramsTabClickedEvent()
}
}
}
viewModelScope.launch {
pushNotifier.notifier.collect { event ->
if (event is PushEvent.RefreshBadgeCount) {
checkNotificationCount()
}
}
}
logTabClickedEvent(_uiState.value.learnType)
checkNotificationCount()
}

fun updateLearnType(learnType: LearnType) {
viewModelScope.launch {
_uiState.update { it.copy(learnType = learnType) }
if (learnType != _uiState.value.learnType) {
_uiState.update { it.copy(learnType = learnType) }
logTabClickedEvent(learnType)
}
}
}

Expand All @@ -93,12 +88,11 @@ class LearnViewModel(
_uiState.update { it.copy(hasUnreadNotifications = false) }
}

private fun logMyCoursesTabClickedEvent() {
logScreenEvent(DashboardAnalyticsEvent.MY_COURSES)
}

private fun logMyProgramsTabClickedEvent() {
logScreenEvent(DashboardAnalyticsEvent.MY_PROGRAMS)
private fun logTabClickedEvent(learnType: LearnType) {
when (learnType) {
LearnType.COURSES -> logScreenEvent(DashboardAnalyticsEvent.MY_COURSES)
LearnType.PROGRAMS -> logScreenEvent(DashboardAnalyticsEvent.MY_PROGRAMS)
}
}

private fun logScreenEvent(event: DashboardAnalyticsEvent) {
Expand Down