Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOB-516 Add NavigatePage event #114

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import exchange.dydx.trading.core.DydxNavGraph
import exchange.dydx.trading.core.biometric.DydxBiometricPrompt
import exchange.dydx.trading.core.biometric.DydxBiometricView
import exchange.dydx.trading.feature.shared.PreferenceKeys
import exchange.dydx.trading.feature.shared.analytics.AnalyticsEvent
import exchange.dydx.utilities.utils.SharedPreferencesStore
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -61,6 +62,11 @@ class TradingActivity : FragmentActivity() {
CarteraSetup.run(this, viewModel.logger)
AnalyticsSetup.run(viewModel.compositeTracking, this, viewModel.logger)

viewModel.compositeTracking.log(
event = AnalyticsEvent.APP_START.rawValue,
data = null,
)

WindowCompat.setDecorFitsSystemWindows(window, false)

lifecycleScope.launch {
Expand Down
24 changes: 3 additions & 21 deletions v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import exchange.dydx.trading.common.AppConfig
import exchange.dydx.trading.common.navigation.DydxRouter
import exchange.dydx.trading.common.navigation.DydxRouter.Destination
import exchange.dydx.trading.common.navigation.MarketRoutes
import exchange.dydx.trading.feature.shared.analytics.RoutingAnalytics
import exchange.dydx.trading.integration.analytics.tracking.Tracking
import exchange.dydx.utilities.utils.Logging
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -35,6 +36,7 @@ class DydxRouterImpl @Inject constructor(
private val tracker: Tracking,
appConfig: AppConfig,
private val logger: Logging,
private val routingAnalytics: RoutingAnalytics,
) : DydxRouter {

private lateinit var navHostController: NavHostController
Expand Down Expand Up @@ -85,7 +87,7 @@ class DydxRouterImpl @Inject constructor(

val destinationRoute = destination.route
if (destinationRoute != null) {
trackRoute(destinationRoute, destination, arguments)
routingAnalytics.logRoute(destinationRoute, arguments)

if (tabRoutes.contains(destinationRoute)) {
routeQueue.clear()
Expand Down Expand Up @@ -220,24 +222,4 @@ class DydxRouterImpl @Inject constructor(
}
return route
}

private fun trackRoute(destinationRoute: String, destination: NavDestination, arguments: Bundle?) {
val trackingData: MutableMap<String, String> = mutableMapOf()
destination.arguments.keys.forEach { key ->
trackingData[key] = arguments?.getString(key) ?: ""
}
// Remove query parameters from the route and remove the last component if it's a dynamic route
var sanitizedRoute = destinationRoute.split("?").first()
val components = sanitizedRoute.split("/")
val lastComponent = components.last()
if (lastComponent.startsWith("{") && lastComponent.endsWith("}")) {
sanitizedRoute = components.dropLast(1).joinToString("_")
} else {
sanitizedRoute = components.joinToString("_")
}
tracker.log(
event = sanitizedRoute,
data = trackingData,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package exchange.dydx.trading.feature.shared.analytics

import android.os.Bundle
import exchange.dydx.trading.integration.analytics.tracking.Tracking
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need singleton here, this class doesn't hold any of it's own state

class RoutingAnalytics @Inject constructor(
private val tracker: Tracking,
) {
fun logRoute(
destinationRoute: String,
arguments: Bundle?
) {
var pathWithArguments = destinationRoute
if (pathWithArguments.startsWith("market/{marketId}")) {
// web does not have a /market/<MARKET> path
pathWithArguments = pathWithArguments.replace("market/{marketId}", "trade/{marketId}")
}
arguments?.keySet()?.forEach { key ->
pathWithArguments = pathWithArguments.replace("{$key}", arguments.getString(key) ?: "")
}
if (!pathWithArguments.startsWith("/")) {
// Android routes don't start with "/"
pathWithArguments = "/$pathWithArguments"
}

tracker.view(
screenName = pathWithArguments,
screenClass = "TradingActivity",
)

tracker.log(
event = AnalyticsEvent.NAVIGATE_PAGE.rawValue,
data = mapOf(
"path" to pathWithArguments,
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ class AmplitudeTracker(
val jsonMap = data?.jsonStringToMap()
amplitude.track(event, jsonMap)
}

override fun view(screenName: String, screenClass: String) {
// No op
Copy link
Contributor

@prashanDYDX prashanDYDX May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no-op here? it'd be nice to log these to amplitude as well. afaik we don't ingest Firebase events into Mode, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tracked on Amplitude with line 34 from RoutingAnalytics

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ class CompositeTracker @Inject constructor() : CompositeTracking {
override fun log(event: String, data: String?) {
trackers.forEach { it.log(event, data) }
}

override fun view(screenName: String, screenClass: String) {
trackers.forEach { it.view(screenName, screenClass) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ class FirebaseTracker(
}
}
}

override fun view(screenName: String, screenClass: String) {
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
param(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
param(FirebaseAnalytics.Param.SCREEN_CLASS, screenClass)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exchange.dydx.trading.integration.analytics.tracking

import exchange.dydx.abacus.protocols.TrackingProtocol
import exchange.dydx.abacus.utils.toJson
import exchange.dydx.trading.common.AsyncResult.Waiting.data

interface Tracking : TrackingProtocol {

Expand All @@ -14,6 +15,8 @@ interface Tracking : TrackingProtocol {
fun log(event: String, data: Map<String, Any?>) {
log(event, data.toJson())
}

fun view(screenName: String, screenClass: String)
}

interface CompositeTracking : Tracking {
Expand Down
Loading