Skip to content

feat(analytics): Send the previous route on page view events - #131762

Closed
priscilawebdev wants to merge 7 commits into
masterfrom
priscila/feat/route-analytics-previous-path
Closed

priscilawebdev wants to merge 7 commits into
masterfrom
priscila/feat/route-analytics-previous-path

Conversation

@priscilawebdev

@priscilawebdev priscilawebdev commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Page view events now include previous_parameterized_path, identifying the route the user came from in the same format as parameterized_path. This supports measuring entries into settings from other parts of the app. The existing previousUrl value becomes the current page URL after rerendering and cannot provide this attribution.

Redirects through the shared redirect helpers preserve the original referrer. For example, Issues → Account settings → Account details reports Issues as the previous route, rather than the intermediate settings redirect. Both redirect implementations share navigation handling, and AppRoot uses that helper too. Redirect navigation state carries redirectFromLocationKey, which analytics compares with the outgoing location key; ordinary replace navigations still record their source, regardless of timing.

The property is omitted on the first page of a page load, including loads through redirects. Query-string changes on the same route preserve the referrer. Existing page-view emission and url behavior are unchanged. Regression coverage includes redirect chains, direct loads, quick replaces, back navigation, and navigation carrying stale redirect state.

@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 19, 2026
Page view events carry `url`, which the code names `previousUrl`, but by
the time the event is sent it holds the page being viewed. There is no
way to tell which page a user came from.

Add `previous_parameterized_path` with the route the user navigated from,
in the same format as `parameterized_path`. It is omitted on the first
page of a page load. `url` is unchanged so existing charts keep working.

This lets settings-v3 analytics measure entries into each settings area
from outside settings.
Name each route, comment each navigation step, assert every page view in
one list, and drop act() around rerender, which already wraps itself.
@priscilawebdev
priscilawebdev marked this pull request as ready for review November 19, 2026 10:04
@priscilawebdev
priscilawebdev requested a review from a team as a code owner November 19, 2026 10:04

@Jesse-Box Jesse-Box left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this. Having the route the user came from will be really useful for the settings entry analysis.

I tested it against the real router (createMemoryRouter + translateSentryRoute, mounted under RouteAnalyticsContextProvider). Two cases produce a wrong previous_parameterized_path:

  1. Redirect routes become the previous route. This affects the main ways into settings, like User settings and Usage & Billing, so those entries look like moves within settings.
  2. A page view can carry the referrer from one page earlier. This needs analytics to be turned off for more than 7s and then a navigation, so it's rare.

Details are inline. These checks all gave the right value: fast navigation through several pages, a late organization, setEventNames, routes with analytics turned off, and query-string-only changes. Removing the oxlint suppression is correct as well: the new spec gives no require-fake-timer-cleanup warnings.

}

// On mount prevRoute equals currentRoute, so the first page keeps no referrer.
if (prevRoute !== currentRoute) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Redirect routes become the previous route

redirectTo routes render as <Redirect> → <Navigate replace> (translateSentryRoute.tsx:69-70). They're matched for one commit before navigating, so currentRoute changes twice, and this line stores the redirect route as the referrer. The page the user actually lands on then reports the redirect, not the page they left.

What I saw with the real route shapes:

Flow previous_parameterized_path sent Expected
Issues → User settings (/settings/account/ → details/) settings.account issues
Issues → Usage & Billing (/settings/billing/ → overview/) settings.billing issues
Issues → team settings (teams/:teamId/ → members/) settings.:org_id.teams.:team_id organizations.:org_id.issues
Direct load of /settings/account/ settings.account omitted (first page)

withDomainRedirect pages do the same thing. For example, Snapshot page → Snapshot Settings reports the pre-redirect settings route.

There are about 50 redirectTo routes under /settings/, including index redirects into details/, overview/ and members/. So for the settings-entry metric, a lot of entries from outside settings would look like moves inside settings.

One option is to update the referrer only after the route has settled, e.g. a route that was replaced before it emitted a page view doesn't count as a referrer. Happy to pair on it.

@priscilawebdev priscilawebdev Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, great catch. Fixed in c537db5.

translateSentryRoute now marks redirectTo routes with REDIRECT_HANDLE, and the hook doesn't use a marked route as the previous route. Issues → /settings/account/ now reports organizations.:org_id.issues, and a direct load of /settings/account/ leaves the field out.

This covers the redirectTo routes, which include the settings index redirects. withDomainRedirect and AppRoot redirect from inside their components, so they aren't marked yet. I can add that separately if it matters for the data.

Redirect routes still send their own page view, as they do on master. This PR only changes previous_parameterized_path.

