BACK
Use casesRouter analytics — page transitions
Use cases · 21

Router analytics — page transitions

Pattern

Analytics needs to know which pages the user actually views, and in what order. Not "navigation attempts" — successfully completed navigations.

The problem it solves

Log NavigationStart and your analytics fills with "ghosts" — a guard cancelled, a redirect diverted, an error interrupted. Metrics are inflated and the funnel is broken. You need to listen only to the finish, and form (from → to) pairs.

Operators and why they matter

  • filter — keep only NavigationEnd. Discard Cancel and Error.
  • pairwise — gives you the pair [previous URL, next URL]. Perfect for a "transition" model.
  • map — assembles the analytics payload (from, to, timestamp, etc.).

Gotchas

  • pairwise BEFORE filter — a cancelled navigation gets into the pair and spoils the data. filter first, then pairwise.
  • The first page creates no transition (nothing to compare with a previous one). If you need an initial referrer, add startWith.
  • Don't block navigation waiting for analytics to send. Analytics is a fire-and-forget side effect.

What you get

Analytics gets a clean stream of real page-to-page transitions, without noise from cancelled attempts.

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...