-
Notifications
You must be signed in to change notification settings - Fork 20
Track session metrics accurately #437
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a91d31c
Track session metrics accurately
Producdevity 10e3275
Cover session tracker privacy and fallback paths
Producdevity 2c9058a
Avoid stale session page load times
Producdevity d0f9673
Clean up session tracker code
Producdevity fd17056
Remove Vercel analytics integrations
Producdevity 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { fireEvent, render, waitFor } from '@testing-library/react' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import SessionTracker from './SessionTracker' | ||
|
|
||
| type MockUser = { | ||
| id: string | ||
| externalAccounts?: { provider?: string }[] | ||
| primaryEmailAddress?: { id: string } | null | ||
| } | ||
|
|
||
| const testState = vi.hoisted(() => ({ | ||
| analyticsAllowed: true, | ||
| pathname: '/', | ||
| user: null as MockUser | null, | ||
| analytics: { | ||
| user: { | ||
| signedIn: vi.fn(), | ||
| }, | ||
| session: { | ||
| featureDiscovered: vi.fn(), | ||
| pageView: vi.fn(), | ||
| sessionEnded: vi.fn(), | ||
| sessionStarted: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@clerk/nextjs', () => ({ | ||
| useUser: () => ({ user: testState.user }), | ||
| })) | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| usePathname: () => testState.pathname, | ||
| })) | ||
|
|
||
| vi.mock('@/hooks', () => ({ | ||
| useCookieConsent: () => ({ analyticsAllowed: testState.analyticsAllowed }), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/analytics', () => ({ | ||
| default: testState.analytics, | ||
| })) | ||
|
|
||
| describe('SessionTracker', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| testState.analyticsAllowed = true | ||
| testState.pathname = '/' | ||
| testState.user = null | ||
| }) | ||
|
|
||
| it('does not track session activity when analytics are disabled', () => { | ||
| testState.analyticsAllowed = false | ||
|
|
||
| render(<SessionTracker />) | ||
|
|
||
| fireEvent.click(document.body) | ||
| window.dispatchEvent(new Event('beforeunload')) | ||
|
|
||
| expect(testState.analytics.session.sessionStarted).not.toHaveBeenCalled() | ||
| expect(testState.analytics.session.pageView).not.toHaveBeenCalled() | ||
| expect(testState.analytics.session.sessionEnded).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('tracks real page view and interaction counts when the session ends', async () => { | ||
| const view = render(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.session.sessionStarted).toHaveBeenCalledOnce() | ||
| expect(testState.analytics.session.pageView).toHaveBeenCalledOnce() | ||
| }) | ||
| expect(testState.analytics.session.pageView).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect.objectContaining({ | ||
| loadTime: expect.any(Number), | ||
| pathname: '/', | ||
| }), | ||
| ) | ||
|
|
||
| testState.pathname = '/games' | ||
| view.rerender(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.session.pageView).toHaveBeenCalledTimes(2) | ||
| }) | ||
| expect(testState.analytics.session.pageView).toHaveBeenLastCalledWith({ | ||
| pathname: '/games', | ||
| userId: undefined, | ||
| }) | ||
|
|
||
| fireEvent.click(document.body) | ||
| fireEvent.keyDown(document, { key: 'Enter' }) | ||
| window.dispatchEvent(new Event('beforeunload')) | ||
|
|
||
| expect(testState.analytics.session.sessionEnded).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| duration: expect.any(Number), | ||
| interactions: 2, | ||
| pageViews: 2, | ||
| sessionId: expect.any(String), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('reports the Clerk OAuth provider without counting sign-in as a page view', async () => { | ||
| const view = render(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.session.sessionStarted).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| testState.user = { | ||
| id: 'user-1', | ||
| externalAccounts: [{ provider: 'oauth_google' }], | ||
| primaryEmailAddress: null, | ||
| } | ||
| view.rerender(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.user.signedIn).toHaveBeenCalledWith({ | ||
| method: 'google', | ||
| userId: 'user-1', | ||
| }) | ||
| }) | ||
| expect(testState.analytics.session.pageView).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('falls back to email sign-in when the Clerk user has no OAuth provider', async () => { | ||
| const view = render(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.session.sessionStarted).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| testState.user = { | ||
| id: 'user-2', | ||
| primaryEmailAddress: { id: 'email-1' }, | ||
| } | ||
| view.rerender(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.user.signedIn).toHaveBeenCalledWith({ | ||
| method: 'email', | ||
| userId: 'user-2', | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| it('falls back to clerk sign-in when the user has no OAuth provider or email', async () => { | ||
| const view = render(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.session.sessionStarted).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| testState.user = { | ||
| id: 'user-3', | ||
| primaryEmailAddress: null, | ||
| } | ||
| view.rerender(<SessionTracker />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(testState.analytics.user.signedIn).toHaveBeenCalledWith({ | ||
| method: 'clerk', | ||
| userId: 'user-3', | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
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.