Skip to content
Open
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
24 changes: 24 additions & 0 deletions packages/web/src/react/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,28 @@ describe('<Analytics />', () => {
});
});
});

describe('pageview tracking', () => {
it('tracks a pageview when route and path are both provided', () => {
render(
<Analytics mode="production" path="/blog/hello" route="/blog/[slug]" />,
);

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(<Analytics mode="production" route="/blog/[slug]" />);

expect(window.vaq?.[0]).toEqual([
'pageview',
{ route: '/blog/[slug]', path: window.location.pathname },
]);
});
});
});
7 changes: 5 additions & 2 deletions packages/web/src/react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down