Skip to content

Commit

Permalink
fix: Analytics events improvements & Add missing ones (openedx#409)
Browse files Browse the repository at this point in the history
Co-authored-by: Hamza Israr <[email protected]>
  • Loading branch information
dixidroid and HamzaIsrar12 authored Dec 2, 2024
1 parent b892560 commit 7b5bf2b
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 53 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/org/openedx/app/AppAnalytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ enum class AppAnalyticsEvent(val eventName: String, val biValue: String) {
"Launch",
"edx.bi.app.launch"
),
LEARN(
"MainDashboard:Learn",
"edx.bi.app.main_dashboard.learn"
),
DISCOVER(
"MainDashboard:Discover",
"edx.bi.app.main_dashboard.discover"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/openedx/app/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
binding.bottomNavView.setOnItemSelectedListener {
when (it.itemId) {
R.id.fragmentLearn -> {
viewModel.logLearnTabClickedEvent()
binding.viewPager.setCurrentItem(0, false)
}

Expand Down Expand Up @@ -89,7 +90,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
putString(ARG_INFO_TYPE, "")
}

when (requireArguments().getString(ARG_OPEN_TAB, HomeTab.LEARN.name)) {
when (requireArguments().getString(ARG_OPEN_TAB, "")) {
HomeTab.LEARN.name,
HomeTab.PROGRAMS.name -> {
binding.bottomNavView.selectedItemId = R.id.fragmentLearn
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/openedx/app/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class MainViewModel(
_isBottomBarEnabled.value = enable
}

fun logLearnTabClickedEvent() {
logScreenEvent(AppAnalyticsEvent.LEARN)
}

fun logDiscoveryTabClickedEvent() {
logScreenEvent(AppAnalyticsEvent.DISCOVER)
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ val screenModule = module {
)
}
viewModel { AllEnrolledCoursesViewModel(get(), get(), get(), get(), get(), get(), get()) }
viewModel { LearnViewModel(get(), get(), get()) }
viewModel { (openTab: String) ->
LearnViewModel(openTab, get(), get(), get())
}

factory { DiscoveryRepository(get(), get(), get()) }
factory { DiscoveryInteractor(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ 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 @@ -241,6 +241,12 @@ class CourseContainerViewModelTest {
any()
)
} returns Unit
every {
analytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
} returns Unit
viewModel.fetchCourseDetails()
advanceUntilIdle()

Expand All @@ -251,6 +257,12 @@ class CourseContainerViewModelTest {
any()
)
}
verify(exactly = 1) {
analytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
}
assert(!viewModel.refreshing.value)
assert(viewModel.courseAccessStatus.value == CourseAccessError.UNKNOWN)
}
Expand Down Expand Up @@ -281,6 +293,12 @@ class CourseContainerViewModelTest {
any()
)
} returns Unit
every {
analytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
} returns Unit
viewModel.fetchCourseDetails()
advanceUntilIdle()

Expand All @@ -291,6 +309,12 @@ class CourseContainerViewModelTest {
any()
)
}
verify(exactly = 1) {
analytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
}
assert(viewModel.errorMessage.value == null)
assert(!viewModel.refreshing.value)
assert(viewModel.courseAccessStatus.value != null)
Expand Down Expand Up @@ -321,6 +345,12 @@ class CourseContainerViewModelTest {
any()
)
} returns Unit
every {
analytics.logScreenEvent(
CourseAnalyticsEvent.HOME_TAB.eventName,
any()
)
} returns Unit
viewModel.fetchCourseDetails()
advanceUntilIdle()
coVerify(exactly = 0) { courseApi.getEnrollmentDetails(any()) }
Expand All @@ -330,6 +360,12 @@ class CourseContainerViewModelTest {
any()
)
}
verify(exactly = 1) {
analytics.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 @@ -7,12 +7,12 @@ interface DashboardAnalytics {

enum class DashboardAnalyticsEvent(val eventName: String, val biValue: String) {
MY_COURSES(
"MainDashboard:My Courses",
"edx.bi.app.main_dashboard.my_course"
"Learn:My Courses",
"edx.bi.app.main_dashboard.learn.my_course"
),
MY_PROGRAMS(
"MainDashboard:My Programs",
"edx.bi.app.main_dashboard.my_program"
"Learn:My Programs",
"edx.bi.app.main_dashboard.learn.my_programs"
),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.ManageAccounts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -31,7 +31,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
Expand All @@ -42,6 +41,7 @@ import androidx.fragment.app.FragmentManager
import androidx.viewpager2.widget.ViewPager2
import org.koin.androidx.compose.koinViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import org.openedx.core.adapter.NavigationFragmentAdapter
import org.openedx.core.presentation.global.viewBinding
import org.openedx.core.ui.crop
Expand All @@ -60,24 +60,30 @@ import org.openedx.core.R as CoreR
class LearnFragment : Fragment(R.layout.fragment_learn) {

private val binding by viewBinding(FragmentLearnBinding::bind)
private val viewModel by viewModel<LearnViewModel>()
private val viewModel by viewModel<LearnViewModel> {
parametersOf(requireArguments().getString(ARG_OPEN_TAB, LearnTab.COURSES.name))
}
private lateinit var adapter: NavigationFragmentAdapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViewPager()
val openTab = requireArguments().getString(ARG_OPEN_TAB, LearnTab.COURSES.name)
val defaultLearnType = if (openTab == LearnTab.PROGRAMS.name) {
LearnType.PROGRAMS
} else {
LearnType.COURSES
}
binding.header.setContent {
OpenEdXTheme {
val uiState by viewModel.uiState.collectAsState()
binding.viewPager.setCurrentItem(
when (uiState.learnType) {
LearnType.COURSES -> 0
LearnType.PROGRAMS -> 1
},
false
)
Header(
fragmentManager = requireParentFragment().parentFragmentManager,
defaultLearnType = defaultLearnType,
viewPager = binding.viewPager
selectedLearnType = uiState.learnType,
onUpdateLearnType = { learnType ->
viewModel.updateLearnType(learnType)
},
)
}
}
Expand All @@ -93,23 +99,12 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
}
binding.viewPager.adapter = adapter
binding.viewPager.setUserInputEnabled(false)

binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
if (LearnType.COURSES.ordinal == position) {
viewModel.logMyCoursesTabClickedEvent()
} else {
viewModel.logMyProgramsTabClickedEvent()
}
}
})
}

