Skip to content

Commit

Permalink
UI: Add mode selection analytics tracking (#614)
Browse files Browse the repository at this point in the history
### TL;DR
Added analytics tracking for mode selection events in trip planner

### What changed?
- Added new analytics events: `ModeClickEvent` and `ModeSelectionDoneEvent`
- Integrated mode selection tracking in TimeTableViewModel
- Added new UI event `ModeClicked` to handle mode selection visibility
- Connected mode click analytics to the UI through TimeTableScreen and TimeTableDestination

### How to test?
1. Open trip planner
2. Click on mode selection button
3. Verify analytics event is fired with correct fromStopId, toStopId, and displayModeSelectionRow state
4. Select/deselect different transport modes
5. Verify mode selection analytics event fires with correct unselected modes

### Why make this change?
To track user interaction with transport mode selection, providing insights into how users filter their journey planning options and which transport modes are commonly excluded from searches.
  • Loading branch information
ksharma-xyz authored Feb 13, 2025
1 parent 163f0cc commit e763f3a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ sealed class AnalyticsEvent(val name: String, val properties: Map<String, Any>?
properties = mapOf("fromStopId" to fromStopId, "toStopId" to toStopId),
)

data class ModeClickEvent(
val fromStopId: String,
val toStopId: String,
val displayModeSelectionRow: Boolean,
) :
AnalyticsEvent(
name = "mode_click",
properties = mapOf(
"fromStopId" to fromStopId,
"toStopId" to toStopId,
"displayModeSelectionRow" to displayModeSelectionRow,
),
)

data class ModeSelectionDoneEvent(
val fromStopId: String, val toStopId: String,
val unselectedProductClasses: Set<Int>,
) : AnalyticsEvent(
name = "mode_selection_done",
properties = mapOf(
"fromStopId" to fromStopId,
"toStopId" to toStopId,
"unselected" to unselectedProductClasses.toString(),
),
)

data class DateTimeSelectEvent(
val dayOfWeek: String,
val time: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ sealed interface TimeTableUiEvent {
data class JourneyLegClicked(val expanded: Boolean) : TimeTableUiEvent

data class ModeSelectionChanged(val unselectedModes: Set<Int>) : TimeTableUiEvent

/**
* when tru, the selection row is displayed else it is hidden.
*/
data class ModeClicked(val displayModeSelectionRow: Boolean) : TimeTableUiEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ internal fun NavGraphBuilder.timeTableDestination(navController: NavHostControll
onModeSelectionChanged = { unselectedModes ->
log("onModeSelectionChanged Exclude :$unselectedModes")
viewModel.onEvent(TimeTableUiEvent.ModeSelectionChanged(unselectedModes))
},
onModeClick = { displayModeSelectionRow ->
viewModel.onEvent(TimeTableUiEvent.ModeClicked(displayModeSelectionRow))
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fun TimeTableScreen(
modifier: Modifier = Modifier,
dateTimeSelectorClicked: () -> Unit = {},
onModeSelectionChanged: (Set<Int>) -> Unit = {},
onModeClick: (Boolean) -> Unit = {},
) {
val themeColorHex by LocalThemeColor.current
val themeColor = themeColorHex.hexToComposeColor()
Expand Down Expand Up @@ -189,6 +190,7 @@ fun TimeTableScreen(
SubtleButton(
onClick = {
displayModeSelectionRow = !displayModeSelectionRow
onModeClick(displayModeSelectionRow)
},
dimensions = ButtonDefaults.mediumButtonSize(),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -152,9 +151,21 @@ class TimeTableViewModel(
}

is TimeTableUiEvent.ModeSelectionChanged -> onModeSelectionChanged(event.unselectedModes)

is TimeTableUiEvent.ModeClicked -> trackOnModeClickEvent(event.displayModeSelectionRow)
}
}

private fun trackOnModeClickEvent(displayModeSelectionRow: Boolean) {
analytics.track(
AnalyticsEvent.ModeClickEvent(
fromStopId = tripInfo?.fromStopId ?: "NA",
toStopId = tripInfo?.toStopId ?: "NA",
displayModeSelectionRow = displayModeSelectionRow,
)
)
}

private fun onModeSelectionChanged(unselectedModes: Set<Int>) {
if (hasModeSelectionChanged(unselectedModes)) {
this.unselectedModes.clear()
Expand All @@ -163,6 +174,15 @@ class TimeTableViewModel(
// call api
rateLimiter.triggerEvent()
updateUiState { copy(isLoading = true) }

// analytics
analytics.track(
AnalyticsEvent.ModeSelectionDoneEvent(
fromStopId = tripInfo?.fromStopId ?: "NA",
toStopId = tripInfo?.toStopId ?: "NA",
unselectedProductClasses = this.unselectedModes,
)
)
} else {
// do nothing.
log("Mode selection not changed")
Expand Down

0 comments on commit e763f3a

Please sign in to comment.