Skip to content

Commit 7b5bf2b

Browse files
fix: Analytics events improvements & Add missing ones (#409)
Co-authored-by: Hamza Israr <[email protected]>
1 parent b892560 commit 7b5bf2b

File tree

11 files changed

+152
-53
lines changed

11 files changed

+152
-53
lines changed

app/src/main/java/org/openedx/app/AppAnalytics.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ enum class AppAnalyticsEvent(val eventName: String, val biValue: String) {
1212
"Launch",
1313
"edx.bi.app.launch"
1414
),
15+
LEARN(
16+
"MainDashboard:Learn",
17+
"edx.bi.app.main_dashboard.learn"
18+
),
1519
DISCOVER(
1620
"MainDashboard:Discover",
1721
"edx.bi.app.main_dashboard.discover"

app/src/main/java/org/openedx/app/MainFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
4646
binding.bottomNavView.setOnItemSelectedListener {
4747
when (it.itemId) {
4848
R.id.fragmentLearn -> {
49+
viewModel.logLearnTabClickedEvent()
4950
binding.viewPager.setCurrentItem(0, false)
5051
}
5152

@@ -89,7 +90,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
8990
putString(ARG_INFO_TYPE, "")
9091
}
9192

92-
when (requireArguments().getString(ARG_OPEN_TAB, HomeTab.LEARN.name)) {
93+
when (requireArguments().getString(ARG_OPEN_TAB, "")) {
9394
HomeTab.LEARN.name,
9495
HomeTab.PROGRAMS.name -> {
9596
binding.bottomNavView.selectedItemId = R.id.fragmentLearn

app/src/main/java/org/openedx/app/MainViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class MainViewModel(
4949
_isBottomBarEnabled.value = enable
5050
}
5151

52+
fun logLearnTabClickedEvent() {
53+
logScreenEvent(AppAnalyticsEvent.LEARN)
54+
}
55+
5256
fun logDiscoveryTabClickedEvent() {
5357
logScreenEvent(AppAnalyticsEvent.DISCOVER)
5458
}

app/src/main/java/org/openedx/app/di/ScreenModule.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ val screenModule = module {
157157
)
158158
}
159159
viewModel { AllEnrolledCoursesViewModel(get(), get(), get(), get(), get(), get(), get()) }
160-
viewModel { LearnViewModel(get(), get(), get()) }
160+
viewModel { (openTab: String) ->
161+
LearnViewModel(openTab, get(), get(), get())
162+
}
161163

162164
factory { DiscoveryRepository(get(), get(), get()) }
163165
factory { DiscoveryInteractor(get()) }

course/src/main/java/org/openedx/course/presentation/container/CourseContainerViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ class CourseContainerViewModel(
322322

323323
private fun courseDashboardViewed() {
324324
logCourseContainerEvent(CourseAnalyticsEvent.DASHBOARD)
325+
courseTabClickedEvent()
325326
}
326327

327328
private fun courseTabClickedEvent() {

course/src/test/java/org/openedx/course/presentation/container/CourseContainerViewModelTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ class CourseContainerViewModelTest {
241241
any()
242242
)
243243
} returns Unit
244+
every {
245+
analytics.logScreenEvent(
246+
CourseAnalyticsEvent.HOME_TAB.eventName,
247+
any()
248+
)
249+
} returns Unit
244250
viewModel.fetchCourseDetails()
245251
advanceUntilIdle()
246252

@@ -251,6 +257,12 @@ class CourseContainerViewModelTest {
251257
any()
252258
)
253259
}
260+
verify(exactly = 1) {
261+
analytics.logScreenEvent(
262+
CourseAnalyticsEvent.HOME_TAB.eventName,
263+
any()
264+
)
265+
}
254266
assert(!viewModel.refreshing.value)
255267
assert(viewModel.courseAccessStatus.value == CourseAccessError.UNKNOWN)
256268
}
@@ -281,6 +293,12 @@ class CourseContainerViewModelTest {
281293
any()
282294
)
283295
} returns Unit
296+
every {
297+
analytics.logScreenEvent(
298+
CourseAnalyticsEvent.HOME_TAB.eventName,
299+
any()
300+
)
301+
} returns Unit
284302
viewModel.fetchCourseDetails()
285303
advanceUntilIdle()
286304

@@ -291,6 +309,12 @@ class CourseContainerViewModelTest {
291309
any()
292310
)
293311
}
312+
verify(exactly = 1) {
313+
analytics.logScreenEvent(
314+
CourseAnalyticsEvent.HOME_TAB.eventName,
315+
any()
316+
)
317+
}
294318
assert(viewModel.errorMessage.value == null)
295319
assert(!viewModel.refreshing.value)
296320
assert(viewModel.courseAccessStatus.value != null)
@@ -321,6 +345,12 @@ class CourseContainerViewModelTest {
321345
any()
322346
)
323347
} returns Unit
348+
every {
349+
analytics.logScreenEvent(
350+
CourseAnalyticsEvent.HOME_TAB.eventName,
351+
any()
352+
)
353+
} returns Unit
324354
viewModel.fetchCourseDetails()
325355
advanceUntilIdle()
326356
coVerify(exactly = 0) { courseApi.getEnrollmentDetails(any()) }
@@ -330,6 +360,12 @@ class CourseContainerViewModelTest {
330360
any()
331361
)
332362
}
363+
verify(exactly = 1) {
364+
analytics.logScreenEvent(
365+
CourseAnalyticsEvent.HOME_TAB.eventName,
366+
any()
367+
)
368+
}
333369

334370
assert(viewModel.errorMessage.value == null)
335371
assert(!viewModel.refreshing.value)

dashboard/src/main/java/org/openedx/dashboard/presentation/DashboardAnalytics.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ interface DashboardAnalytics {
77

88
enum class DashboardAnalyticsEvent(val eventName: String, val biValue: String) {
99
MY_COURSES(
10-
"MainDashboard:My Courses",
11-
"edx.bi.app.main_dashboard.my_course"
10+
"Learn:My Courses",
11+
"edx.bi.app.main_dashboard.learn.my_course"
1212
),
1313
MY_PROGRAMS(
14-
"MainDashboard:My Programs",
15-
"edx.bi.app.main_dashboard.my_program"
14+
"Learn:My Programs",
15+
"edx.bi.app.main_dashboard.learn.my_programs"
1616
),
1717
}
1818

dashboard/src/main/java/org/openedx/learn/presentation/LearnFragment.kt

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import androidx.compose.material.icons.Icons
2222
import androidx.compose.material.icons.filled.ExpandMore
2323
import androidx.compose.material.icons.filled.ManageAccounts
2424
import androidx.compose.runtime.Composable
25-
import androidx.compose.runtime.LaunchedEffect
25+
import androidx.compose.runtime.collectAsState
2626
import androidx.compose.runtime.getValue
2727
import androidx.compose.runtime.mutableStateOf
2828
import androidx.compose.runtime.remember
@@ -31,7 +31,6 @@ import androidx.compose.ui.Alignment
3131
import androidx.compose.ui.Modifier
3232
import androidx.compose.ui.draw.rotate
3333
import androidx.compose.ui.graphics.Color
34-
import androidx.compose.ui.platform.LocalContext
3534
import androidx.compose.ui.res.stringResource
3635
import androidx.compose.ui.tooling.preview.Preview
3736
import androidx.compose.ui.unit.Dp
@@ -42,6 +41,7 @@ import androidx.fragment.app.FragmentManager
4241
import androidx.viewpager2.widget.ViewPager2
4342
import org.koin.androidx.compose.koinViewModel
4443
import org.koin.androidx.viewmodel.ext.android.viewModel
44+
import org.koin.core.parameter.parametersOf
4545
import org.openedx.core.adapter.NavigationFragmentAdapter
4646
import org.openedx.core.presentation.global.viewBinding
4747
import org.openedx.core.ui.crop
@@ -60,24 +60,30 @@ import org.openedx.core.R as CoreR
6060
class LearnFragment : Fragment(R.layout.fragment_learn) {
6161

6262
private val binding by viewBinding(FragmentLearnBinding::bind)
63-
private val viewModel by viewModel<LearnViewModel>()
63+
private val viewModel by viewModel<LearnViewModel> {
64+
parametersOf(requireArguments().getString(ARG_OPEN_TAB, LearnTab.COURSES.name))
65+
}
6466
private lateinit var adapter: NavigationFragmentAdapter
6567

6668
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
6769
super.onViewCreated(view, savedInstanceState)
6870
initViewPager()
69-
val openTab = requireArguments().getString(ARG_OPEN_TAB, LearnTab.COURSES.name)
70-
val defaultLearnType = if (openTab == LearnTab.PROGRAMS.name) {
71-
LearnType.PROGRAMS
72-
} else {
73-
LearnType.COURSES
74-
}
7571
binding.header.setContent {
7672
OpenEdXTheme {
73+
val uiState by viewModel.uiState.collectAsState()
74+
binding.viewPager.setCurrentItem(
75+
when (uiState.learnType) {
76+
LearnType.COURSES -> 0
77+
LearnType.PROGRAMS -> 1
78+
},
79+
false
80+
)
7781
Header(
7882
fragmentManager = requireParentFragment().parentFragmentManager,
79-
defaultLearnType = defaultLearnType,
80-
viewPager = binding.viewPager
83+
selectedLearnType = uiState.learnType,
84+
onUpdateLearnType = { learnType ->
85+
viewModel.updateLearnType(learnType)
86+
},
8187
)
8288
}
8389
}
@@ -93,23 +99,12 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
9399
}
94100
binding.viewPager.adapter = adapter
95101
binding.viewPager.setUserInputEnabled(false)
96-
97-
binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
98-
override fun onPageSelected(position: Int) {
99-
super.onPageSelected(position)
100-
if (LearnType.COURSES.ordinal == position) {
101-
viewModel.logMyCoursesTabClickedEvent()
102-
} else {
103-
viewModel.logMyProgramsTabClickedEvent()
104-
}
105-
}
106-
})
107102
}
108103

