record

fun record(name: String): String?

Records name as the most recently viewed screen, returning the screen it replaces — i.e. exactly the previous_screen of the view being recorded, or null if this is the first.

Returning the displaced value rather than Unit makes the one correct usage the shortest one. Every emit site needs the same three steps — read the previous screen, emit with it, record the new one — and doing them in the wrong order silently attributes a screen view to itself as its own previous_screen. That is bad data with no error to notice, so the API hands the caller the right value instead of trusting the ordering:

val previous = history.record(name)
tracker.screen(name, withPreviousScreen(properties, previous))