-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add enterprise user activity events for request/document actions #10337
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
Open
fiosman
wants to merge
7
commits into
develop
Choose a base branch
from
feat/account-tracking-activity-events
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0aaf888
feat: track user activity events for request/document actions
fiosman 21c0491
chore: address copilot feedback
fiosman c259626
feat: adds enterprise plan type guard
fiosman 9d9c09d
chore: formatting
fiosman 8d2ae9b
refactor: clean up
fiosman c86e7b4
chore: use proper types
fiosman 249c7ac
refactor: clean up
fiosman 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
103 changes: 103 additions & 0 deletions
103
packages/insomnia/src/ui/utils/track-user-activity.test.ts
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,103 @@ | ||
| // @vitest-environment jsdom | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const { mockTrackUserAction, mockGetCurrentSessionId, mockGetAccountId } = vi.hoisted(() => ({ | ||
| mockTrackUserAction: vi.fn(), | ||
| mockGetCurrentSessionId: vi.fn(), | ||
| mockGetAccountId: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('insomnia-api', () => ({ | ||
| trackUserAction: mockTrackUserAction, | ||
| })); | ||
|
|
||
| vi.mock('~/common/account/session', () => ({ | ||
| getCurrentSessionId: mockGetCurrentSessionId, | ||
| getAccountId: mockGetAccountId, | ||
| })); | ||
|
|
||
| vi.mock('uuid', () => ({ | ||
| v4: () => 'evt_123', | ||
| })); | ||
|
|
||
| import { trackUserActivity } from './track-user-activity'; | ||
|
|
||
| describe('trackUserActivity', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| localStorage.clear(); | ||
| }); | ||
|
|
||
| it('no-ops when there is no session', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue(null); | ||
|
|
||
| await trackUserActivity('request_created'); | ||
|
|
||
| expect(mockTrackUserAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('no-ops when there is no account id', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue(null); | ||
|
|
||
| await trackUserActivity('request_created'); | ||
|
|
||
| expect(mockTrackUserAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('no-ops when the current user is not on an enterprise plan', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue('acct_123'); | ||
| localStorage.setItem('acct_123:currentPlan', JSON.stringify({ type: 'individual' })); | ||
|
|
||
| await trackUserActivity('request_created'); | ||
|
|
||
| expect(mockTrackUserAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('no-ops when there is no cached plan', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue('acct_123'); | ||
|
|
||
| await trackUserActivity('request_created'); | ||
|
|
||
| expect(mockTrackUserAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('tracks the action for an enterprise plan', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue('acct_123'); | ||
| localStorage.setItem('acct_123:currentPlan', JSON.stringify({ type: 'enterprise' })); | ||
|
|
||
| await trackUserActivity('request_created'); | ||
|
|
||
| expect(mockTrackUserAction).toHaveBeenCalledWith({ | ||
| sessionId: 'sess_xyz', | ||
| eventId: 'evt_123', | ||
| actionType: 'request_created', | ||
| }); | ||
| }); | ||
|
|
||
| it('tracks the action for an enterprise-member plan', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue('acct_123'); | ||
| localStorage.setItem('acct_123:currentPlan', JSON.stringify({ type: 'enterprise-member' })); | ||
|
|
||
| await trackUserActivity('request_executed'); | ||
|
|
||
| expect(mockTrackUserAction).toHaveBeenCalledWith({ | ||
| sessionId: 'sess_xyz', | ||
| eventId: 'evt_123', | ||
| actionType: 'request_executed', | ||
| }); | ||
| }); | ||
|
|
||
| it('does not throw when trackUserAction rejects', async () => { | ||
| mockGetCurrentSessionId.mockResolvedValue('sess_xyz'); | ||
| mockGetAccountId.mockResolvedValue('acct_123'); | ||
| localStorage.setItem('acct_123:currentPlan', JSON.stringify({ type: 'enterprise' })); | ||
| mockTrackUserAction.mockRejectedValue(new Error('network error')); | ||
|
|
||
| await expect(trackUserActivity('document_created')).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
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,36 @@ | ||
| import { type CurrentPlan, trackUserAction, type UserActionType } from 'insomnia-api'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| import { getAccountId, getCurrentSessionId } from '~/common/account/session'; | ||
|
|
||
| const isCurrentAccountOnEnterprisePlan = async (): Promise<boolean> => { | ||
| const accountId = await getAccountId(); | ||
| if (!accountId) { | ||
| return false; | ||
| } | ||
|
|
||
| const currentPlan = JSON.parse(localStorage.getItem(`${accountId}:currentPlan`) || '{}') as CurrentPlan; | ||
| return currentPlan?.type === 'enterprise' || currentPlan?.type === 'enterprise-member'; | ||
| }; | ||
|
|
||
| /** | ||
| * POST /v3/users/me/actions. Fire-and-forget: never throws, no-ops when logged out or when the current user isn't on an enterprise plan. | ||
| * Call sites should not await this so it never blocks the calling flow. | ||
| */ | ||
| export const trackUserActivity = async (actionType: UserActionType): Promise<void> => { | ||
| try { | ||
| const sessionId = await getCurrentSessionId(); | ||
| if (!sessionId) { | ||
| return; | ||
| } | ||
|
|
||
| const isEnterpriseMember = await isCurrentAccountOnEnterprisePlan(); | ||
| if (!isEnterpriseMember) { | ||
| return; | ||
| } | ||
|
|
||
| await trackUserAction({ sessionId, eventId: uuidv4(), actionType }); | ||
| } catch (error) { | ||
| console.error('Failed to track user activity', error); | ||
| } | ||
| }; |
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.