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
57 changes: 57 additions & 0 deletions src/providers/posthog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { posthog } from 'posthog-js';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { PostHogAnalyticsProvider } from './posthog';

vi.mock('posthog-js', () => ({
posthog: {
init: vi.fn(),
},
}));

describe('PostHogAnalyticsProvider', () => {
const init = vi.mocked(posthog.init);

beforeEach(() => {
init.mockClear();
});

it('defaults pageview capture to history changes for SPA navigation', async () => {
const provider = new PostHogAnalyticsProvider(
{
enabled: true,
key: 'test-key',
},
'test',
);

await provider.initialize();

expect(init).toHaveBeenCalledWith(
'test-key',
expect.objectContaining({
capture_pageview: 'history_change',
}),
);
});

it('keeps an explicit pageview capture override', async () => {
const provider = new PostHogAnalyticsProvider(
{
capture_pageview: false,
enabled: true,
key: 'test-key',
},
'test',
);

await provider.initialize();

expect(init).toHaveBeenCalledWith(
'test-key',
expect.objectContaining({
capture_pageview: false,
}),
);
});
});
1 change: 1 addition & 0 deletions src/providers/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class PostHogAnalyticsProvider extends BaseAnalytics {
const initConfig = {
...posthogConfig, // User's posthog-js config options
api_host: host || posthogConfig.api_host || 'https://app.posthog.com',
capture_pageview: posthogConfig.capture_pageview ?? 'history_change',
// Use before_send to dynamically add business context to all events
before_send: this.createBeforeSendHandler(posthogConfig.before_send),
debug: this.debug,
Expand Down
Loading