Skip to content

Commit b53e9d0

Browse files
committed
tests: add pageleave sanity checks
1 parent a9f6065 commit b53e9d0

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

playground/nextjs/src/posthog.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ if (typeof window !== 'undefined') {
5959
ignoreClass: 'ph-ignore-image',
6060
},
6161
debug: true,
62+
capture_pageview: 'history_change',
6263
disable_web_experiments: false,
6364
scroll_root_selector: ['#scroll_element', 'html'],
6465
persistence: cookieConsentGiven() ? 'localStorage+cookie' : 'memory',

playground/react-router/app/root.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { PostHogProvider, usePostHog } from 'posthog-js/react'
1+
import { PostHogProvider } from 'posthog-js/react'
22
import type { PostHogConfig } from 'posthog-js'
33

4-
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration, useLocation } from 'react-router'
4+
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router'
55
import type { Route } from './+types/root'
66
import './app.css'
7-
import { useEffect } from 'react'
87

98
export const links: Route.LinksFunction = () => [
109
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' },
@@ -40,7 +39,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
4039
const posthogKey: string = import.meta.env.VITE_PUBLIC_POSTHOG_KEY!
4140
const posthogOptions: Partial<PostHogConfig> = {
4241
api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST,
43-
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
42+
capture_pageview: 'history_change',
4443
capture_pageleave: true,
4544
debug: true,
4645
}
@@ -54,12 +53,6 @@ export default function App() {
5453
}
5554

5655
export function AppInner() {
57-
const posthog = usePostHog()
58-
const location = useLocation()
59-
useEffect(() => {
60-
posthog.capture('$pageview')
61-
}, [posthog, location])
62-
6356
return <Outlet />
6457
}
6558

src/__tests__/posthog-core.ts

+29
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,21 @@ describe('posthog core', () => {
684684
expect(posthog.capture).toHaveBeenCalledWith('$pageleave')
685685
})
686686

687+
it('captures $pageleave when capture_pageview is set to history_change', () => {
688+
const posthog = posthogWith(
689+
{
690+
capture_pageview: 'history_change',
691+
capture_pageleave: 'if_capture_pageview',
692+
request_batching: true,
693+
},
694+
{ capture: jest.fn() }
695+
)
696+
697+
posthog._handle_unload()
698+
699+
expect(posthog.capture).toHaveBeenCalledWith('$pageleave')
700+
})
701+
687702
it('does not capture $pageleave when capture_pageview=false and capture_pageleave=if_capture_pageview', () => {
688703
const posthog = posthogWith(
689704
{
@@ -744,6 +759,20 @@ describe('posthog core', () => {
744759
expect(posthog.capture).toHaveBeenCalledWith('$pageleave', null, { transport: 'sendBeacon' })
745760
})
746761

762+
it('captures $pageleave when capture_pageview is set to history_change', () => {
763+
const posthog = posthogWith(
764+
{
765+
capture_pageview: 'history_change',
766+
capture_pageleave: 'if_capture_pageview',
767+
request_batching: false,
768+
},
769+
{ capture: jest.fn() }
770+
)
771+
posthog._handle_unload()
772+
773+
expect(posthog.capture).toHaveBeenCalledWith('$pageleave', null, { transport: 'sendBeacon' })
774+
})
775+
747776
it('does not capture $pageleave when capture_pageview=false', () => {
748777
const posthog = posthogWith(
749778
{

0 commit comments

Comments
 (0)