-
Notifications
You must be signed in to change notification settings - Fork 330
fix(browser): capture SPA pageviews on query string and hash changes #4729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| 'posthog-js': minor | ||
| '@posthog/types': minor | ||
| --- | ||
|
|
||
| `capture_pageview: 'history_change'` now captures a `$pageview` when a single-page-app navigation changes the query string or the hash, not only the path. Before this change, navigation to search results, filters, pagination, and tab state captured no pageview, so these apps undercounted pageviews. `disable_capture_url_hashes` still suppresses hash-only changes. To keep the old behaviour, set `capture_pageview: { path: true }`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,7 +118,7 @@ export class HistoryAutocapture implements Extension { | |
| const capturePageview = this._instance.config.capture_pageview | ||
|
|
||
| if (capturePageview === 'history_change') { | ||
| return { path: true } | ||
| return { path: true, search: true, hash: true } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Startup query changes can send duplicate pageviewsWhy we think it's a valid issue
Issue descriptionLine 121 enables search and hash captures as soon as Suggested fixCoordinate history capture with the initial pageview state. Before the initial pageview fires, make a history capture satisfy it or suppress the history capture. Add an integration test that changes the query and hash during Prompt to fix with AI (copy-paste)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the pinned
|
||
| } | ||
|
|
||
| return isObject(capturePageview) ? capturePageview : {} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1368,7 +1368,8 @@ export interface PostHogConfig { | |
| * Can be: | ||
| * - `true`: Capture the initial pageview | ||
| * - `false`: Don't capture any pageviews | ||
| * - `'history_change'`: Capture the initial pageview and pageviews when the pathname changes | ||
| * - `'history_change'`: Capture the initial pageview and pageviews when the path, query string, or hash changes. | ||
| * `disable_capture_url_hashes` still suppresses hash-only changes. | ||
|
Comment on lines
+1371
to
+1372
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regenerate the public API referenceWhy we think it's a valid issue
Issue descriptionThe checked-in browser API reference still says that Suggested fixRun Prompt to fix with AI (copy-paste)
Comment on lines
+1371
to
+1372
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the JavaScript SDK config guideWhy we think it's a valid issue
Issue descriptionThe public JavaScript config guide still says that Suggested fixUpdate Prompt to fix with AI (copy-paste) |
||
| * - An object: Capture the initial pageview and pageviews when any selected URL component changes | ||
| * | ||
| * @default true (or `'history_change'` when `defaults` is `'2025-05-24'` or later) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
State when modern defaults disable hash capture
Why we think it's a valid issue
packages/browser/src/posthog-core.ts, the hash gate inpackages/browser/src/extensions/history-autocapture.ts, the doc comment the PR edits inpackages/types/src/posthog-config.ts, and the changeset text itself.packages/browser/src/posthog-core.ts:251setsdisable_capture_url_hashes: !!(defaults && defaults >= '2026-06-25'), and line 244 setscapture_pageview: 'history_change'fordefaults >= '2025-05-24'. A project on'2026-06-25'defaults therefore gets'history_change'and hash stripping at the same time.packages/browser/src/extensions/history-autocapture.ts:127-129returns false from_shouldCaptureHashChangesin that state, so_hasLocationChanged(line 139) ignores hash-only navigation andmonitorHistoryChanges(line 73) never adds the hashchange listener. The test atpackages/browser/src/__tests__/extensions/history-autocapture.test.ts:214-222confirms the silence.packages/types/src/posthog-config.ts:1371-1372) both namedisable_capture_url_hashes, but neither says the SDK turns it on from'2026-06-25'defaults. So the premise of the finding is correct: the note promises hash pageviews to a cohort that will not receive them.'2026-06-25'defaults opted into hash stripping through a change that its own release note called a breaking change for hash-routing apps. Such a project already collapses hash routes in$current_url, so no hash-only pageview is the consistent result, not a surprise. The added clause improves the note; its absence misleads few readers.consider. No code behavior is wrong, the escape hatch is already named in the same paragraph, and the fix is one clause of release-note text.Issue description
The release note names
disable_capture_url_hashes, but it does not state when the SDK enables that option automatically. Users withdefaults: '2026-06-25'or later still receive no hash-only pageviews.Suggested fix
State that modern defaults enable
disable_capture_url_hashes. Tell users to setdisable_capture_url_hashes: falseif they want hash-only navigation pageviews.Prompt to fix with AI (copy-paste)