Repository navigation
feat(apps): app pages report usage events through Desktop telemetry, and the sidebar records page opens #2087
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
10 commits
Select commit
Hold shift + click to select a range
35e7ac4
feat(apps): let an app page report usage events through Desktop telem…
vqt123 dba7c57
feat(apps): take any well-formed page event, with the app id Desktop …
vqt123 4799265
fix(apps): give a page event the app whose page is loaded, not the ap…
vqt123 194e1bc
fix(apps): keep the app id when an app drawn by a hash route is reope…
vqt123 82a6084
fix(apps): keep catalog app ids, limit page events, and drop the app …
vqt123 2dbf399
fix(apps): count only sent page events against the limits, keep the a…
vqt123 6861ebc
docs(bridge-types): list the keys Desktop sets itself and the shared …
vqt123 22e865e
fix(panel): record a sidebar page open only for a click in the sidebar
vqt123 f1b2aa7
fix(apps): keep a hash-route app through a query change or a child ro…
vqt123 2dbe9b3
chore(bridge-types): bump to 0.13.0 for the hosted telemetry bridge
vqt123 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import os from 'os' | ||
| import path from 'path' | ||
|
|
||
| vi.mock('electron', () => ({ | ||
| app: { | ||
| getPath: () => path.join(os.tmpdir(), 'launcher-test'), | ||
| isPackaged: true, | ||
| on: () => {} | ||
| }, | ||
| BrowserWindow: { getAllWindows: () => [] } | ||
| })) | ||
|
|
||
| const sdkCaptures = vi.hoisted( | ||
| () => [] as Array<{ event: string; properties?: Record<string, unknown> }> | ||
| ) | ||
|
|
||
| vi.mock('posthog-node', () => ({ | ||
| PostHog: class { | ||
| on(): () => void { | ||
| return () => {} | ||
| } | ||
| capture(call: { event: string; properties?: Record<string, unknown> }): void { | ||
| sdkCaptures.push(call) | ||
| } | ||
| identify(): void {} | ||
| flush(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| shutdown(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| import * as telemetry from '../lib/telemetry' | ||
| import { | ||
| APP_PAGE_EVENT_PREFIX, | ||
| DESKTOP_APP_EVENTS, | ||
| MAX_PAGE_EVENT_PROPERTIES, | ||
| UNLIMITED_EVENT_NAMES, | ||
| _resetPageEventLimitsForTest, | ||
| checkAppPageEvent, | ||
| trackAppPageEvent | ||
| } from './appPageEvents' | ||
|
|
||
| const APP = 'remove-background' | ||
|
|
||
| describe('checkAppPageEvent', () => { | ||
| it('sends a page event under the Apps prefix with the app id Desktop holds', () => { | ||
| expect( | ||
| checkAppPageEvent( | ||
| 'run_started', | ||
| { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true }, | ||
| APP | ||
| ) | ||
| ).toEqual({ | ||
| event: 'comfy.desktop.apps.run_started', | ||
| properties: { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true, app_id: APP } | ||
| }) | ||
| }) | ||
|
|
||
| it.each(['click', 'run_started', 'example_used', 'a', `a${'b'.repeat(47)}`, 'step_2_done'])( | ||
| 'accepts the event name %s', | ||
| (name) => { | ||
| expect(checkAppPageEvent(name, {}, APP)?.event).toBe(`${APP_PAGE_EVENT_PREFIX}${name}`) | ||
| } | ||
| ) | ||
|
|
||
| it.each([ | ||
| ['too long', `a${'b'.repeat(48)}`], | ||
| ['starting with a digit', '2fast'], | ||
| ['starting with an underscore', '_hidden'], | ||
| ['with capitals', 'RunStarted'], | ||
| ['with a dot', 'apps.run_started'], | ||
| ['with a dash', 'run-started'], | ||
| ['with a space', 'run started'], | ||
| ['empty', ''], | ||
| ['not a string', 42] | ||
| ])('refuses an event name %s', (_why, name) => { | ||
| expect(checkAppPageEvent(name, {}, APP)).toBeNull() | ||
| }) | ||
|
|
||
| it.each([...DESKTOP_APP_EVENTS])('refuses %s, which only Desktop sends', (name) => { | ||
| expect(['app_opened', 'app_page_failed']).toContain(name) | ||
| expect(checkAppPageEvent(name, {}, APP)).toBeNull() | ||
| }) | ||
|
|
||
| it('takes app_id from Desktop and ignores the app the page names', () => { | ||
| expect( | ||
| checkAppPageEvent('click', { app: 'other-app', app_id: 'other-app', target: 'run' }, APP) | ||
| ).toEqual({ | ||
| event: 'comfy.desktop.apps.click', | ||
| properties: { target: 'run', app_id: APP } | ||
| }) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['no app id', null], | ||
| ['an app id the catalog would not list', 'Remove Background'], | ||
| ['an app id with a dot', 'remove.background'], | ||
| ['an app id longer than the catalog allows', `a${'b'.repeat(128)}`] | ||
| ])('drops the event when Desktop holds %s for the page', (_why, appId) => { | ||
| expect(checkAppPageEvent('click', { target: 'run' }, appId)).toBeNull() | ||
| }) | ||
|
|
||
| it.each(['HDR', 'Upscale_4x', `a${'b'.repeat(127)}`])( | ||
| 'keeps the catalog app id %s as Desktop holds it', | ||
| (appId) => { | ||
| expect(checkAppPageEvent('click', {}, appId)?.properties).toEqual({ app_id: appId }) | ||
| } | ||
| ) | ||
|
|
||
| it('refuses error, whose full name the shared telemetry limit lets through unlimited', () => { | ||
| expect(UNLIMITED_EVENT_NAMES.has('error')).toBe(true) | ||
| expect(checkAppPageEvent('error', {}, APP)).toBeNull() | ||
| expect(checkAppPageEvent('run_error', {}, APP)?.event).toBe('comfy.desktop.apps.run_error') | ||
| }) | ||
|
|
||
| it('drops free text, bad values and bad property names, keeping the event', () => { | ||
| expect( | ||
| checkAppPageEvent( | ||
| 'run_started', | ||
| { | ||
| where: 'local', | ||
| prompt: 'a cat on the moon', | ||
| file: '/Users/someone/Pictures/me.png', | ||
| name: 'My Model.safetensors', | ||
| long: 'a'.repeat(65), | ||
| nan: Number.NaN, | ||
| forever: Number.POSITIVE_INFINITY, | ||
| nothing: null, | ||
| list: ['a'], | ||
| nested: { a: 1 }, | ||
| Capital: 'x', | ||
| '1st': 'x', | ||
| [`a${'b'.repeat(32)}`]: 'x', | ||
| $set: 'x', | ||
| app_version: '9.9.9', | ||
| installation_id: 'fake', | ||
| ok: false, | ||
| count: 0 | ||
| }, | ||
| APP | ||
| ) | ||
| ).toEqual({ | ||
| event: 'comfy.desktop.apps.run_started', | ||
| properties: { where: 'local', ok: false, count: 0, app_id: APP } | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps a 64-character slug and a 32-character property name', () => { | ||
| const key = `a${'b'.repeat(31)}` | ||
| const value = `a${'b'.repeat(63)}` | ||
| expect(checkAppPageEvent('click', { [key]: value }, APP)?.properties).toEqual({ | ||
| [key]: value, | ||
| app_id: APP | ||
| }) | ||
| }) | ||
|
|
||
| it(`keeps at most ${MAX_PAGE_EVENT_PROPERTIES} properties besides app_id`, () => { | ||
| const many = Object.fromEntries(Array.from({ length: 20 }, (_, i) => [`p${i}`, i])) | ||
| const kept = checkAppPageEvent('click', many, APP)?.properties ?? {} | ||
| expect(Object.keys(kept)).toHaveLength(MAX_PAGE_EVENT_PROPERTIES + 1) | ||
| expect(kept).toMatchObject({ p0: 0, p15: 15, app_id: APP }) | ||
| expect(kept).not.toHaveProperty('p16') | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['not an object', 'run'], | ||
| ['a list', ['run']], | ||
| ['missing', undefined] | ||
| ])('sends only app_id when the properties are %s', (_why, props) => { | ||
| expect(checkAppPageEvent('click', props, APP)?.properties).toEqual({ app_id: APP }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('trackAppPageEvent through Desktop telemetry', () => { | ||
| beforeEach(() => { | ||
| process.env['POSTHOG_API_KEY'] = 'test-key' | ||
| process.env['POSTHOG_ENABLED'] = '1' | ||
| telemetry._resetForTest() | ||
| _resetPageEventLimitsForTest() | ||
| telemetry.initTelemetry({ appVersion: '0.0.0', appEnv: 'test', isPackaged: true }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| telemetry._resetForTest() | ||
| sdkCaptures.length = 0 | ||
| delete process.env['POSTHOG_API_KEY'] | ||
| delete process.env['POSTHOG_ENABLED'] | ||
| }) | ||
|
|
||
| function appCaptures(): Array<{ event: string; properties?: Record<string, unknown> }> { | ||
| return sdkCaptures.filter((c) => c.event.startsWith(APP_PAGE_EVENT_PREFIX)) | ||
| } | ||
|
|
||
| it('sends a good event to PostHog when the person allows telemetry', () => { | ||
| telemetry.setConsentState('granted') | ||
| telemetry.bindAnonymousId('anon-1', 'anon-1', {}) | ||
|
|
||
| expect(trackAppPageEvent('run_started', { where: 'cloud', prompt: 'a cat' }, APP)).toBe(true) | ||
|
|
||
| const sent = appCaptures() | ||
| expect(sent.map((c) => c.event)).toEqual(['comfy.desktop.apps.run_started']) | ||
| expect(sent[0]?.properties).toMatchObject({ app_id: APP, where: 'cloud' }) | ||
| expect(sent[0]?.properties).not.toHaveProperty('prompt') | ||
| }) | ||
|
|
||
| it.each(['denied', 'undecided'] as const)('sends nothing while consent is %s', (consent) => { | ||
| telemetry.setConsentState(consent) | ||
| telemetry.bindAnonymousId('anon-1', 'anon-1', {}) | ||
|
|
||
| expect(trackAppPageEvent('run_started', { where: 'cloud' }, APP)).toBe(false) | ||
| expect(trackAppPageEvent('click', { target: 'run' }, APP)).toBe(false) | ||
|
|
||
| expect(appCaptures()).toEqual([]) | ||
| }) | ||
|
|
||
| it('sends nothing for an event it refuses, even with consent', () => { | ||
| telemetry.setConsentState('granted') | ||
| telemetry.bindAnonymousId('anon-1', 'anon-1', {}) | ||
|
|
||
| expect(trackAppPageEvent('app_opened', {}, APP)).toBe(false) | ||
| expect(trackAppPageEvent('Not An Event', {}, APP)).toBe(false) | ||
| expect(trackAppPageEvent('click', {}, null)).toBe(false) | ||
|
|
||
| expect(appCaptures()).toEqual([]) | ||
| }) | ||
| }) |
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.