100
)
101
);
102
103
-// Direct-to-PostHog target; env overrides fall back to the embedded constants.
104
-// No dependency on the API base URL, so pre-login events can send.
103
const getPostHogConfig = environmentProvider
104
.load(
105
Config.all({
169
return (yield* decodeJson(raw)) as A;
170
}).pipe(Effect.catchAll(() => Effect.succeed(undefined)));
171
174
-// `install_id` = anonymous device UUID; `apollo_user_id` = Apollo `org_member.id`
175
-// persisted at login, used as the PostHog identity post-login.
172
type AnalyticsState = {
173
readonly install_id?: string;
174
readonly apollo_user_id?: string;
175
+ // Tracked separately from apollo_user_id so an alias that was never enqueued
176
+ // (no project key, telemetry off) is retried instead of blocked forever.
177
+ readonly aliased_apollo_user_id?: string;
178
readonly created_at?: string;
179
};
180
215
: null
216
);
217
219
-// Persists the Apollo `org_member.id`, preserving `install_id`/`created_at`.
220
-const persistApolloUserId = (installId: string, apolloUserId: string) =>
218
+const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>) =>
219
Effect.gen(function* () {
220
const fs = yield* FileSystem.FileSystem;
221
const paths = yield* getAnalyticsPaths;
225
.pipe(Effect.catchAll(() => Effect.void));
226
227
const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
230
- const createdAt = state?.created_at ?? DateTime.formatIso(yield* DateTime.now);
228
const contents = yield* encodeJson({
229
...(state ?? {}),
230
install_id: state?.install_id ?? installId,
234
- apollo_user_id: apolloUserId,
235
- created_at: createdAt,
231
+ created_at: state?.created_at ?? DateTime.formatIso(yield* DateTime.now),
232
+ ...patch,
233
});
234
yield* fs.writeFileString(paths.analyticsStatePath, contents);
235
}).pipe(Effect.catchAll(() => Effect.void));
253
: null;
254
});
255
259
-// Post-login: the Apollo `org_member.id`. Pre-login: the raw `install_id` (the
260
-// login-time `$create_alias` merges this exact value into the Apollo person).
256
const getDistinctId = (installId: string) =>
257
Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
258
346
);
347
});
348
354
-// Shapes an envelope into a PostHog capture body; the public project key
355
-// authenticates the ingest in-band (no user API key attached).
349
const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
350
api_key: projectKey,
351
event: envelope.event,
485
});
486
});
487
495
-// Hands an envelope to the detached best-effort worker for PostHog delivery.
496
-// Short-circuits when no project key is configured (the default for OSS forks /
497
-// local builds) so we never spawn a worker that would only no-op.
488
const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
489
Effect.gen(function* () {
490
const { projectKey } = yield* getPostHogConfig;
493
reason: 'missing_project_key',
494
eventName: envelope.event,
495
});
506
- return;
496
+ return false;
497
}
498
499
yield* telemetryDebugLog('enqueue', { envelope });
504
encodedPayload
505
);
506
yield* spawnWorker(command, args);
507
+ return true;
508
});
509
510
export const trackCliEventEffect = (event: TrackEvent) =>
539
yield* enqueuePostHogEnvelope(envelope);
540
}).pipe(Effect.catchAllCause(() => Effect.void));
541
551
-// Fires one `$create_alias` merging the `install_id` device into the Apollo person.
542
export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
543
Effect.gen(function* () {
544
const disabled = yield* analyticsDisabled;
545
if (disabled) {
546
yield* telemetryDebugLog('alias_skip', { reason: 'disabled', installId });
557
- return;
547
+ return false;
548
}
549
550
const sentAt = DateTime.formatIso(yield* DateTime.now);
561
distinctId: apolloUserId,
562
installId,
563
};
574
- yield* enqueuePostHogEnvelope(envelope);
575
- }).pipe(Effect.catchAllCause(() => Effect.void));
576
-
577
-// Called at login: persist the Apollo identity and, on first association, alias
578
-// the anonymous install into it. Non-fatal so login never fails on telemetry.
564
+ return yield* enqueuePostHogEnvelope(envelope);
565
+ }).pipe(Effect.catchAllCause(() => Effect.succeed(false)));
566
+
567
+/**
568
+ * Records the Apollo `org_member.id` as the analytics identity and stitches the
569
+ * anonymous install into it. Safe to call from any command that already resolved
570
+ * a session, which is how already-authenticated installs get backfilled.
571
+ */
572
export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
573
Effect.gen(function* () {
574
const resolved = typeof apolloUserId === 'string' ? apolloUserId.trim() : '';
576
return;
577
}
578
586
- // Disabled telemetry writes nothing — not even the local identity file.
579
const disabled = yield* analyticsDisabled;
580
if (disabled) {
581
yield* telemetryDebugLog('alias_skip', { reason: 'disabled' });
582
return;
583
}
584
593
- const existing = yield* readApolloUserId;
585
+ const state = yield* readAnalyticsState;
586
const installId = yield* getOrCreateInstallId;
595
- yield* persistApolloUserId(installId, resolved);
596
- // Alias only the first time this install is tied to an Apollo user: a repeat
597
- // login is redundant, and PostHog rejects re-aliasing one install into a
598
- // second account, so skip once an id has already been persisted.
599
- if (existing === null) {
600
- yield* emitPostHogAlias(installId, resolved);
587
+ if (state?.apollo_user_id !== resolved) {
588
+ yield* mergeAnalyticsState(installId, { apollo_user_id: resolved });
589
+ }
590
+
591
+ if (state?.aliased_apollo_user_id === resolved) {
592
+ return;
593
+ }
594
+ if (yield* emitPostHogAlias(installId, resolved)) {
595
+ yield* mergeAnalyticsState(installId, { aliased_apollo_user_id: resolved });
596
}
597
}).pipe(Effect.catchAllCause(() => Effect.void));
598
599
+/** Drops the Apollo identity on logout so later events revert to `install_id`. */
600
+export const clearApolloIdentityForAnalytics = Effect.gen(function* () {
601
+ const installId = yield* getOrCreateInstallId;
602
+ yield* mergeAnalyticsState(installId, { apollo_user_id: undefined });
603
+}).pipe(Effect.catchAllCause(() => Effect.void));
604
+
605
export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
606
Effect.gen(function* () {
607
const endpoint = yield* getCliCodactFailuresEndpoint;