Skip to content

feat(analytics): capture $pageleave and send $pathname - #876

Merged
scttbnsn merged 1 commit into
dev/v1.7from
feat/analytics-pageleave-pathname
Aug 26, 2026
Merged

scttbnsn merged 1 commit into
dev/v1.7from
feat/analytics-pageleave-pathname

Conversation

@scttbnsn

@scttbnsn scttbnsn commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Two properties the analytics contract never sent, both costing real data and neither changing the privacy posture. Same change as portwing #214, adapted to drydock's contract shape.

What's wrong today

capture_pageleave was false, so a session's last recorded timestamp is its last pageview. A five minute read of one page scores as zero seconds, and PostHog's Web analytics Page, Entry page, and Exit page tables return zero rows: they key off $pathname, which the contract never sent.

Why flipping the option alone doesn't work

posthog-js gates the event on capture_pageleave === true || ('if_capture_pageview' && capture_pageview). Since capture_pageview is false here and pageviews are captured by hand, the option has to be an explicit true.

That alone would have fixed nothing quietly: createBeforeSend's event switch only had branches for $pageview, cta activated, and $web_vitals. Any $pageleave envelope posthog-js emitted would fall through to the trailing return null and get dropped with no error, no ingestion warning, nothing. This widens the existing $pageview branch to also handle $pageleave, mirroring the same $current_url synthesis from PRODUCTION_ORIGIN + path, and adds $pathname to both.

Privacy

Nothing opens up. cookieless_mode, person_profiles, persistence, disable_persistence, respect_dnt, save_referrer, and save_campaign_params are all untouched.

$pathname is set to the already-canonicalized path, never the raw pathname, so it can't carry a route outside the known-route allowlist (/_other catch-all) and adds no information the event wasn't already sending. A regression test asserts the two never diverge, including for an unlisted route.

Tests

npm run test:scripts in apps/web: 68 passed (66 baseline + 2 new). New cases:

  • $pageleave rebuilt from posthog-js's own envelope ($current_url, $raw_user_agent, $host, token, $cookieless_mode, $process_person_profile) onto the canonical contract, through before_send.
  • $pathname always equals path for both $pageview and $pageleave, and an unlisted route still collapses to /_other rather than leaking.

Also updated the exact-options assertion in analytics-client.test.mjs for capture_pageleave: true. npm run type-check and biome both clean. Full pre-push pipeline (knip, qlty, coverage, build) passed.

No CHANGELOG entry: this repo's feature PRs only touch it at release prep, not per PR (checked the last several feature commits into dev/v1.7).

Part of X16 in the ops execution plan. Same change already up for portwing (#214); careerrat, sockguard, and codeswhat.com still to come.

Changelog

  • ✨ Added $pathname to $pageview and $pageleave events.
  • ✨ Added regression tests for pageleave reconstruction and route canonicalization.
  • 🔧 Changed analytics processing to handle $pageleave in before_send.
  • 🔧 Changed $pathname and $current_url derivation to use canonicalized paths.
  • 🔧 Enabled explicit capture_pageleave in PostHog options.
  • 🔒 Preserved existing privacy options and /_other handling for unlisted routes.

Concerns

  • Verify that $pageleave processing uses the same privacy and canonicalization utilities as $pageview.
  • Confirm that enabling automatic pageleave capture does not create duplicate events for existing manual capture flows.

208 of 432 sessions across the five instrumented sites record zero
duration, because capture_pageleave was false and a session's last
recorded timestamp was its last pageview. PostHog's Web analytics
Page, Entry page, and Exit page tables also return zero rows: they
key off $pathname, which the contract never sent.

Flipping capture_pageleave alone would have fixed neither problem
silently wrong: before_send's event switch had no branch for
$pageleave, so posthog-js's own $pageleave envelope would fall
through to the default null and every event would keep dropping with
no error. This widens the existing $pageview branch to also handle
$pageleave, mirroring the same $current_url synthesis from
PRODUCTION_ORIGIN + path, and adds $pathname to both.

$pathname is bound to the already-canonicalized `path` value (the
one reduced to the known-route allowlist with /_other as the
catch-all), never the raw pathname, so it can't leak a route outside
the allowlist. No other option changes: cookieless_mode,
person_profiles, persistence, disable_persistence, respect_dnt,
save_referrer, and save_campaign_params are untouched.

Part of X16 in the ops execution plan.
@scttbnsn

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@vercel

vercel Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
drydock-website Ready Ready Preview Aug 26, 2026 4:17pm
drydockdemo-website Ready Ready Preview Aug 26, 2026 4:17pm

@coderabbitai

coderabbitai Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
❌ Action failed

Review failed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 78936079-1f24-4339-a2c6-4f16ea745f10

📥 Commits

Reviewing files that changed from the base of the PR and between ae77c06 and cd324f7.

📒 Files selected for processing (4)
  • apps/web/scripts/analytics-client.test.mjs
  • apps/web/scripts/analytics-contract.test.mjs
  • apps/web/src/lib/analytics-client.ts
  • apps/web/src/lib/analytics-contract.ts

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The PostHog client now enables automatic $pageleave capture. The analytics contract rebuilds both $pageview and $pageleave events with canonical $pathname values and production $current_url values. Tests cover canonical routes, unknown-route fallback behavior, required cookieless fields, and removal of unapproved properties.

Merge Risk: ⚪ Minimal · up to cd324

This localized analytics change adds page-leave capture and canonical path data without a supplied merge-blocking concern; it is merge-ready after normal checks and review.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/analytics-pageleave-pathname

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttbnsn
scttbnsn merged commit ad2f140 into dev/v1.7 Aug 26, 2026
32 checks passed
@scttbnsn
scttbnsn deleted the feat/analytics-pageleave-pathname branch August 26, 2026 17:47

This branch was successfully deployed

2 active deployments
Preview – drydock-website — cd324f79 Deployed Aug 26, 2026 by vercel[bot]
Preview – drydockdemo-website — cd324f79 Deployed Aug 26, 2026 by vercel[bot]
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.

3 participants