Skip to content

Make the analytics shim's tripwire name the API that will replace it - #552

Merged
AaronBuxbaum merged 2 commits into
mainfrom
fix/vercel-request-context-readonly-slot
Aug 15, 2026
Merged

AaronBuxbaum merged 2 commits into
mainfrom
fix/vercel-request-context-readonly-slot

Conversation

@AaronBuxbaum

@AaronBuxbaum AaronBuxbaum commented Aug 15, 2026 •

Copy link
Copy Markdown
Owner

Follow-on to #550, prompted by the upstream PR actually being opened: vercel/analytics#208

The gap

analytics-request-context.test.ts carries a tripwire asserting the server SDK still gives a caller no way to supply the event's page URL — the condition that justifies the global shim existing at all:

expect(sdk).not.toMatch(/options\s*(\?\.)?\s*\.\s*url\b/);

That matches an options.url override. But the request went upstream as vercel/analytics#208, which adds a beforeSend hook mirroring the browser SDK's — source reading options?.beforeSend and event.url, matching neither that regex nor anything else the test looked for.

So the tripwire would have stayed green through exactly the API it was watching for. We'd have bumped the dependency past the release carrying beforeSend and kept the shim — an undocumented hook into a Vercel internal — outliving its own supported replacement, with nothing to say so. The test's own comment said "(or a beforeSend)"; the assertion never covered it.

The change

Assert on beforeSend by name as well, so the bump becomes a red test rather than something to remember. The current release has zero occurrences in that bundle, so it passes today and fires on the release that lands #208.

Also records the upstream PR on the follow-up entry, which until now only said to open one — and notes that beforeSend is a strictly better fit than the options.url override the entry originally proposed, since it can withhold an event entirely, which is what our trackEvent already does when it cannot redact.

Owned paths

  • src/lib/analytics-request-context.test.ts
  • docs/product/follow-ups/FU-20260814-vercel-analytics-url-override.md

No production code changes. Lint, typecheck, check:repo, check:follow-ups, and both analytics specs green (26/26).

🤖 Generated with Claude Code

Summary by Sourcery

Tighten the analytics server SDK tripwire so dependency bumps fail as soon as the upstream beforeSend hook that can replace our shim is introduced, and update the associated follow-up documentation to reference the landed upstream PR and its preferred API shape.

Documentation:

  • Update the follow-up document to record the upstream vercel/analytics pull request adding a beforeSend hook and clarify that this hook is the preferred replacement for the current shim.

Tests:

  • Extend the analytics request context tripwire test to assert that the server bundle does not include a beforeSend hook, ensuring the test fails once the upstream replacement API ships.

The contract test asserted the server SDK still offers no way for a caller
to supply the event's page URL, by matching an `options.url` override. The
request actually went upstream as vercel/analytics#208, which adds a
`beforeSend` hook mirroring the browser SDK's -- source reading
`options?.beforeSend` and `event.url`, matching neither that regex nor
anything else the test looked for.

So the tripwire would have stayed green through exactly the API it was
watching for, and the shim would have outlived its own replacement without
anyone being told. Assert on `beforeSend` by name as well, so bumping the
dependency past the release that carries it is a red test rather than
something to remember.

Records the upstream PR on the follow-up entry, which until now only said
to open one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

The PR tightens the analytics shim tripwire by explicitly checking for the new beforeSend server-side analytics hook in the bundled SDK, and updates the follow-up documentation to reference the upstream PR and clarify that beforeSend is the preferred replacement API over an options.url override.

Flow diagram for analytics shim tripwire reacting to beforeSend introduction

flowchart TD
  vercel_analytics_release["Vercel analytics releases server beforeSend hook"]
  dependency_bump["Project bumps @vercel/analytics dependency"]
  run_tests["Run analytics-request-context.test"]
  check_bundle["Check sdk bundle for options.url and beforeSend"]
  has_beforeSend{sdk contains beforeSend}
  test_fail["Test fails (tripwire triggered)"]
  migrate_shim["Migrate trackEvent to beforeSend and delete shim"]

  vercel_analytics_release --> dependency_bump --> run_tests --> check_bundle --> has_beforeSend
  has_beforeSend -->|yes| test_fail --> migrate_shim
  has_beforeSend -->|no| run_tests
Loading

File-Level Changes

Change Details Files
Strengthen the analytics shim tripwire test to fail as soon as the server SDK exposes the beforeSend hook, ensuring the shim is removed once the upstream API lands.
  • Keep the existing regex assertion that the SDK bundle does not expose an options.url override.
  • Add a new assertion that the SDK bundle source does not contain the string beforeSend, explicitly guarding against the exact API requested upstream.
  • Expand the test commentary to explain why beforeSend must be named explicitly and how the previous assertion would have stayed green through the new API.
src/lib/analytics-request-context.test.ts
Update the follow-up documentation to note that the upstream PR has been opened and to describe beforeSend as the preferred replacement API, while documenting that the test now acts as a tripwire tied to that hook.
  • Mark the follow-up action to open an upstream issue/PR as completed and link directly to feat(server): add beforeSend to server-side track vercel/analytics#208.
  • Describe the semantics of the new beforeSend hook and why it is a better fit than an options.url override, including its ability to drop events.
  • Clarify that the analytics-request-context test will fail as soon as a release carrying beforeSend is consumed, turning the follow-up into a red test rather than a manual reminder.
