TrackScreenViews

fun NavController.TrackScreenViews(screenName: (NavDestination) -> String? = { it.route })

Automatically records a Screen Viewed event for every destination change of this NavController (androidx.navigation, a.k.a. navigation-compose).

val navController = rememberNavController()
navController.TrackScreenViews()
NavHost(navController, startDestination = "home") { ... }

The current destination's name is also declared as the screen, so autocaptured taps anywhere in the NavHost carry it — a frame in the ambient ScopeStack, the same channel TrackedScreen uses. A TrackedScreen inside a destination still wins: this frame is pushed once, when this composable enters, and the destination's is pushed after it, so the later one resolves as the screen (screen and section go by push order, not by nesting — the two frames are siblings). That makes the call order load-bearing: call this above the NavHost, as the example does.

A destination screenName maps to null is not tracked and declares no screen: taps on it carry none rather than the route the user came from. screenName is read afresh on every destination change, so replacing it takes effect from the next one — it is a mapping, not a switch to flip mid-destination.

Parameters

screenName

maps a destination to a screen name; return null to skip tracking that destination. Defaults to the destination route.