109104
companion object {
110105
private const val ARG_OPEN_TAB = "open_tab"
111106
fun newInstance(
112-
openTab: String = LearnTab.COURSES.name
107+
openTab: String = LearnTab.COURSES.name,
113108
): LearnFragment {
114109
val fragment = LearnFragment()
115110
fragment.arguments = bundleOf(
@@ -123,8 +118,8 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
123118
@Composable
124119
private fun Header(
125120
fragmentManager: FragmentManager,
126-
defaultLearnType: LearnType,
127-
viewPager: ViewPager2,
121+
selectedLearnType: LearnType,
122+
onUpdateLearnType: (LearnType) -> Unit
128123
) {
129124
val viewModel: LearnViewModel = koinViewModel()
130125
val windowSize = rememberWindowSize()
@@ -156,8 +151,8 @@ private fun Header(
156151
modifier = Modifier
157152
.align(Alignment.Start)
158153
.padding(horizontal = 16.dp),
159-
defaultLearnType = defaultLearnType,
160-
viewPager = viewPager
154+
selectedLearnType = selectedLearnType,
155+
onUpdateLearnType = onUpdateLearnType
161156
)
162157
}
163158
}
@@ -200,26 +195,15 @@ private fun Title(
200195
@Composable
201196
private fun LearnDropdownMenu(
202197
modifier: Modifier = Modifier,
203-
defaultLearnType: LearnType,
204-
viewPager: ViewPager2,
198+
selectedLearnType: LearnType,
199+
onUpdateLearnType: (LearnType) -> Unit
205200
) {
206201
var expanded by remember { mutableStateOf(false) }
207-
var currentValue by remember { mutableStateOf(defaultLearnType) }
208202
val iconRotation by animateFloatAsState(
209203
targetValue = if (expanded) 180f else 0f,
210204
label = ""
211205
)
212206

213-
LaunchedEffect(currentValue) {
214-
viewPager.setCurrentItem(
215-
when (currentValue) {
216-
LearnType.COURSES -> 0
217-
LearnType.PROGRAMS -> 1
218-
},
219-
false
220-
)
221-
}
222-
223207
Column(
224208
modifier = modifier
225209
) {
@@ -231,7 +215,7 @@ private fun LearnDropdownMenu(
231215
verticalAlignment = Alignment.CenterVertically
232216
) {
233217
Text(
234-
text = stringResource(id = currentValue.title),
218+
text = stringResource(id = selectedLearnType.title),
235219
color = MaterialTheme.appColors.textDark,
236220
style = MaterialTheme.appTypography.titleSmall
237221
)
@@ -262,7 +246,7 @@ private fun LearnDropdownMenu(
262246
for (learnType in LearnType.entries) {
263247
val background: Color
264248
val textColor: Color
265-
if (currentValue == learnType) {
249+
if (selectedLearnType == learnType) {
266250
background = MaterialTheme.appColors.primary
267251
textColor = MaterialTheme.appColors.primaryButtonText
268252
} else {
@@ -273,7 +257,7 @@ private fun LearnDropdownMenu(
273257
modifier = Modifier
274258
.background(background),
275259
onClick = {
276-
currentValue = learnType
260+
onUpdateLearnType(learnType)
277261
expanded = false
278262
}
279263
) {
@@ -304,10 +288,9 @@ private fun HeaderPreview() {
304288
@Composable
305289
private fun LearnDropdownMenuPreview() {
306290
OpenEdXTheme {
307-
val context = LocalContext.current
308291
LearnDropdownMenu(
309-
defaultLearnType = LearnType.COURSES,
310-
viewPager = ViewPager2(context)
292+
selectedLearnType = LearnType.COURSES,
293+
onUpdateLearnType = {}
311294
)
312295
}
313296
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.openedx.learn.presentation
2+
3+
import org.openedx.learn.LearnType
4+
5+
data class LearnUIState(val learnType: LearnType)

0 commit comments

Comments
 (0)