docs/product/follow-ups/FU-20260814-vercel-analytics-url-override.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've found 1 issue, and left some high level feedback:

  • The expect(sdk).not.toContain("beforeSend") assertion is quite broad and could fail on incidental occurrences (e.g., comments, metadata); consider tightening it to a regex that specifically targets options?.beforeSend in the server track implementation.
  • Given that this test is acting as a tripwire on the bundled SDK, it might be more robust to key off a structural signal (e.g., the presence of a beforeSend option in the exported API shape) rather than plain text matching, to reduce fragility against bundling/minification changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `expect(sdk).not.toContain("beforeSend")` assertion is quite broad and could fail on incidental occurrences (e.g., comments, metadata); consider tightening it to a regex that specifically targets `options?.beforeSend` in the server track implementation.
- Given that this test is acting as a tripwire on the bundled SDK, it might be more robust to key off a structural signal (e.g., the presence of a beforeSend option in the exported API shape) rather than plain text matching, to reduce fragility against bundling/minification changes.

## Individual Comments

### Comment 1
<location path="docs/product/follow-ups/FU-20260814-vercel-analytics-url-override.md" line_range="65-67" />
<code_context>
+   https://github.com/vercel/analytics/pull/208** — `feat(server): add \`beforeSend\` to server-side
+   \`track\``, opened by a human under their own account, as this entry required. It adds the
+   browser SDK's `beforeSend` contract to the server `track` as an option: the hook receives
+   `{ type: "event", url }` and returns the event or `null` to drop it, runs before the request body
+   is built, and is fail-closed (a throwing hook is caught rather than falling through to the
+   un-edited URL). That is the shape to adopt, and it is a strictly better fit than the
+   `options.url` override this entry originally proposed — it can withhold an event entirely, which
</code_context>
<issue_to_address>
**suggestion (typo):** Consider spelling "un-edited" as "unedited".

Here, "unedited" is normally written as one word. Updating "un-edited URL" to "unedited URL" would follow standard spelling.

```suggestion
   `{ type: "event", url }` and returns the event or `null` to drop it, runs before the request body
   is built, and is fail-closed (a throwing hook is caught rather than falling through to the
   unedited URL). That is the shape to adopt, and it is a strictly better fit than the
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +65 to +67
`{ type: "event", url }` and returns the event or `null` to drop it, runs before the request body
is built, and is fail-closed (a throwing hook is caught rather than falling through to the
un-edited URL). That is the shape to adopt, and it is a strictly better fit than the

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.

suggestion (typo): Consider spelling "un-edited" as "unedited".

Here, "unedited" is normally written as one word. Updating "un-edited URL" to "unedited URL" would follow standard spelling.

Suggested change
`{ type: "event", url }` and returns the event or `null` to drop it, runs before the request body
is built, and is fail-closed (a throwing hook is caught rather than falling through to the
un-edited URL). That is the shape to adopt, and it is a strictly better fit than the
`{ type: "event", url }` and returns the event or `null` to drop it, runs before the request body
is built, and is fail-closed (a throwing hook is caught rather than falling through to the
unedited URL). That is the shape to adopt, and it is a strictly better fit than the

The neighbouring assertion pinned the page URL to the `o:` property it was
written inline against:

  /o:\s*\(?\s*requestContext[\s\S]{0,60}?\.url/

vercel/analytics#208 hoists that same expression into a `pageUrl` local so a
`beforeSend` hook can edit it before the body is built, leaving `o: pageUrl`.
The SDK reads `requestContext.url` exactly as before and the shim keeps
working -- but pinned to the old shape this test goes red on that release
claiming the redaction contract broke. False, and the opposite of the actual
news.

Match the derivation instead. The arrival of a supported API is the
`beforeSend` assertion's job to report, and only its job, so the two
conditions now fail independently and say what they mean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@reg-suit

reg-suit Bot commented Aug 15, 2026

Copy link
Copy Markdown

✨✨ That's perfect, there is no visual difference! ✨✨

Check out the report here.

@github-actions

Copy link
Copy Markdown
Contributor

Visual regression — no differences across 459 compared surface(s)

reg-suit compared 459 surface(s) against the baseline for this
commit's parent and found no differences. A baseline did resolve, so this is a real
comparison rather than an empty one.

Changed New (no baseline) Deleted (not captured) Passed Baseline images
0 0 0 459 459

Look at the pixels

  • Agents: pnpm visual:report --commit 7265ce959260332a6a8031608b44b43afbb1bf3c — writes .reg-report/7265ce9…/REPORT.md plus the expected/actual/diff PNGs, readable without a browser.
  • Humans: hosted reg-suit report — a client-rendered SPA, so it needs a browser.

This comment is informational and never fails the build. Accounting for every changed pixel
is the reviewer's job (AGENTS.md), and merging an explained change is what makes it the next
baseline.

Commit 7265ce9 · posted by the visual-report CI job, updated in place on each push. The reg commit status and the reg-suit[bot] comment come from reg-suit's own GitHub App and carry counts only; this comment adds the item names and checks that a baseline actually resolved.

@AaronBuxbaum
AaronBuxbaum merged commit 856d59e into main Aug 15, 2026
26 checks passed
@AaronBuxbaum
AaronBuxbaum deleted the fix/vercel-request-context-readonly-slot branch August 15, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant