Repository navigation
feat(analytics): auto page view tracking + key events #3773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import posthog from 'posthog-js'; | ||
|
|
||
| /** | ||
| * @desc Check whether PostHog has been initialized and is ready to capture. | ||
| * @returns {boolean} True when the PostHog SDK instance is loaded. | ||
| */ | ||
| function isPosthogReady() { | ||
| return !!(posthog && posthog.__loaded); | ||
| } | ||
|
|
||
| /** | ||
| * @desc Capture an analytics event, guarded by PostHog readiness. | ||
| * @param {string} event - The event name (e.g. 'signup_completed'). | ||
| * @param {Object} [properties={}] - Optional properties to attach. | ||
| * @returns {void} | ||
| */ | ||
| function capture(event, properties = {}) { | ||
| if (!isPosthogReady()) return; | ||
| posthog.capture(event, properties); | ||
| } | ||
|
|
||
| /** | ||
| * @desc Capture a `$pageview` event for the given route. | ||
| * @param {import('vue-router').RouteLocationNormalized} to - Target route. | ||
| * @returns {void} | ||
| */ | ||
| function capturePageview(to) { | ||
| capture('$pageview', { | ||
| $current_url: to.fullPath, | ||
|
PierreBrisorgueil marked this conversation as resolved.
|
||
| title: to.meta?.title || to.name, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Exports. | ||
| */ | ||
| export { isPosthogReady, capture, capturePageview }; | ||
| export default { isPosthogReady, capture, capturePageview }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| const mockCapture = vi.fn(); | ||
|
|
||
| vi.mock('posthog-js', () => ({ | ||
| default: { | ||
| __loaded: false, | ||
| capture: (...args) => mockCapture(...args), | ||
| }, | ||
| })); | ||
|
|
||
| import posthog from 'posthog-js'; | ||
| import { isPosthogReady, capture, capturePageview } from '../analytics'; | ||
|
|
||
| describe('analytics helper', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| posthog.__loaded = false; | ||
| }); | ||
|
|
||
| describe('isPosthogReady', () => { | ||
| it('should return false when posthog is not loaded', () => { | ||
| expect(isPosthogReady()).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true when posthog is loaded', () => { | ||
| posthog.__loaded = true; | ||
| expect(isPosthogReady()).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('capture', () => { | ||
| it('should not call posthog.capture when not loaded', () => { | ||
| capture('test_event', { key: 'value' }); | ||
| expect(mockCapture).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call posthog.capture when loaded', () => { | ||
| posthog.__loaded = true; | ||
| capture('test_event', { key: 'value' }); | ||
| expect(mockCapture).toHaveBeenCalledWith('test_event', { key: 'value' }); | ||
| }); | ||
|
|
||
| it('should pass empty properties by default', () => { | ||
| posthog.__loaded = true; | ||
| capture('test_event'); | ||
| expect(mockCapture).toHaveBeenCalledWith('test_event', {}); | ||
| }); | ||
| }); | ||
|
|
||
| describe('capturePageview', () => { | ||
| it('should not capture when posthog is not loaded', () => { | ||
| capturePageview({ fullPath: '/test', meta: { title: 'Test' }, name: 'test' }); | ||
| expect(mockCapture).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should capture $pageview with route title from meta', () => { | ||
| posthog.__loaded = true; | ||
| capturePageview({ fullPath: '/dashboard', meta: { title: 'Dashboard' }, name: 'dashboard' }); | ||
| expect(mockCapture).toHaveBeenCalledWith('$pageview', { | ||
| $current_url: '/dashboard', | ||
| title: 'Dashboard', | ||
| }); | ||
| }); | ||
|
|
||
| it('should fall back to route name when meta.title is absent', () => { | ||
| posthog.__loaded = true; | ||
| capturePageview({ fullPath: '/tasks', meta: {}, name: 'tasks' }); | ||
| expect(mockCapture).toHaveBeenCalledWith('$pageview', { | ||
| $current_url: '/tasks', | ||
| title: 'tasks', | ||
| }); | ||
|
PierreBrisorgueil marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
|
PierreBrisorgueil marked this conversation as resolved.
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.