-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: inso analytics [INS-1834] #9621
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
6 commits
Select commit
Hold shift + click to select a range
ac3aa1c
feat: inso analytics [INS-1834]
ryan-willis 3455325
formatting + circular import fix
ryan-willis e03b37f
fix: don't lose overrides when memDB contains Settings
ryan-willis e9fe38b
refactor: alleviate circular import additions
ryan-willis b51013a
review changes
ryan-willis dd38ed7
reduce refs to db type
ryan-willis 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const mockTrack = vi.fn(); | ||
|
|
||
| vi.mock('@segment/analytics-node', () => ({ | ||
| Analytics: vi.fn(() => ({ | ||
| track: mockTrack, | ||
| closeAndFlush: vi.fn(), | ||
| })), | ||
| })); | ||
|
|
||
| vi.mock('./db/adapters/ne-db-adapter', () => ({ | ||
| default: vi.fn().mockResolvedValue(null), | ||
| })); | ||
|
|
||
| describe('analytics', () => { | ||
| beforeEach(() => { | ||
| vi.stubEnv('NODE_ENV', 'production'); | ||
| vi.resetModules(); | ||
| mockTrack.mockClear(); | ||
| }); | ||
|
|
||
| it('should use the same anonymousId for multiple trackInsoEvent calls', async () => { | ||
| const { trackInsoEvent, InsoEvent } = await import('./analytics'); | ||
|
|
||
| await trackInsoEvent(InsoEvent.lintSpec); | ||
| await trackInsoEvent(InsoEvent.exportSpec); | ||
|
|
||
| expect(mockTrack).toHaveBeenCalledTimes(2); | ||
|
|
||
| const firstCallAnonymousId = mockTrack.mock.calls[0][0].anonymousId; | ||
| const secondCallAnonymousId = mockTrack.mock.calls[1][0].anonymousId; | ||
|
|
||
| expect(firstCallAnonymousId).toBe(secondCallAnonymousId); | ||
| expect(firstCallAnonymousId).toMatch(/^anon_/); | ||
| }); | ||
| }); |
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,118 @@ | ||
| import os from 'node:os'; | ||
|
|
||
| import { Analytics } from '@segment/analytics-node'; | ||
| import { getSegmentWriteKey } from 'insomnia/src/common/constants'; | ||
| import type { Settings } from 'insomnia/src/models/settings'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| import packageJson from '../package.json'; | ||
| import neDbAdapter from './db/adapters/ne-db-adapter'; | ||
| import { getAppDataDir, getDefaultProductName } from './util'; | ||
|
|
||
| export enum InsoEvent { | ||
| runTest = 'inso_run_test', | ||
| runCollection = 'inso_run_collection', | ||
| lintSpec = 'inso_lint_spec', | ||
| exportSpec = 'inso_export_spec', | ||
| script = 'inso_script', | ||
| } | ||
|
|
||
| const analyticsClient = new Analytics({ writeKey: getSegmentWriteKey() }); | ||
| let deviceId: string | null = null; | ||
| let localSettings: Settings | null = null; | ||
|
|
||
| const getLocalSettings = async (): Promise<Settings | null> => { | ||
| if (localSettings) { | ||
| return localSettings; | ||
| } | ||
|
|
||
| try { | ||
| const appDataDir = getAppDataDir(getDefaultProductName()); | ||
| const db = await neDbAdapter(appDataDir, ['Settings']); | ||
| localSettings = db?.Settings?.[0] ?? null; | ||
| return localSettings; | ||
ryan-willis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const getDeviceId = async (): Promise<string> => { | ||
| if (deviceId) { | ||
| return deviceId; | ||
| } | ||
|
|
||
| try { | ||
| const settings = await getLocalSettings(); | ||
| if (settings?.deviceId) { | ||
| deviceId = settings.deviceId; | ||
| return deviceId; | ||
| } | ||
| } catch {} | ||
|
|
||
| deviceId = `anon_${uuidv4()}`; | ||
| return deviceId; | ||
| }; | ||
|
|
||
| const getOsName = (): string => { | ||
| switch (process.platform) { | ||
| case 'darwin': { | ||
| return 'mac'; | ||
| } | ||
| case 'win32': { | ||
| return 'windows'; | ||
| } | ||
| default: { | ||
| return process.platform; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const trackInsoEvent = async (event: InsoEvent, properties?: Record<string, unknown>): Promise<void> => { | ||
| if (process.env.NODE_ENV === 'test') { | ||
ZxBing0066 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| const settings = await getLocalSettings(); | ||
| if (settings && !settings.enableAnalytics) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const anonymousId = await getDeviceId(); | ||
ryan-willis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const version = process.env.VERSION || packageJson.version; | ||
|
|
||
| analyticsClient.track( | ||
| { | ||
| event, | ||
| anonymousId, | ||
| properties: { | ||
| ...properties, | ||
| platform: 'cli', | ||
| }, | ||
| context: { | ||
| app: { | ||
| name: 'inso', | ||
| version, | ||
| }, | ||
| os: { | ||
| name: getOsName(), | ||
| version: os.release(), | ||
| }, | ||
| }, | ||
| }, | ||
| () => { | ||
| // Silently fail | ||
| }, | ||
| ); | ||
| } catch { | ||
| // Silently fail | ||
| } | ||
| }; | ||
|
|
||
| export const flushAnalytics = async (): Promise<void> => { | ||
| try { | ||
| await analyticsClient.closeAndFlush({ timeout: 5000 }); | ||
| } catch { | ||
| // Silently fail | ||
| } | ||
| }; | ||
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.
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.