Skip to content

Commit b13fef8

Browse files
committed
telemetry: send the install id with the plugin version search
The API already tracks plugin version searches in Mixpanel, but with a fresh id per event. Sending the install id (X-Codify-Install-Id, only while telemetry is on) lets those events count per install too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016QnBk3jcWWemxdBkVpmxmN
1 parent da92298 commit b13fef8

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Parse → TemplateExpander.expand() → PluginManager.initialize() → os/distro
300300

301301
- **Events**: `command_completed` (`BaseCommand.trackCompleted`, called by each command before `process.exit` and by `catch`), `resource_outcome` (one per applied resource, `RunStats.recordApplyResult`) and `prompt_session_completed` (`ToolHost.stats`). `RunStats.recordPlan` collects resource type counts during plan/apply/destroy.
302302
- **Never sent**: config contents, parameter values, resource names, file paths, error messages, prompt text. Resource types of non-official plugins are sent as `custom`; errors only as `error.name` (must be set explicitly, class names don't survive minification) and `PluginErrorData.errorType`; flags only as names.
303-
- **Identity**: a random install id in `~/.codify/telemetry.json`. The CLI never sends a user id: the API reads it from the verified access token and stores it as a plain property (no Mixpanel identity merge).
303+
- **Identity**: a random install id in `~/.codify/telemetry.json`. Also sent as `X-Codify-Install-Id` with the plugin version search (`telemetry.installIdHeader()`, `ApiClient.searchPlugins`), so the API's own plugin events count per install; omitted when telemetry is off. The CLI never sends a user id: the API reads it from the verified access token and stores it as a plain property (no Mixpanel identity merge).
304304
- **Opt out**: `codify telemetry disable`, `DO_NOT_TRACK=1`, `CODIFY_TELEMETRY_DISABLED=1` (also set in `codify test` VMs). Off under Vitest. `CODIFY_TELEMETRY_DEBUG=1` prints events to stderr instead of sending them. The notice is printed once, to stderr.
305305
- A new field must be added to the API's schema (`workers/api/src/telemetry/schemas.ts`) too, or it is dropped.
306306

‎src/api/backend/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path';
44
import { Readable } from 'node:stream';
55
import { finished } from 'node:stream/promises';
66

7+
import { telemetry } from '../../telemetry/telemetry.js';
78
import { PluginSearchQuery, PluginSearchResult } from './types.js';
89

910
const API_BASE_URL = 'https://api.codifycli.com'
@@ -13,7 +14,8 @@ export const ApiClient = {
1314
const body = JSON.stringify({ query, cliVersion });
1415
const res = await fetch(
1516
`${API_BASE_URL}/v2/plugins/versions/search`,
16-
{ method: 'POST', body, headers: { 'Content-Type': 'application/json' } }
17+
// The install id lets the API count plugin resolutions per install (nothing when telemetry is off)
18+
{ method: 'POST', body, headers: { 'Content-Type': 'application/json', ...telemetry.installIdHeader() } }
1719
);
1820

1921
if (!res.ok) {

‎src/commands/telemetry.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export default class Telemetry extends Command {
88
`Show or change whether Codify sends anonymous usage stats.
99
1010
Codify sends which commands you run and how they end, which resource types are
11-
applied and whether they fail, and your CLI version, OS and architecture. It never
12-
sends your configs, file paths, parameter values or error messages.
11+
applied and whether they fail, and your CLI version, OS and architecture. Stats are
12+
tied to a random install id (also sent when Codify looks up plugin versions), never
13+
to your name or machine. It never sends your configs, file paths, parameter values
14+
or error messages.
1315
1416
DO_NOT_TRACK=1 or CODIFY_TELEMETRY_DISABLED=1 also turn it off.
1517
CODIFY_TELEMETRY_DEBUG=1 prints every event instead of sending it.

‎src/telemetry/telemetry.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ describe('Telemetry', () => {
8383
expect(t.pending()).toEqual([]);
8484
});
8585

86+
it('adds the install id header only when telemetry is on', () => {
87+
const on = new Telemetry(statePath, undefined, env());
88+
expect(on.installIdHeader()).toEqual({ 'X-Codify-Install-Id': on.installId });
89+
90+
expect(new Telemetry(statePath, undefined, env({ disabledByEnv: true })).installIdHeader()).toEqual({});
91+
on.setDisabled(true);
92+
expect(on.installIdHeader()).toEqual({});
93+
});
94+
8695
it('prints events instead of sending them in debug mode', async () => {
8796
const write = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
8897
const fetchImpl = vi.fn();

‎src/telemetry/telemetry.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const TELEMETRY_NOTICE = `Codify collects anonymous usage stats to help i
1111
We never collect your configs, file paths or parameter values.
1212
To opt out, run \`codify telemetry disable\` or set DO_NOT_TRACK=1.`;
1313

14+
export const INSTALL_ID_HEADER = 'X-Codify-Install-Id';
15+
1416
const SEND_TIMEOUT_MS = 3000;
1517
// The API accepts at most this many events per request
1618
const MAX_EVENTS_PER_REQUEST = 50;
@@ -42,7 +44,8 @@ function isSet(name: string): boolean {
4244
* fail, and how `codify prompt` sessions go. Never config contents, parameter values, file paths, error messages or
4345
* prompt text.
4446
*
45-
* The install id is a random UUID in ~/.codify/telemetry.json (deleting the file resets it). The user id is never sent
47+
* The install id is a random UUID in ~/.codify/telemetry.json (deleting the file resets it). It is also sent with the
48+
* plugin version search (`installIdHeader`), which the API tracks. The user id is never sent
4649
* by the CLI: when logged in, the API reads it from the verified access token. Events are sent to the Codify API, which
4750
* forwards them to Mixpanel without the IP address.
4851
*
@@ -88,6 +91,14 @@ export class Telemetry {
8891
return state.installId;
8992
}
9093

94+
/**
95+
* `X-Codify-Install-Id` for API requests the API tracks itself (plugin version search), so those events count per
96+
* install too. Empty when telemetry is off.
97+
*/
98+
installIdHeader(): Record<string, string> {
99+
return this.enabled ? { [INSTALL_ID_HEADER]: this.installId } : {};
100+
}
101+
91102
setDisabled(disabled: boolean): void {
92103
this.readState().disabled = disabled;
93104
if (disabled) {

0 commit comments

Comments
 (0)