companion object {
private const val ARG_OPEN_TAB = "open_tab"
fun newInstance(
openTab: String = LearnTab.COURSES.name
openTab: String = LearnTab.COURSES.name,
): LearnFragment {
val fragment = LearnFragment()
fragment.arguments = bundleOf(
Expand All @@ -123,8 +118,8 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
@Composable
private fun Header(
fragmentManager: FragmentManager,
defaultLearnType: LearnType,
viewPager: ViewPager2,
selectedLearnType: LearnType,
onUpdateLearnType: (LearnType) -> Unit
) {
val viewModel: LearnViewModel = koinViewModel()
val windowSize = rememberWindowSize()
Expand Down Expand Up @@ -156,8 +151,8 @@ private fun Header(
modifier = Modifier
.align(Alignment.Start)
.padding(horizontal = 16.dp),
defaultLearnType = defaultLearnType,
viewPager = viewPager
selectedLearnType = selectedLearnType,
onUpdateLearnType = onUpdateLearnType
)
}
}
Expand Down Expand Up @@ -200,26 +195,15 @@ private fun Title(
@Composable
private fun LearnDropdownMenu(
modifier: Modifier = Modifier,
defaultLearnType: LearnType,
viewPager: ViewPager2,
selectedLearnType: LearnType,
onUpdateLearnType: (LearnType) -> Unit
) {
var expanded by remember { mutableStateOf(false) }
var currentValue by remember { mutableStateOf(defaultLearnType) }
val iconRotation by animateFloatAsState(
targetValue = if (expanded) 180f else 0f,
label = ""
)

LaunchedEffect(currentValue) {
viewPager.setCurrentItem(
when (currentValue) {
LearnType.COURSES -> 0
LearnType.PROGRAMS -> 1
},
false
)
}

Column(
modifier = modifier
) {
Expand All @@ -231,7 +215,7 @@ private fun LearnDropdownMenu(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(id = currentValue.title),
text = stringResource(id = selectedLearnType.title),
color = MaterialTheme.appColors.textDark,
style = MaterialTheme.appTypography.titleSmall
)
Expand Down Expand Up @@ -262,7 +246,7 @@ private fun LearnDropdownMenu(
for (learnType in LearnType.entries) {
val background: Color
val textColor: Color
if (currentValue == learnType) {
if (selectedLearnType == learnType) {
background = MaterialTheme.appColors.primary
textColor = MaterialTheme.appColors.primaryButtonText
} else {
Expand All @@ -273,7 +257,7 @@ private fun LearnDropdownMenu(
modifier = Modifier
.background(background),
onClick = {
currentValue = learnType
onUpdateLearnType(learnType)
expanded = false
}
) {
Expand Down Expand Up @@ -304,10 +288,9 @@ private fun HeaderPreview() {
@Composable
private fun LearnDropdownMenuPreview() {
OpenEdXTheme {
val context = LocalContext.current
LearnDropdownMenu(
defaultLearnType = LearnType.COURSES,
viewPager = ViewPager2(context)
selectedLearnType = LearnType.COURSES,
onUpdateLearnType = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.openedx.learn.presentation

import org.openedx.learn.LearnType

data class LearnUIState(val learnType: LearnType)
Loading

0 comments on commit 7b5bf2b

Please sign in to comment.