url: previousUrl, // pass in the previous URL
// pass in the parameterized path as well
parameterized_path: reloadPath,
...(referrerRoute === undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Empty string gets past the undefined check

Top-level redirect routes (e.g. /account/ → /settings/account/details/) have no handle, so getEventPath returns '' for them. That passes the referrerRoute === undefined check, and the event goes out with previous_parameterized_path: "":

  • Issues → /account/: the details page reports "" instead of organizations.:org_id.issues.
  • Direct load of /account/: the first page view has "". The field should be left out.
  • Direct load of /: the AppRoot redirect to issues does the same thing.

Even without a full fix for redirects, checking !referrerRoute here (or skipping '' when setting it) would stop the empty values.

@priscilawebdev priscilawebdev Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in c537db5. The event now checks referrerRoute for truthiness, so "" is never sent. Top-level redirectTo routes like /account/ are also no longer used as the previous route (see the redirect thread).


// On mount prevRoute equals currentRoute, so the first page keeps no referrer.
if (prevRoute !== currentRoute) {
setReferrerRoute(prevRoute);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A page view can be sent with the referrer from one page earlier (rare)

This update is applied one render after the route changes. If a page has analytics turned off for more than 7s, the timer sets readyToSend = true but nothing is sent. When the page then turns analytics back on and navigates in the same commit, the send effect (line 115) runs in the render where currentRoute has already changed, before this setReferrerRoute(prevRoute) has applied. The destination page view goes out with the old referrer, and the page that was left never gets its own page view.

I reproduced it with the groupDetails.tsx pattern: useDisableRouteAnalytics(!group || !event || !project), plus the reprocessing navigate(..., {replace: true}) in an effect, with a load that takes more than 7s:

  • Expected: "previous_parameterized_path": "organizations.:org_id.issues.:group_id"
  • Received: "previous_parameterized_path": "organizations.:org_id.issues"

The wrong url and eventKey on that event already happen without this PR. The new field just inherits the same bug. It's a rare case, so I wouldn't block on it, but it's worth knowing about.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the detailed repro. I am leaving this one as is. The settings-entry charts do not depend on this case, and as you said, url and eventKey on that event already have the same bug without this PR. It is better fixed for all three fields together in a separate change.

expect(rawTrackAnalyticsEvent).toHaveBeenCalledTimes(2);
});

it('sends the route the user navigated from as previous_parameterized_path', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The hook-level tests pass a hand-built matches array, so they can't catch the redirect case. Could we add one test that mounts a real router? Something like:

const tree: SentryRouteObject = {
  path: '/',
  component: Root, // calls useRouteActivatedHook({location: useLocation(), matches: useMatches()})
  children: [
    {path: '/organizations/:orgId/issues/', component: Page},
    {
      path: '/settings/account/',
      component: Outlet,
      children: [
        {index: true, redirectTo: 'details/'},
        {path: 'details/', component: Page},
      ],
    },
  ],
};
const router = createMemoryRouter([translateSentryRoute(tree)], {initialEntries: [issuesUrl]});
// render <RouterProvider useTransitions={false} router={router} />, navigate to /settings/account/,
// then expect the settings.account.details page view to have
// previous_parameterized_path === 'organizations.:org_id.issues'

@priscilawebdev priscilawebdev Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in c537db5. The new with a real router tests mount translateSentryRoute routes through the test router and cover:

  • Issues → /settings/account/ (index redirectTo)
  • Issues → /account/ (top-level redirectTo)
  • direct loads of both

Without the fix, three of them fail. The direct load of /account/ is covered by the empty-string check.

A route that replaces itself within a second of mounting is a redirect
(`redirectTo` routes, `withDomainRedirect`, `AppRoot`). It no longer
sends its own page view, and it is not used as the previous route, so
the page after it reports the page the user actually came from.

Also leave `previous_parameterized_path` out when the previous route
has no path, instead of sending an empty string.

Adds tests that mount a real router with `translateSentryRoute`.
@priscilawebdev

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Replace the timing check from the previous commit with an explicit
marker. `translateSentryRoute` now sets `REDIRECT_HANDLE` on `redirectTo`
routes, and the page view hook skips those routes when it sets the
previous route.

Redirect routes send their own page view again, as they do on master,
so this change only affects `previous_parameterized_path`.
@github-actions

Copy link
Copy Markdown
Contributor

📊 Type Coverage Diff

Metric Before After Delta
Coverage 96.08% 96.08% ±0%
Typed 144,207 144,212 🟢 +5
Untyped 5,878 5,879 🔴 +1
🔍 1 new type safety issue introduced

Type assertions (as) (1 new)

File Line Detail
static/gsApp/overrides/useRouteActivatedHook.tsx 22 `as Record<string, unknown>

This is informational only and does not block the PR.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c537db5. Configure here.

function isRedirect(matches: Props['matches']) {
const handle = matches.at(-1)?.handle as Record<string, unknown> | undefined;
return handle?.[REDIRECT_HANDLE] === true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Component redirects stored as previous route

Medium Severity

isRedirect only looks for REDIRECT_HANDLE on redirectTo routes. Component redirects such as withDomainRedirect and AppRoot still call setReferrerRoute, so the landing page reports the bounce URL as previous_parameterized_path instead of the page the user came from, and a direct load incorrectly includes the field.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c537db5. Configure here.

withDomainRedirect and AppRoot redirect from inside their components, so the
REDIRECT_HANDLE marker on redirectTo routes missed them and the landing page
reported the redirect as previous_parameterized_path.

Every redirect navigates with replace, so treat a route that is replaced before
its page view was due as a redirect instead. This covers redirectTo routes too,
so the marker is removed.
Remove timing-based redirect detection so previous_parameterized_path consistently records the previous router route. Cover quick replace and back navigations with real-router tests.
Share redirect navigation handling and identify the outgoing location in navigation state.
Preserve the original route through redirects without treating ordinary replaces as redirects.
Route AppRoot through the shared helper and cover redirect chains, direct loads, and stale state.

This branch was successfully deployed

1 active deployment
Preview — 46426de0 Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants