Skip to content

Commit

Permalink
Analytics: Add journey and trip tracking events (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz authored Dec 19, 2024
1 parent 6f91346 commit 39e121f
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent

interface Analytics {

fun track(event: AnalyticsEvent, properties: Map<String, Any>? = null)
fun track(event: AnalyticsEvent)

fun setUserId(userId: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent

class RealAnalytics(private val firebaseAnalytics: FirebaseAnalytics) : Analytics {

override fun track(event: AnalyticsEvent, properties: Map<String, Any>?) {
firebaseAnalytics.logEvent(event.name, properties)
override fun track(event: AnalyticsEvent) {
firebaseAnalytics.logEvent(event.name, event.properties)
}

override fun setUserId(userId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ sealed class AnalyticsEvent(val name: String, val properties: Map<String, Any>?

// endregion

// region PlanTripScreen / DateTimeSelection Screen

data object ResetTimeClickEvent : AnalyticsEvent("reset_time_click")

// endregion

// region TimeTable Screen

data class ReverseTimeTableClickEvent(val fromStopId: String, val toStopId: String) :
Expand All @@ -64,23 +70,30 @@ sealed class AnalyticsEvent(val name: String, val properties: Map<String, Any>?
properties = mapOf("fromStopId" to fromStopId, "toStopId" to toStopId),
)

data class DateTimeSelectEvent(val dayOfWeek: String, val time: String, val type: String) :
AnalyticsEvent(
name = "date_time_select",
properties = mapOf("dayOfWeek" to dayOfWeek, "time" to time, "type" to type),
)

data object ResetTimeClickEvent : AnalyticsEvent("reset_time_click")
data class DateTimeSelectEvent(
val dayOfWeek: String,
val time: String,
val journeyOption: String,
) : AnalyticsEvent(
name = "date_time_select",
properties = mapOf("dayOfWeek" to dayOfWeek, "time" to time, "option" to journeyOption),
)

data class JourneyCardExpandEvent(val hasStarted: Boolean) :
AnalyticsEvent(
name = "journey_card_expand",
properties = mapOf("hasStarted" to hasStarted),
)

data object JourneyCardCollapseEvent : AnalyticsEvent(name = "journey_card_collapse")
data class JourneyCardCollapseEvent(val hasStarted: Boolean) : AnalyticsEvent(
name = "journey_card_collapse",
properties = mapOf("hasStarted" to hasStarted),
)

data object JourneyLegClickEvent : AnalyticsEvent(name = "journey_leg_click")
data class JourneyLegClickEvent(val expanded: Boolean) : AnalyticsEvent(
name = "journey_leg_click",
properties = mapOf("expanded" to expanded),
)

data class JourneyAlertClickEvent(val fromStopId: String, val toStopId: String) :
AnalyticsEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package xyz.ksharma.krail.trip.planner.ui.state.timetable
import xyz.ksharma.krail.trip.planner.ui.state.datetimeselector.DateTimeSelectionItem

sealed interface TimeTableUiEvent {

data object SaveTripButtonClicked : TimeTableUiEvent

data class LoadTimeTable(val trip: Trip) : TimeTableUiEvent

data class JourneyCardClicked(val journeyId: String) : TimeTableUiEvent

data class DateTimeSelectionChanged(val dateTimeSelectionItem: DateTimeSelectionItem?) :
TimeTableUiEvent

data object ReverseTripButtonClicked : TimeTableUiEvent

data object RetryButtonClicked : TimeTableUiEvent

data object AnalyticsDateTimeSelectorClicked : TimeTableUiEvent

data class JourneyLegClicked(val expanded: Boolean) : TimeTableUiEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fun JourneyCard(
totalUniqueServiceAlerts: Int,
modifier: Modifier = Modifier,
onAlertClick: () -> Unit = {},
onLegClick: (Boolean) -> Unit = {},
) {
val onSurface: Color = KrailTheme.colors.onSurface
val borderColors = remember(transportModeList) { transportModeList.toColors(onSurface) }
Expand Down Expand Up @@ -173,6 +174,7 @@ fun JourneyCard(
legList = legList,
totalUniqueServiceAlerts = totalUniqueServiceAlerts,
onAlertClick = onAlertClick,
onLegClick = onLegClick,
modifier = Modifier.clickable(
role = Role.Button,
onClick = onClick,
Expand All @@ -196,6 +198,7 @@ fun ExpandedJourneyCardContent(
legList: ImmutableList<TimeTableState.JourneyCardInfo.Leg>,
totalUniqueServiceAlerts: Int,
onAlertClick: () -> Unit,
onLegClick: (Boolean) -> Unit,
modifier: Modifier = Modifier,
) {
val appPlatformType: AppPlatformType = LocalAppPlatformProvider.current
Expand Down Expand Up @@ -319,6 +322,7 @@ fun ExpandedJourneyCardContent(
lastLeg = legList[(index - 1).coerceAtLeast(0)]
) else 0.dp
),
onClick = onLegClick,
)
}

Expand Down Expand Up @@ -535,6 +539,7 @@ private fun PreviewJourneyCard() {
totalWalkTime = null,
totalUniqueServiceAlerts = 0,
onClick = {},
onLegClick = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fun LegView(
displayDuration: Boolean,
modifier: Modifier = Modifier,
displayAllStops: Boolean = false,
onClick: (Boolean) -> Unit = {},
) {
val circleRadius = 8.dp
val strokeWidth = 4.dp
Expand All @@ -83,7 +84,10 @@ fun LegView(
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
onClick = { showIntermediateStops = !showIntermediateStops },
onClick = {
showIntermediateStops = !showIntermediateStops
onClick(showIntermediateStops)
},
role = Role.Button,
)
.padding(vertical = 12.dp, horizontal = 12.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,23 @@ internal fun NavGraphBuilder.savedTripsDestination(navController: NavHostControl
// Timber.e("Select both stops")
}
},
onSearchButtonClick = { fromStop, toStop ->
if (fromStop != null && toStop != null) {
onSearchButtonClick = {
if (fromStopItem != null && toStopItem != null) {
val fromStopId = fromStopItem!!.stopId
val toStopId = toStopItem!!.stopId

viewModel.onEvent(
SavedTripUiEvent.AnalyticsLoadTimeTableClick(
fromStopId = fromStop.stopId,
toStopId = toStop.stopId,
fromStopId = fromStopId,
toStopId = toStopId,
)
)
navController.navigate(
route = TimeTableRoute(
fromStopId = fromStop.stopId,
fromStopName = fromStop.stopName,
toStopId = toStop.stopId,
toStopName = toStop.stopName,
fromStopId = fromStopId,
fromStopName = fromStopItem!!.stopName,
toStopId = toStopId,
toStopName = toStopItem!!.stopName,
),
navOptions = NavOptions.Builder().setLaunchSingleTop(true).build(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun SavedTripsScreen(
toButtonClick: () -> Unit = {},
onReverseButtonClick: () -> Unit = {},
onSavedTripCardClick: (StopItem?, StopItem?) -> Unit = { _, _ -> },
onSearchButtonClick: (StopItem?, StopItem?) -> Unit = { _, _ -> },
onSearchButtonClick: () -> Unit = {},
onSettingsButtonClick: () -> Unit = {},
onEvent: (SavedTripUiEvent) -> Unit = {},
) {
Expand Down Expand Up @@ -126,16 +126,6 @@ fun SavedTripsScreen(
stopName = trip.toStopName,
),
)
onSearchButtonClick(
StopItem(
stopId = trip.fromStopId,
stopName = trip.fromStopName,
),
StopItem(
stopId = trip.toStopId,
stopName = trip.toStopName,
),
)
},
primaryTransportMode = null, // TODO
modifier = Modifier
Expand All @@ -155,7 +145,7 @@ fun SavedTripsScreen(
fromButtonClick = fromButtonClick,
toButtonClick = toButtonClick,
onReverseButtonClick = onReverseButtonClick,
onSearchButtonClick = { onSearchButtonClick(null, null) },
onSearchButtonClick = { onSearchButtonClick() },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xyz.ksharma.krail.trip.planner.ui.timetable

import xyz.ksharma.krail.core.analytics.Analytics
import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent

internal fun Analytics.trackJourneyCardExpandEvent(hasStarted: Boolean) {
track(AnalyticsEvent.JourneyCardExpandEvent(hasStarted = hasStarted))
}

internal fun Analytics.trackJourneyCardCollapseEvent(hasStarted: Boolean) {
track(AnalyticsEvent.JourneyCardCollapseEvent(hasStarted = hasStarted))
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ internal fun NavGraphBuilder.timeTableDestination(navController: NavHostControll
},
dateTimeSelectionItem = dateTimeSelectionItem,
dateTimeSelectorClicked = {
viewModel.onEvent(TimeTableUiEvent.AnalyticsDateTimeSelectorClicked)
navController.navigate(
route = DateTimeSelectorRoute(dateTimeSelectionItem?.toJsonString()),
navOptions = NavOptions.Builder().setLaunchSingleTop(singleTop = true).build(),
)
},
onJourneyLegClick = { journeyId ->
viewModel.onEvent(TimeTableUiEvent.JourneyLegClicked(journeyId))
},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package xyz.ksharma.krail.trip.planner.ui.timetable
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -75,6 +73,7 @@ fun TimeTableScreen(
onEvent: (TimeTableUiEvent) -> Unit,
onAlertClick: (String) -> Unit,
onBackClick: () -> Unit,
onJourneyLegClick: (Boolean) -> Unit,
modifier: Modifier = Modifier,
dateTimeSelectorClicked: () -> Unit = {},
) {
Expand Down Expand Up @@ -243,6 +242,7 @@ fun TimeTableScreen(
onAlertClick = {
onAlertClick(journey.journeyId)
},
onLegClick = onJourneyLegClick,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp)
.animateItem(),
)
Expand Down Expand Up @@ -282,6 +282,7 @@ private fun JourneyCardItem(
totalUniqueServiceAlerts: Int,
modifier: Modifier = Modifier,
transportModeLineList: ImmutableList<TransportModeLine>? = null,
onLegClick: (Boolean) -> Unit,
) {
if (!transportModeLineList.isNullOrEmpty() && legList.isNotEmpty()) {
JourneyCard(
Expand All @@ -300,6 +301,7 @@ private fun JourneyCardItem(
onClick = onClick,
onAlertClick = onAlertClick,
totalUniqueServiceAlerts = totalUniqueServiceAlerts,
onLegClick = onLegClick,
modifier = modifier,
)
}
Expand Down Expand Up @@ -364,6 +366,7 @@ private fun PreviewTimeTableScreen() {
onAlertClick = {},
onBackClick = {},
dateTimeSelectionItem = null,
onJourneyLegClick = {},
)
}
}
Expand All @@ -390,6 +393,7 @@ private fun PreviewTimeTableScreenError() {
onAlertClick = {},
onBackClick = {},
dateTimeSelectionItem = null,
onJourneyLegClick = {},
)
}
}
Expand All @@ -416,6 +420,7 @@ private fun PreviewTimeTableScreenNoResults() {
onEvent = {},
onAlertClick = {},
onBackClick = {},
onJourneyLegClick = {},
)
}
}
Expand Down
Loading

0 comments on commit 39e121f

Please sign in to comment.