Repository navigation
feat(analytics): capture $pageleave and send $pathname - #876
Conversation
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.
|
@coderabbitai review |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
❌ Action failedReview failed.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review. 📝 WalkthroughWalkthroughThe PostHog client now enables automatic Merge Risk: ⚪ Minimal · up to 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)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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_pageleavewasfalse, 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). Sincecapture_pageviewisfalsehere and pageviews are captured by hand, the option has to be an explicittrue.That alone would have fixed nothing quietly:
createBeforeSend's event switch only had branches for$pageview,cta activated, and$web_vitals. Any$pageleaveenvelope posthog-js emitted would fall through to the trailingreturn nulland get dropped with no error, no ingestion warning, nothing. This widens the existing$pageviewbranch to also handle$pageleave, mirroring the same$current_urlsynthesis fromPRODUCTION_ORIGIN + path, and adds$pathnameto both.Privacy
Nothing opens up.
cookieless_mode,person_profiles,persistence,disable_persistence,respect_dnt,save_referrer, andsave_campaign_paramsare all untouched.$pathnameis set to the already-canonicalizedpath, never the raw pathname, so it can't carry a route outside the known-route allowlist (/_othercatch-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:scriptsinapps/web: 68 passed (66 baseline + 2 new). New cases:$pageleaverebuilt from posthog-js's own envelope ($current_url,$raw_user_agent,$host,token,$cookieless_mode,$process_person_profile) onto the canonical contract, throughbefore_send.$pathnamealways equalspathfor both$pageviewand$pageleave, and an unlisted route still collapses to/_otherrather than leaking.Also updated the exact-options assertion in
analytics-client.test.mjsforcapture_pageleave: true.npm run type-checkand 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
$pathnameto$pageviewand$pageleaveevents.$pageleaveinbefore_send.$pathnameand$current_urlderivation to use canonicalized paths.capture_pageleavein PostHog options./_otherhandling for unlisted routes.Concerns
$pageleaveprocessing uses the same privacy and canonicalization utilities as$pageview.