Skip to content

Commit f1810ee

Browse files
committed
refactor: rely on capture_pageview config
1 parent e0d50ec commit f1810ee

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

src/__tests__/__snapshots__/config-snapshot.test.ts.snap

+2-5
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ exports[`config snapshot for PostHogConfig 1`] = `
5353
\\"false\\",
5454
\\"true\\"
5555
],
56-
\\"capture_history_events\\": [
57-
\\"false\\",
58-
\\"true\\"
59-
],
6056
\\"cross_subdomain_cookie\\": [
6157
\\"false\\",
6258
\\"true\\"
@@ -100,7 +96,8 @@ exports[`config snapshot for PostHogConfig 1`] = `
10096
],
10197
\\"capture_pageview\\": [
10298
\\"false\\",
103-
\\"true\\"
99+
\\"true\\",
100+
\\"\\\\\\"history_change\\\\\\"\\"
104101
],
105102
\\"capture_pageleave\\": [
106103
\\"false\\",

src/__tests__/extensions/history-autocapture.test.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('HistoryAutocapture', () => {
3535
posthog = {
3636
capture,
3737
config: {
38-
capture_history_events: true,
38+
capture_pageview: 'history_change',
3939
},
4040
pageViewManager: {
4141
doPageView: pageViewManagerDoPageView,
@@ -63,23 +63,29 @@ describe('HistoryAutocapture', () => {
6363
expect((window.history.replaceState as any).__posthog_wrapped__).toBe(true)
6464
})
6565

66-
it('should be enabled with true option', () => {
67-
posthog.config.capture_history_events = true
66+
it('should NOT be enabled with true option for backwards compatibility', () => {
67+
posthog.config.capture_pageview = true
6868
const historyAutocaptureEnabled = new HistoryAutocapture(posthog)
69-
expect(historyAutocaptureEnabled.isEnabled).toBe(true)
69+
expect(historyAutocaptureEnabled.isEnabled).toBe(false)
7070
})
7171

7272
it('should be disabled with false option', () => {
73-
posthog.config.capture_history_events = false
73+
posthog.config.capture_pageview = false
7474
const historyAutocaptureDisabled = new HistoryAutocapture(posthog)
7575
expect(historyAutocaptureDisabled.isEnabled).toBe(false)
7676
})
7777

78+
it('should be enabled with history_change option', () => {
79+
posthog.config.capture_pageview = 'history_change'
80+
const historyAutocaptureEnabled = new HistoryAutocapture(posthog)
81+
expect(historyAutocaptureEnabled.isEnabled).toBe(true)
82+
})
83+
7884
it('should not setup event listeners if feature is disabled', () => {
7985
window.history.pushState = originalPushState
8086
window.history.replaceState = originalReplaceState
8187

82-
posthog.config.capture_history_events = false
88+
posthog.config.capture_pageview = false
8389
const historyAutocaptureDisabled = new HistoryAutocapture(posthog)
8490
historyAutocaptureDisabled.startIfEnabled()
8591

@@ -121,9 +127,9 @@ describe('HistoryAutocapture', () => {
121127
expect(capture).not.toHaveBeenCalled()
122128
})
123129

124-
it('should not capture pageview when capture_history_events is disabled', () => {
130+
it('should not capture pageview when capture_pageview is disabled', () => {
125131
historyAutocapture.stop()
126-
posthog.config.capture_history_events = false
132+
posthog.config.capture_pageview = false
127133
historyAutocapture = new HistoryAutocapture(posthog)
128134
historyAutocapture.startIfEnabled()
129135

src/extensions/history-autocapture.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { patch } from './replay/rrweb-plugins/patch'
77
/**
88
* This class is used to capture pageview events when the user navigates using the history API (pushState, replaceState)
99
* and when the user navigates using the browser's back/forward buttons.
10+
*
11+
* The behavior is controlled by the `capture_pageview` configuration option:
12+
* - When set to `'history_change'`, this class will capture pageviews on history API changes
1013
*/
1114
export class HistoryAutocapture {
1215
private _instance: PostHog
@@ -19,7 +22,7 @@ export class HistoryAutocapture {
1922
}
2023

2124
public get isEnabled(): boolean {
22-
return !!this._instance.config.capture_history_events
25+
return this._instance.config.capture_pageview === 'history_change'
2326
}
2427

2528
public startIfEnabled(): void {

src/posthog-core.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ export const defaultConfig = (): PostHogConfig => ({
145145
custom_campaign_params: [],
146146
custom_blocked_useragents: [],
147147
save_referrer: true,
148-
capture_pageview: true,
148+
capture_pageview: true, // can be true, false, or 'history-change'
149149
capture_pageleave: 'if_capture_pageview', // We'll only capture pageleave events if capture_pageview is also true
150-
capture_history_events: false,
151150
debug: (location && isString(location?.search) && location.search.indexOf('__posthog_debug=true') !== -1) || false,
152151
cookie_expiration: 365,
153152
upgrade: false,
@@ -2002,7 +2001,8 @@ export class PostHog {
20022001
_shouldCapturePageleave(): boolean {
20032002
return (
20042003
this.config.capture_pageleave === true ||
2005-
(this.config.capture_pageleave === 'if_capture_pageview' && this.config.capture_pageview)
2004+
(this.config.capture_pageleave === 'if_capture_pageview' &&
2005+
(this.config.capture_pageview === true || this.config.capture_pageview === 'history_change'))
20062006
)
20072007
}
20082008

src/types.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,6 @@ export interface PostHogConfig {
331331
*/
332332
rageclick: boolean
333333

334-
/**
335-
* Determines whether PostHog should automatically capture navigation events using the History API and emit them as pageviews.
336-
*
337-
* @default false
338-
*/
339-
capture_history_events: boolean
340-
341334
/**
342335
* Determines if cookie should be set on the top level domain (example.com).
343336
* If PostHog-js is loaded on a subdomain (test.example.com), and `cross_subdomain_cookie` is set to false,
@@ -426,10 +419,14 @@ export interface PostHogConfig {
426419

427420
/**
428421
* Determines whether PostHog should capture pageview events automatically.
422+
* Can be:
423+
* - `true`: Capture regular pageviews (default)
424+
* - `false`: Don't capture any pageviews
425+
* - `'history_change'`: Only capture pageviews on history API changes (pushState, replaceState, popstate)
429426
*
430427
* @default true
431428
*/
432-
capture_pageview: boolean
429+
capture_pageview: boolean | 'history_change'
433430

434431
/**
435432
* Determines whether PostHog should capture pageleave events.

0 commit comments

Comments
 (0)