diff --git a/packages/web/src/react/index.test.tsx b/packages/web/src/react/index.test.tsx index d564b6d..622d896 100644 --- a/packages/web/src/react/index.test.tsx +++ b/packages/web/src/react/index.test.tsx @@ -140,4 +140,28 @@ describe('', () => { }); }); }); + + describe('pageview tracking', () => { + it('tracks a pageview when route and path are both provided', () => { + render( + , + ); + + expect(window.vaq?.[0]).toEqual([ + 'pageview', + { route: '/blog/[slug]', path: '/blog/hello' }, + ]); + }); + + it('tracks a pageview when route is provided without path', () => { + // auto tracking is disabled as soon as `route` is set, so the component + // must still emit a pageview instead of silently dropping it. + render(); + + expect(window.vaq?.[0]).toEqual([ + 'pageview', + { route: '/blog/[slug]', path: window.location.pathname }, + ]); + }); + }); }); diff --git a/packages/web/src/react/index.tsx b/packages/web/src/react/index.tsx index 0825012..852d61c 100644 --- a/packages/web/src/react/index.tsx +++ b/packages/web/src/react/index.tsx @@ -57,8 +57,11 @@ function Analytics( useEffect(() => { // explicitely track page view, since we disabled auto tracking - if (props.route && props.path) { - pageview({ route: props.route, path: props.path }); + if (props.route) { + pageview({ + route: props.route, + path: props.path ?? window.location.pathname, + }); } }, [props.route, props.path]);