Skip to content

Commit afb5ff4

Browse files
HamzaIsrar12dixidroid
authored andcommitted
fix: Analytics events improvements & Add missing ones
1 parent 88dfc54 commit afb5ff4

File tree

10 files changed

+127
-53
lines changed

10 files changed

+127
-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
@@ -312,6 +312,7 @@ class CourseContainerViewModel(
312312

313313
private fun courseDashboardViewed() {
314314
logCourseContainerEvent(CourseAnalyticsEvent.DASHBOARD)
315+
courseTabClickedEvent()
315316
}
316317

317318
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
@@ -259,6 +259,12 @@ class CourseContainerViewModelTest {
259259
any()
260260
)
261261
} returns Unit
262+
every {
263+
analytics.logScreenEvent(
264+
CourseAnalyticsEvent.HOME_TAB.eventName,
265+
any()
266+
)
267+
} returns Unit
262268
viewModel.fetchCourseDetails()
263269
advanceUntilIdle()
264270

@@ -269,6 +275,12 @@ class CourseContainerViewModelTest {
269275
any()
270276
)
271277
}
278+
verify(exactly = 1) {
279+
analytics.logScreenEvent(
280+
CourseAnalyticsEvent.HOME_TAB.eventName,
281+
any()
282+
)
283+
}
272284
assert(!viewModel.refreshing.value)
273285
assert(viewModel.courseAccessStatus.value == CourseAccessError.UNKNOWN)
274286
}
@@ -299,6 +311,12 @@ class CourseContainerViewModelTest {
299311
any()
300312
)
301313
} returns Unit
314+
every {
315+
analytics.logScreenEvent(
316+
CourseAnalyticsEvent.HOME_TAB.eventName,
317+
any()
318+
)
319+
} returns Unit
302320
viewModel.fetchCourseDetails()
303321
advanceUntilIdle()
304322

@@ -309,6 +327,12 @@ class CourseContainerViewModelTest {
309327
any()
310328
)
311329
}
330+
verify(exactly = 1) {
331+
analytics.logScreenEvent(
332+
CourseAnalyticsEvent.HOME_TAB.eventName,
333+
any()
334+
)
335+
}
312336
assert(viewModel.errorMessage.value == null)
313337
assert(!viewModel.refreshing.value)
314338
assert(viewModel.courseAccessStatus.value != null)
@@ -339,6 +363,12 @@ class CourseContainerViewModelTest {
339363
any()
340364
)
341365
} returns Unit
366+
every {
367+
analytics.logScreenEvent(
368+
CourseAnalyticsEvent.HOME_TAB.eventName,
369+
any()
370+
)
371+
} returns Unit
342372
viewModel.fetchCourseDetails()
343373
advanceUntilIdle()
344374
coVerify(exactly = 0) { courseApi.getEnrollmentDetails(any()) }
@@ -348,6 +378,12 @@ class CourseContainerViewModelTest {
348378
any()
349379
)
350380
}
381+
verify(exactly = 1) {
382+
analytics.logScreenEvent(
383+
CourseAnalyticsEvent.HOME_TAB.eventName,
384+
any()
385+
)
386+
}
351387

352388
assert(viewModel.errorMessage.value == null)
353389
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: 28 additions & 45 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,29 @@ 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+
}, false
79+
)
7780
Header(
7881
fragmentManager = requireParentFragment().parentFragmentManager,
79-
defaultLearnType = defaultLearnType,
80-
viewPager = binding.viewPager
82+
selectedLearnType = uiState.learnType,
83+
onUpdateLearnType = { learnType ->
84+
viewModel.updateLearnType(learnType)
85+
},
8186
)
8287
}
8388
}
@@ -93,23 +98,12 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
9398
}
9499
binding.viewPager.adapter = adapter
95100
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-
})
107101
}
108102

109103
companion object {
110104
private const val ARG_OPEN_TAB = "open_tab"
111105
fun newInstance(
112-
openTab: String = LearnTab.COURSES.name
106+
openTab: String = LearnTab.COURSES.name,
113107
): LearnFragment {
114108
val fragment = LearnFragment()
115109
fragment.arguments = bundleOf(
@@ -123,8 +117,8 @@ class LearnFragment : Fragment(R.layout.fragment_learn) {
123117
@Composable
124118
private fun Header(
125119
fragmentManager: FragmentManager,
126-
defaultLearnType: LearnType,
127-
viewPager: ViewPager2,
120+
selectedLearnType: LearnType,
121+
onUpdateLearnType: (LearnType) -> Unit
128122
) {
129123
val viewModel: LearnViewModel = koinViewModel()
130124
val windowSize = rememberWindowSize()
@@ -156,8 +150,8 @@ private fun Header(
156150
modifier = Modifier
157151
.align(Alignment.Start)
158152
.padding(horizontal = 16.dp),
159-
defaultLearnType = defaultLearnType,
160-
viewPager = viewPager
153+
selectedLearnType = selectedLearnType,
154+
onUpdateLearnType = onUpdateLearnType
161155
)
162156
}
163157
}
@@ -200,25 +194,15 @@ private fun Title(
200194
@Composable
201195
private fun LearnDropdownMenu(
202196
modifier: Modifier = Modifier,
203-
defaultLearnType: LearnType,
204-
viewPager: ViewPager2,
197+
selectedLearnType: LearnType,
198+
onUpdateLearnType: (LearnType) -> Unit
205199
) {
206200
var expanded by remember { mutableStateOf(false) }
207-
var currentValue by remember { mutableStateOf(defaultLearnType) }
208201
val iconRotation by animateFloatAsState(
209202
targetValue = if (expanded) 180f else 0f,
210203
label = ""
211204
)
212205

213-
LaunchedEffect(currentValue) {
214-
viewPager.setCurrentItem(
215-
when (currentValue) {
216-
LearnType.COURSES -> 0
217-
LearnType.PROGRAMS -> 1
218-
}, false
219-
)
220-
}
221-
222206
Column(
223207
modifier = modifier
224208
) {
@@ -230,7 +214,7 @@ private fun LearnDropdownMenu(
230214
verticalAlignment = Alignment.CenterVertically
231215
) {
232216
Text(
233-
text = stringResource(id = currentValue.title),
217+
text = stringResource(id = selectedLearnType.title),
234218
color = MaterialTheme.appColors.textDark,
235219
style = MaterialTheme.appTypography.titleSmall
236220
)
@@ -261,7 +245,7 @@ private fun LearnDropdownMenu(
261245
for (learnType in LearnType.entries) {
262246
val background: Color
263247
val textColor: Color
264-
if (currentValue == learnType) {
248+
if (selectedLearnType == learnType) {
265249
background = MaterialTheme.appColors.primary
266250
textColor = MaterialTheme.appColors.primaryButtonText
267251
} else {
@@ -272,7 +256,7 @@ private fun LearnDropdownMenu(
272256
modifier = Modifier
273257
.background(background),
274258
onClick = {
275-
currentValue = learnType
259+
onUpdateLearnType(learnType)
276260
expanded = false
277261
}
278262
) {
@@ -303,10 +287,9 @@ private fun HeaderPreview() {
303287
@Composable
304288
private fun LearnDropdownMenuPreview() {
305289
OpenEdXTheme {
306-
val context = LocalContext.current
307290
LearnDropdownMenu(
308-
defaultLearnType = LearnType.COURSES,
309-
viewPager = ViewPager2(context)
291+
selectedLearnType = LearnType.COURSES,
292+
onUpdateLearnType = {}
310293
)
311294
}
312295
}
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)