@devries-cameron20 / composio Mirror · read-only / commits / 06e97b717
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 18:30 UTC

feat(cli): send telemetry direct to PostHog and stitch install→Apollo identity

The CLI analytics path now delivers events straight to PostHog's public capture API instead of the Apollo /api/v3/cli/analytics endpoint. This removes the base-URL gate that silently dropped every pre-login event, so install / setup / plugin-setup telemetry now reaches PostHog keyed by the anonymous install_id. At login the CLI persists the Apollo org_member.id into ~/.composio/ analytics.json and fires a single $create_alias (install_id -> org_member.id), so post-login events and all later CLI activity resolve to the same PostHog person as web onboarding. distinct_id is now the persisted apollo_user_id when present, else the bare install_id (no more anon_/user_ prefixing). Delivery choice: direct-only (the Apollo send is dropped for CLI events). The detached best-effort worker and all opt-outs are unchanged; no secrets or tool-arg values are sent. The public phc_* project-196278 write key is left empty with a TODO — direct delivery is a safe no-op until the maintainer fills it in. Env overrides: COMPOSIO_POSTHOG_KEY / COMPOSIO_POSTHOG_INGEST_URL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Kshitij Jhunjhunwala committed Jul 22, 2026 at 18:11 UTC 06e97b71794ef128b4cc521e46deeb83480616b6
5 files changed +372 -59
ts/packages/cli/CHANGELOG.md
+4
@@ -1,5 +1,9 @@
1 # @composio/cli
2
3 +## Unreleased
4 +
5 +- Telemetry now delivers directly to PostHog instead of through the Apollo analytics endpoint. This fixes the dark top-of-funnel: pre-login `install`/`setup`/`plugin-setup` events no longer require a logged-in API base URL, so they are captured under the anonymous `install_id`. At login the CLI persists the Apollo `org_member.id` and fires a single `$create_alias`, so post-login events (and all later CLI activity) resolve to the same PostHog person as web onboarding. Existing opt-outs (`COMPOSIO_CLI_TELEMETRY_DISABLED`, `TELEMETRY_DISABLED`, `COMPOSIO_DISABLE_TELEMETRY`, `CI`, `NODE_ENV=test`) are unchanged, and no secrets or tool-argument values are ever sent.
6 +
7 ## 0.3.0
8
9 ### Security
ts/packages/cli/src/analytics/dispatch.ts
+175 -46
@@ -23,9 +23,13 @@ const INTERNAL_CODACT_FAILURE_WORKER_FLAG = '__codact-failure-worker';
23 const COMPOSIO_DIR = '.composio';
24 const ANALYTICS_STATE_FILE_NAME = 'analytics.json';
25 const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
26 -const CLI_ANALYTICS_PATH = '/api/v3/cli/analytics';
26 const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
27
28 +// PostHog's single-event capture endpoint expects `$create_alias` to merge an
29 +// anonymous person into an identified one (`properties.alias` -> `distinct_id`).
30 +const POSTHOG_ALIAS_EVENT = '$create_alias';
31 +const POSTHOG_LIB = 'composio-cli';
32 +
33 export type CliCodactFailureType = 'wrong_tool_slug' | 'wrong_tool_input_param';
34
35 export type CliCodactFailureToolInfo = {
@@ -98,6 +102,24 @@ const analyticsDisabled = environmentProvider.load(
102 )
103 );
104
105 +// Resolves the direct-to-PostHog transport target. Both fields fall back to the
106 +// embedded constants; env overrides exist for tests and staging. Crucially this
107 +// does NOT depend on the Composio API base URL (which is only written at login),
108 +// so pre-login install/setup events can be delivered.
109 +const getPostHogConfig = environmentProvider
110 + .load(
111 + Config.all({
112 + ingestUrl: optionalString('COMPOSIO_POSTHOG_INGEST_URL'),
113 + projectKey: optionalString('COMPOSIO_POSTHOG_KEY'),
114 + })
115 + )
116 + .pipe(
117 + Effect.map(({ ingestUrl, projectKey }) => ({
118 + ingestUrl: configuredString(ingestUrl) ?? constants.COMPOSIO_POSTHOG_INGEST_URL,
119 + projectKey: configuredString(projectKey) ?? constants.COMPOSIO_POSTHOG_PROJECT_KEY,
120 + }))
121 + );
122 +
123 const jsonFromString = Schema.parseJson();
124 const prettyJsonFromString = Schema.parseJson({ space: 2 });
125 const decodeJson = Schema.decodeUnknown(jsonFromString);
@@ -153,10 +175,22 @@ const readOptionalJson = <A>(filePath: string) =>
175 return (yield* decodeJson(raw)) as A;
176 }).pipe(Effect.catchAll(() => Effect.succeed(undefined)));
177
156 -const hashString = (value: string): string => djb2Hash(value).toString(16).padStart(8, '0');
178 +// Persisted `~/.composio/analytics.json`. `install_id` is the anonymous device
179 +// UUID; `apollo_user_id` (the Apollo `org_member.id`, persisted at login) becomes
180 +// the PostHog identity so CLI events join the same person as web onboarding.
181 +type AnalyticsState = {
182 + readonly install_id?: string;
183 + readonly apollo_user_id?: string;
184 + readonly created_at?: string;
185 +};
186
187 const makeInstallId = Effect.sync(() => crypto.randomUUID());
188
189 +const readAnalyticsState = Effect.gen(function* () {
190 + const paths = yield* getAnalyticsPaths;
191 + return yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
192 +});
193 +
194 const getOrCreateInstallId = Effect.gen(function* () {
195 const fs = yield* FileSystem.FileSystem;
196 const paths = yield* getAnalyticsPaths;
@@ -165,7 +199,7 @@ const getOrCreateInstallId = Effect.gen(function* () {
199 .makeDirectory(paths.analyticsDir, { recursive: true })
200 .pipe(Effect.catchAll(() => Effect.void));
201
168 - const state = yield* readOptionalJson<{ install_id?: unknown }>(paths.analyticsStatePath);
202 + const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
203 if (typeof state?.install_id === 'string' && state.install_id.length > 0) {
204 return state.install_id;
205 }
@@ -173,6 +207,7 @@ const getOrCreateInstallId = Effect.gen(function* () {
207 const installId = yield* makeInstallId;
208 const createdAt = DateTime.formatIso(yield* DateTime.now);
209 const contents = yield* encodeJson({
210 + ...(state ?? {}),
211 install_id: installId,
212 created_at: createdAt,
213 });
@@ -180,6 +215,35 @@ const getOrCreateInstallId = Effect.gen(function* () {
215 return installId;
216 }).pipe(Effect.catchAll(() => makeInstallId));
217
218 +const readApolloUserId = Effect.map(readAnalyticsState, state =>
219 + typeof state?.apollo_user_id === 'string' && state.apollo_user_id.length > 0
220 + ? state.apollo_user_id
221 + : null
222 +);
223 +
224 +// Persists the Apollo `org_member.id` into the analytics state, preserving the
225 +// existing `install_id`/`created_at` so the anonymous device identity is kept
226 +// for the login-time alias. Best-effort: never surfaces or throws.
227 +const persistApolloUserId = (installId: string, apolloUserId: string) =>
228 + Effect.gen(function* () {
229 + const fs = yield* FileSystem.FileSystem;
230 + const paths = yield* getAnalyticsPaths;
231 +
232 + yield* fs
233 + .makeDirectory(paths.analyticsDir, { recursive: true })
234 + .pipe(Effect.catchAll(() => Effect.void));
235 +
236 + const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
237 + const createdAt = state?.created_at ?? DateTime.formatIso(yield* DateTime.now);
238 + const contents = yield* encodeJson({
239 + ...(state ?? {}),
240 + install_id: state?.install_id ?? installId,
241 + apollo_user_id: apolloUserId,
242 + created_at: createdAt,
243 + });
244 + yield* fs.writeFileString(paths.analyticsStatePath, contents);
245 + }).pipe(Effect.catchAll(() => Effect.void));
246 +
247 const readUserConfig = Effect.gen(function* () {
248 const paths = yield* getAnalyticsPaths;
249 return yield* readOptionalJson<{ api_key?: unknown; base_url?: unknown }>(paths.userConfigPath);
@@ -199,10 +263,12 @@ const getUserApiKey = Effect.gen(function* () {
263 : null;
264 });
265
266 +// Post-login events key on the Apollo `org_member.id` (persisted at login) so
267 +// they resolve to the same PostHog person as web onboarding. Pre-login, the raw
268 +// `install_id` is the distinct id (NOT wrapped as `anon_<id>` — the login-time
269 +// `$create_alias` merges this exact value into the Apollo person).
270 const getDistinctId = (installId: string) =>
203 - Effect.map(getUserApiKey, userApiKey =>
204 - userApiKey ? `user_${hashString(userApiKey)}` : `anon_${installId}`
205 - );
271 + Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
272
273 const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);
274
@@ -256,10 +322,6 @@ export const readApiBaseUrl = Effect.gen(function* () {
322 : null;
323 }).pipe(Effect.catchAllCause(() => Effect.succeed(null)));
324
259 -const getAnalyticsEndpoint = Effect.map(readApiBaseUrl, baseUrl =>
260 - baseUrl ? `${baseUrl}${CLI_ANALYTICS_PATH}` : null
261 -);
262 -
325 const getCliCodactFailuresEndpoint = Effect.map(readApiBaseUrl, baseUrl =>
326 baseUrl ? `${baseUrl}${CLI_CODACT_FAILURES_PATH}` : null
327 );
@@ -298,40 +360,57 @@ const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
360 );
361 });
362
301 -const captureToComposioAnalytics = (envelope: AnalyticsEnvelope) =>
363 +// Shapes an analytics envelope into a PostHog single-event capture body. The
364 +// public project write key authenticates the ingest in-band (no user API key),
365 +// which is what lets pre-login events send. Only allowlisted, already-shaped
366 +// properties from the event builders flow through here — no secrets or tool-arg
367 +// values (see events.ts).
368 +const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
369 + api_key: projectKey,
370 + event: envelope.event,
371 + distinct_id: envelope.distinctId,
372 + timestamp: envelope.sentAt,
373 + properties: {
374 + ...(envelope.properties ?? {}),
375 + install_id: envelope.installId,
376 + $lib: POSTHOG_LIB,
377 + $lib_version: constants.APP_VERSION,
378 + },
379 +});
380 +
381 +const captureToPostHog = (envelope: AnalyticsEnvelope) =>
382 Effect.gen(function* () {
303 - const endpoint = yield* getAnalyticsEndpoint;
383 const disabled = yield* analyticsDisabled;
305 - if (!endpoint || disabled) {
306 - yield* telemetryDebugLog('delivery_skipped', {
307 - reason: disabled ? 'disabled' : 'missing_endpoint',
308 - endpoint,
384 + const { ingestUrl, projectKey } = yield* getPostHogConfig;
385 + if (disabled || projectKey.length === 0) {
386 + yield* telemetryDebugLog('posthog_delivery_skipped', {
387 + reason: disabled ? 'disabled' : 'missing_project_key',
388 + endpoint: ingestUrl,
389 eventName: envelope.event,
390 });
391 return;
392 }
393
394 const httpClient = yield* HttpClient.HttpClient;
315 - const userApiKey = yield* getUserApiKey;
316 - const request = yield* HttpClientRequest.post(endpoint).pipe(
317 - HttpClientRequest.setHeaders({
318 - 'x-composio-analytics-source': 'cli',
319 - ...(userApiKey ? { 'x-user-api-key': userApiKey } : {}),
320 - }),
321 - HttpClientRequest.bodyJson(envelope)
395 + const request = yield* HttpClientRequest.post(ingestUrl).pipe(
396 + HttpClientRequest.setHeader('x-composio-analytics-source', 'cli'),
397 + HttpClientRequest.bodyJson(buildPostHogBody(envelope, projectKey))
398 );
399 const response = yield* httpClient.execute(request);
400 const responseOk = response.status >= 200 && response.status < 300;
401 const debugEnabled = yield* telemetryDebugEnabled;
402 const responseBody = !responseOk && debugEnabled ? yield* response.text : undefined;
403
328 - yield* telemetryDebugLog(responseOk ? 'delivery_succeeded' : 'delivery_failed', {
329 - endpoint,
330 - eventName: envelope.event,
331 - status: response.status,
332 - ok: responseOk,
333 - responseBody: responseBody?.slice(0, 1000),
334 - });
404 + yield* telemetryDebugLog(
405 + responseOk ? 'posthog_delivery_succeeded' : 'posthog_delivery_failed',
406 + {
407 + endpoint: ingestUrl,
408 + eventName: envelope.event,
409 + status: response.status,
410 + ok: responseOk,
411 + responseBody: responseBody?.slice(0, 1000),
412 + }
413 + );
414 });
415
416 type CliCodactFailureBody = {
@@ -425,20 +504,32 @@ const captureToComposioCodactFailures = (failure: CliCodactFailure) =>
504 });
505 });
506
507 +// Encodes an envelope and hands it to the detached best-effort worker, which
508 +// delivers it directly to PostHog. Shared by event tracking and login aliasing.
509 +const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
510 + Effect.gen(function* () {
511 + yield* telemetryDebugLog('enqueue', { envelope });
512 + const serializedEnvelope = yield* encodeJson(envelope);
513 + const encodedPayload = Encoding.encodeBase64Url(serializedEnvelope);
514 + const { command, args } = yield* getWorkerSpawnArgs(
515 + INTERNAL_ANALYTICS_WORKER_FLAG,
516 + encodedPayload
517 + );
518 + yield* spawnWorker(command, args);
519 + });
520 +
521 export const trackCliEventEffect = (event: TrackEvent) =>
522 Effect.gen(function* () {
523 if (!event) {
524 return;
525 }
526
434 - const endpoint = yield* getAnalyticsEndpoint;
527 + // The direct PostHog transport carries a fixed endpoint + embedded public
528 + // key, so — unlike the old Apollo path — there is no base-URL gate here and
529 + // pre-login install/setup events are delivered.
530 const disabled = yield* analyticsDisabled;
436 - if (disabled || !endpoint) {
437 - yield* telemetryDebugLog('skip', {
438 - reason: disabled ? 'disabled' : 'missing_endpoint',
439 - eventName: event.name,
440 - endpoint,
441 - });
531 + if (disabled) {
532 + yield* telemetryDebugLog('skip', { reason: 'disabled', eventName: event.name });
533 return;
534 }
535
@@ -459,14 +550,52 @@ export const trackCliEventEffect = (event: TrackEvent) =>
550 distinctId,
551 installId,
552 };
462 - yield* telemetryDebugLog('enqueue', { endpoint, envelope });
463 - const serializedEnvelope = yield* encodeJson(envelope);
464 - const encodedPayload = Encoding.encodeBase64Url(serializedEnvelope);
465 - const { command, args } = yield* getWorkerSpawnArgs(
466 - INTERNAL_ANALYTICS_WORKER_FLAG,
467 - encodedPayload
468 - );
469 - yield* spawnWorker(command, args);
553 + yield* enqueuePostHogEnvelope(envelope);
554 + }).pipe(Effect.catchAllCause(() => Effect.void));
555 +
556 +// Fires a single PostHog `$create_alias` merging the anonymous `install_id`
557 +// device person into the identified Apollo `org_member.id` person. Best-effort.
558 +export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
559 + Effect.gen(function* () {
560 + const disabled = yield* analyticsDisabled;
561 + if (disabled) {
562 + yield* telemetryDebugLog('alias_skip', { reason: 'disabled', installId });
563 + return;
564 + }
565 +
566 + const sentAt = DateTime.formatIso(yield* DateTime.now);
567 + const envelope: AnalyticsEnvelope = {
568 + event: POSTHOG_ALIAS_EVENT,
569 + properties: {
570 + // PostHog merges `properties.alias` into the event's `distinct_id`.
571 + distinct_id: apolloUserId,
572 + alias: installId,
573 + cli_version: constants.APP_VERSION,
574 + },
575 + sentAt,
576 + source: 'cli',
577 + distinctId: apolloUserId,
578 + installId,
579 + };
580 + yield* enqueuePostHogEnvelope(envelope);
581 + }).pipe(Effect.catchAllCause(() => Effect.void));
582 +
583 +// Called at login: persists the Apollo `org_member.id` as the analytics identity
584 +// and stitches the pre-login anonymous device into that person via one alias.
585 +// Fully non-fatal so login never fails on telemetry.
586 +export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
587 + Effect.gen(function* () {
588 + if (typeof apolloUserId !== 'string' || apolloUserId.trim().length === 0) {
589 + return;
590 + }
591 +
592 + const disabled = yield* analyticsDisabled;
593 + const installId = yield* getOrCreateInstallId;
594 + yield* persistApolloUserId(installId, apolloUserId);
595 + if (disabled) {
596 + return;
597 + }
598 + yield* emitPostHogAlias(installId, apolloUserId);
599 }).pipe(Effect.catchAllCause(() => Effect.void));
600
601 export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
@@ -523,7 +652,7 @@ const runAnalyticsWorker = (argv: ReadonlyArray<string>) => {
652 if (typeof envelope?.event !== 'string' || envelope.event.length === 0) {
653 return true;
654 }
526 - yield* captureToComposioAnalytics(envelope);
655 + yield* captureToPostHog(envelope);
656 return true;
657 }).pipe(
658 Effect.catchAllCause(cause =>
ts/packages/cli/src/commands/login.cmd.ts
+10
@@ -18,6 +18,7 @@ import { setupCacheDir } from 'src/effects/setup-cache-dir';
18 import { primeConsumerConnectedToolkitsCacheInBackground } from 'src/services/consumer-short-term-cache';
19 import { inferSkillReleaseChannel, installSkillSafe } from 'src/effects/install-skill';
20 import { handleAgentAuthError } from 'src/effects/handle-agent-auth-error';
21 +import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
22 import { APP_VERSION } from 'src/constants';
23 import {
24 ensureAgentSignupAllowed,
@@ -323,6 +324,9 @@ const directLogin = (params: { userApiKey: string; org?: string }) =>
324 userApiKey: params.userApiKey,
325 });
326
327 + // Stitch the anonymous pre-login install into the Apollo person (non-fatal).
328 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
329 +
330 const selectedOrg = yield* resolveDirectLoginOrganization({
331 apiKey: params.userApiKey,
332 baseURL: ctx.data.baseURL,
@@ -506,6 +510,9 @@ const loginWithKey = (params: {
510 userApiKey: uakApiKey,
511 });
512
513 + // Stitch the anonymous pre-login install into the Apollo person (non-fatal).
514 + yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
515 +
516 const organizations = params.defaultToFirstOrg
517 ? yield* listOrganizations({
518 baseURL: ctx.data.baseURL,
@@ -712,6 +719,9 @@ export const browserLogin = (params: {
719 userApiKey: uakApiKey,
720 });
721
722 + // Stitch the anonymous pre-login install into the Apollo person (non-fatal).
723 + yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
724 +
725 // e.g., "pr_xlSR6oN5jIlk"
726 const xProjectId = uakSessionInfo.project.nano_id;
727 // e.g., "k2OiqRLMdHyM"
ts/packages/cli/src/constants.ts
+24
@@ -77,6 +77,30 @@ export const PROJECT_ENV_FILE_NAME = '.env';
77 */
78 export const PROJECT_COMPOSIO_DIR = '.composio';
79
80 +/**
81 + * PostHog ingest endpoint the CLI sends telemetry directly to.
82 + *
83 + * This is the public single-event capture API. It does not depend on the
84 + * Composio API base URL, so pre-login install/setup events can be delivered
85 + * before the user has ever authenticated. Overridable via the
86 + * `COMPOSIO_POSTHOG_INGEST_URL` environment variable (used by tests / staging).
87 + */
88 +export const COMPOSIO_POSTHOG_INGEST_URL = 'https://us.i.posthog.com/i/v0/e/';
89 +
90 +/**
91 + * Public, write-only PostHog project write key for project 196278
92 + * ("Composio Platform"). This is client-embeddable by design (same trust model
93 + * as the dashboard JS bundle): it can only ingest events, never read them.
94 + *
95 + * Overridable via the `COMPOSIO_POSTHOG_KEY` environment variable.
96 + *
97 + * TODO(kj): fill in the real public `phc_*` project-196278 write key from the
98 + * PostHog project settings. While this is empty, direct PostHog delivery is a
99 + * safe no-op (the transport skips when no key is configured). Do NOT commit a
100 + * real token into tests or fixtures.
101 + */
102 +export const COMPOSIO_POSTHOG_PROJECT_KEY = '';
103 +
104 /**
105 * GitHub repository information for release fetching
106 */
ts/packages/cli/test/src/analytics.dispatch.test.ts
+159 -13
@@ -5,12 +5,15 @@ import { BunFileSystem, BunPath } from '@effect/platform-bun';
5 import { Effect, Layer } from 'effect';
6 import * as tempy from 'tempy';
7 import {
8 + emitPostHogAlias,
9 getCurrentCwdSessionId,
10 + linkApolloIdentityForAnalytics,
11 readApiBaseUrl,
12 runBackgroundWorkerFromArgv,
13 trackCliCodactFailureEffect,
14 trackCliEventEffect,
15 } from 'src/analytics/dispatch';
16 +import { CLI_ANALYTICS_EVENTS } from 'src/analytics/events';
17 import { APP_VERSION, USER_CONFIG_FILE_NAME } from 'src/constants';
18 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
19 import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
@@ -149,29 +152,29 @@ describe('CLI analytics dispatch', () => {
152 ]).pipe(Effect.provide(makePlatformLayer(home)));
153 });
154
152 - it.effect('delivers analytics worker payloads to the configured endpoint', () => {
155 + it.effect('delivers analytics worker payloads directly to PostHog', () => {
156 const home = tempy.temporaryDirectory();
157 const envelope = {
158 event: 'cli_command_invoked',
159 + properties: { cli_version: '9.9.9' },
160 sentAt: '2026-07-16T00:00:00.000Z',
161 source: 'cli' as const,
158 - distinctId: 'anon_test',
162 + distinctId: 'install_test',
163 installId: 'install_test',
164 };
165 const encodedPayload = btoa(JSON.stringify(envelope))
166 .replace(/\+/gu, '-')
167 .replace(/\//gu, '_')
168 .replace(/=+$/u, '');
165 - vi.stubEnv('COMPOSIO_BASE_URL', 'https://backend.example.test');
166 - vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_test');
167 - vi.stubEnv('NODE_ENV', 'development');
168 - vi.stubEnv('CI', 'false');
169 - vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DISABLED', 'false');
170 - vi.stubEnv('TELEMETRY_DISABLED', 'false');
171 - vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'false');
169 + enableTelemetry();
170 + // Direct-to-PostHog transport does NOT read the Composio base URL. Overriding
171 + // the ingest URL + a fake public key keeps the test off the real project.
172 + vi.stubEnv('COMPOSIO_BASE_URL', '');
173 + vi.stubEnv('COMPOSIO_POSTHOG_INGEST_URL', 'https://posthog.example.test/i/v0/e/');
174 + vi.stubEnv('COMPOSIO_POSTHOG_KEY', 'phc_test_key');
175 const fetchSpy = vi
176 .spyOn(globalThis, 'fetch')
174 - .mockResolvedValue(new Response(null, { status: 204 }));
177 + .mockResolvedValue(new Response(null, { status: 200 }));
178
179 return Effect.gen(function* () {
180 yield* runBackgroundWorkerFromArgv([
@@ -183,16 +186,60 @@ describe('CLI analytics dispatch', () => {
186
187 expect(fetchSpy).toHaveBeenCalledTimes(1);
188 const [endpoint, request] = fetchSpy.mock.calls[0]!;
186 - expect(String(endpoint)).toBe('https://backend.example.test/api/v3/cli/analytics');
189 + expect(String(endpoint)).toBe('https://posthog.example.test/i/v0/e/');
190 expect(request).toMatchObject({
191 method: 'POST',
192 headers: expect.objectContaining({
193 'content-type': 'application/json',
194 'x-composio-analytics-source': 'cli',
192 - 'x-user-api-key': 'uak_test',
195 }),
196 });
195 - expect(JSON.parse(new TextDecoder().decode(request?.body as Uint8Array))).toEqual(envelope);
197 + // The public write key authenticates in-band; no user API key is attached.
198 + expect((request?.headers as Record<string, string>)['x-user-api-key']).toBeUndefined();
199 + const body = JSON.parse(new TextDecoder().decode(request?.body as Uint8Array));
200 + expect(body).toMatchObject({
201 + api_key: 'phc_test_key',
202 + event: 'cli_command_invoked',
203 + distinct_id: 'install_test',
204 + timestamp: '2026-07-16T00:00:00.000Z',
205 + properties: expect.objectContaining({
206 + cli_version: '9.9.9',
207 + install_id: 'install_test',
208 + $lib: 'composio-cli',
209 + }),
210 + });
211 + });
212 + });
213 +
214 + it.effect('skips PostHog delivery when no project key is configured', () => {
215 + const home = tempy.temporaryDirectory();
216 + const envelope = {
217 + event: 'cli_command_invoked',
218 + sentAt: '2026-07-16T00:00:00.000Z',
219 + source: 'cli' as const,
220 + distinctId: 'install_test',
221 + installId: 'install_test',
222 + };
223 + const encodedPayload = btoa(JSON.stringify(envelope))
224 + .replace(/\+/gu, '-')
225 + .replace(/\//gu, '_')
226 + .replace(/=+$/u, '');
227 + enableTelemetry();
228 + // No COMPOSIO_POSTHOG_KEY override -> falls back to the empty embedded
229 + // placeholder, so delivery is a safe no-op until the real key is filled in.
230 + const fetchSpy = vi
231 + .spyOn(globalThis, 'fetch')
232 + .mockResolvedValue(new Response(null, { status: 200 }));
233 +
234 + return Effect.gen(function* () {
235 + yield* runBackgroundWorkerFromArgv([
236 + process.execPath,
237 + 'composio',
238 + '__analytics-worker',
239 + encodedPayload,
240 + ]).pipe(Effect.provide(makePlatformLayer(home)));
241 +
242 + expect(fetchSpy).not.toHaveBeenCalled();
243 });
244 });
245
@@ -319,11 +366,110 @@ describe('CLI analytics dispatch', () => {
366 failureType: 'wrong_tool_slug',
367 ctx: { slug: 'MISSING_TOOL' },
368 });
369 + yield* emitPostHogAlias('install_disabled', 'apollo_user_disabled');
370 + yield* linkApolloIdentityForAnalytics('apollo_user_disabled');
371
372 expect(childProcessMocks.spawn).not.toHaveBeenCalled();
373 }).pipe(Effect.provide(makePlatformLayer(home)));
374 });
375
376 + it.effect('keys pre-login events on the bare install_id', () => {
377 + const home = tempy.temporaryDirectory();
378 + const scriptPath = `${home}/composio.ts`;
379 + // No persisted apollo_user_id and no user API key -> anonymous device identity.
380 + enableTelemetry('');
381 + process.argv[1] = scriptPath;
382 +
383 + return Effect.gen(function* () {
384 + const fs = yield* FileSystem.FileSystem;
385 + yield* fs.writeFileString(scriptPath, '');
386 + yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED });
387 +
388 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
389 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
390 + const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
391 + // Pre-login, distinct_id is the raw install_id (not `anon_<id>`), so the
392 + // login-time alias can merge this exact person into the Apollo user.
393 + expect(payload.installId.length).toBeGreaterThan(0);
394 + expect(payload.distinctId).toBe(payload.installId);
395 + }).pipe(Effect.provide(makePlatformLayer(home)));
396 + });
397 +
398 + it.effect('keys post-login events on the persisted apollo_user_id', () => {
399 + const home = tempy.temporaryDirectory();
400 + const scriptPath = `${home}/composio.ts`;
401 + enableTelemetry('');
402 + process.argv[1] = scriptPath;
403 +
404 + return Effect.gen(function* () {
405 + const fs = yield* FileSystem.FileSystem;
406 + const path = yield* Path.Path;
407 + yield* fs.writeFileString(scriptPath, '');
408 + const composioDir = path.join(home, '.composio');
409 + yield* fs.makeDirectory(composioDir, { recursive: true });
410 + yield* fs.writeFileString(
411 + path.join(composioDir, 'analytics.json'),
412 + JSON.stringify({ install_id: 'install_persisted', apollo_user_id: 'om_apollo_123' })
413 + );
414 +
415 + yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED });
416 +
417 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
418 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
419 + const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
420 + expect(payload.distinctId).toBe('om_apollo_123');
421 + expect(payload.installId).toBe('install_persisted');
422 + }).pipe(Effect.provide(makePlatformLayer(home)));
423 + });
424 +
425 + it.effect('persists apollo_user_id and emits exactly one $create_alias at login', () => {
426 + const home = tempy.temporaryDirectory();
427 + const scriptPath = `${home}/composio.ts`;
428 + enableTelemetry('');
429 + process.argv[1] = scriptPath;
430 +
431 + return Effect.gen(function* () {
432 + const fs = yield* FileSystem.FileSystem;
433 + const path = yield* Path.Path;
434 + yield* fs.writeFileString(scriptPath, '');
435 + const composioDir = path.join(home, '.composio');
436 + yield* fs.makeDirectory(composioDir, { recursive: true });
437 + yield* fs.writeFileString(
438 + path.join(composioDir, 'analytics.json'),
439 + JSON.stringify({ install_id: 'install_login', created_at: '2026-01-01T00:00:00.000Z' })
440 + );
441 +
442 + yield* linkApolloIdentityForAnalytics('om_apollo_login');
443 +
444 + // Exactly one alias event is spawned.
445 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
446 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
447 + const payload = decodeWorkerPayload<{
448 + event: string;
449 + distinctId: string;
450 + installId: string;
451 + properties: { distinct_id: string; alias: string };
452 + }>(args[2]!);
453 + expect(payload.event).toBe('$create_alias');
454 + expect(payload.distinctId).toBe('om_apollo_login');
455 + expect(payload.installId).toBe('install_login');
456 + expect(payload.properties).toMatchObject({
457 + distinct_id: 'om_apollo_login',
458 + alias: 'install_login',
459 + });
460 +
461 + // The Apollo id is persisted while keeping the original install_id/created_at.
462 + const persisted = JSON.parse(
463 + yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8')
464 + ) as { install_id: string; apollo_user_id: string; created_at: string };
465 + expect(persisted).toMatchObject({
466 + install_id: 'install_login',
467 + apollo_user_id: 'om_apollo_login',
468 + created_at: '2026-01-01T00:00:00.000Z',
469 + });
470 + }).pipe(Effect.provide(makePlatformLayer(home)));
471 + });
472 +
473 it.effect('delivers codact worker failures with the user key and session body', () => {
474 const home = tempy.temporaryDirectory();
475 const failureBody = {