Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,15 @@ function Root() {
// no-ops without a configured token; sends only allowlisted custom events
// through the same-origin /rly proxy. The initial sanitized $pageview
// emits once the SDK is ready; client-side route changes emit from
// useRoute, deduped on pathname.
void initAnalytics().then(() => capturePageview()).catch(() => {});
// useRoute, deduped on pathname. The boot-level popstate listener covers
// back/forward for anonymous sessions too (useRoute only mounts inside the
// authenticated App) — pathname dedupe collapses the overlap.
void initAnalytics()
.then(() => {
capturePageview();
window.addEventListener('popstate', () => capturePageview());
})
.catch(() => {});

createRoot(document.getElementById("root")!).render(
<StrictMode>
Expand Down
5 changes: 4 additions & 1 deletion test/posthog-analytics.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,10 @@ test('facade dedupes pageviews and gates on the public host', () => {

test('pageview wiring: boot emit once, route changes and popstate covered', () => {
const main = src('src/main.tsx');
assert.match(main, /initAnalytics\(\)\.then\(\(\) => capturePageview\(\)\)/);
assert.match(main, /initAnalytics\(\)[\s\S]*?capturePageview\(\)/);
// boot-level popstate covers back/forward even before the authenticated
// App mounts; useRoute's own listener dedupes against it on pathname.
assert.match(main, /addEventListener\('popstate',[\s\S]*?capturePageview\(\)/);
const pages = src('src/pages.ts');
// navigate + popstate each emit — dedup in capturePageview collapses
// same-path repeats (StrictMode, hash/range-only history entries).
Expand Down
Loading