mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-10 08:05:14 +02:00
refactor(telemetry): drop no-op groupIdentify, rely on capture groups field
This commit is contained in:
parent
05265b19dd
commit
db7757df32
2 changed files with 0 additions and 74 deletions
|
|
@ -2,14 +2,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|||
|
||||
import {
|
||||
__resetTelemetryEmitterForTests,
|
||||
groupIdentifyProject,
|
||||
shutdownTelemetryEmitter,
|
||||
trackTelemetryEvent,
|
||||
} from './emitter.js';
|
||||
import type { BuiltTelemetryEvent } from './events.js';
|
||||
|
||||
const captures: unknown[] = [];
|
||||
const groupIdentifies: unknown[] = [];
|
||||
const shutdown = vi.fn(async () => {});
|
||||
|
||||
function liveConfigId(): string {
|
||||
|
|
@ -20,7 +18,6 @@ vi.mock('posthog-node', () => ({
|
|||
PostHog: vi.fn().mockImplementation(function () {
|
||||
return {
|
||||
capture: (event: unknown) => captures.push(event),
|
||||
groupIdentify: (event: unknown) => groupIdentifies.push(event),
|
||||
shutdown,
|
||||
};
|
||||
}),
|
||||
|
|
@ -50,7 +47,6 @@ function commandEvent(): BuiltTelemetryEvent<'command'> {
|
|||
describe('telemetry emitter', () => {
|
||||
beforeEach(() => {
|
||||
captures.length = 0;
|
||||
groupIdentifies.length = 0;
|
||||
shutdown.mockClear();
|
||||
__resetTelemetryEmitterForTests();
|
||||
});
|
||||
|
|
@ -88,29 +84,6 @@ describe('telemetry emitter', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('group-identifies once per project when live config is supplied', async () => {
|
||||
await groupIdentifyProject({
|
||||
distinctId: 'install-1',
|
||||
projectId: 'project-1',
|
||||
projectApiKey: liveConfigId(),
|
||||
host: 'https://us.i.posthog.com',
|
||||
});
|
||||
await groupIdentifyProject({
|
||||
distinctId: 'install-1',
|
||||
projectId: 'project-1',
|
||||
projectApiKey: liveConfigId(),
|
||||
host: 'https://us.i.posthog.com',
|
||||
});
|
||||
|
||||
expect(groupIdentifies).toEqual([
|
||||
{
|
||||
groupType: 'project',
|
||||
groupKey: 'project-1',
|
||||
distinctId: 'install-1',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('captures with distinctId, properties, and groups when live config is supplied', async () => {
|
||||
await trackTelemetryEvent({
|
||||
event: commandEvent(),
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ type PostHogClient = {
|
|||
properties: Record<string, unknown>;
|
||||
groups?: Record<string, string>;
|
||||
}): void;
|
||||
groupIdentify(event: { groupType: string; groupKey: string; distinctId?: string }): void;
|
||||
shutdown(): Promise<void> | void;
|
||||
};
|
||||
|
||||
|
|
@ -26,7 +25,6 @@ const POSTHOG_HOST = 'https://us.i.posthog.com';
|
|||
const SHUTDOWN_TIMEOUT_MS = 1500;
|
||||
|
||||
let clientPromise: Promise<PostHogClient | null> | undefined;
|
||||
const identifiedProjects = new Set<string>();
|
||||
|
||||
function telemetryHost(env: TelemetryEmitterEnv, explicitHost?: string): string {
|
||||
return explicitHost ?? env.KTX_TELEMETRY_ENDPOINT ?? POSTHOG_HOST;
|
||||
|
|
@ -72,40 +70,6 @@ function writeDebugPayload(input: {
|
|||
);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function groupIdentifyProject(input: {
|
||||
distinctId: string;
|
||||
projectId: string;
|
||||
env?: TelemetryEmitterEnv;
|
||||
projectApiKey?: string;
|
||||
host?: string;
|
||||
}): Promise<void> {
|
||||
const env = input.env ?? process.env;
|
||||
const projectApiKey = telemetryProjectApiKey(input.projectApiKey);
|
||||
const host = telemetryHost(env, input.host);
|
||||
const projectKey = `${host}:${input.projectId}`;
|
||||
|
||||
if (identifiedProjects.has(projectKey)) {
|
||||
return;
|
||||
}
|
||||
identifiedProjects.add(projectKey);
|
||||
|
||||
const client = await getPostHogClient(projectApiKey, host);
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
client.groupIdentify({
|
||||
groupType: 'project',
|
||||
groupKey: input.projectId,
|
||||
distinctId: input.distinctId,
|
||||
});
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function trackTelemetryEvent(input: {
|
||||
event: BuiltTelemetryEvent;
|
||||
distinctId: string;
|
||||
|
|
@ -130,16 +94,6 @@ export async function trackTelemetryEvent(input: {
|
|||
}
|
||||
|
||||
try {
|
||||
if (input.projectId) {
|
||||
await groupIdentifyProject({
|
||||
distinctId: input.distinctId,
|
||||
projectId: input.projectId,
|
||||
env,
|
||||
projectApiKey,
|
||||
host,
|
||||
});
|
||||
}
|
||||
|
||||
client.capture({
|
||||
distinctId: input.distinctId,
|
||||
event: input.event.name,
|
||||
|
|
@ -168,5 +122,4 @@ export async function shutdownTelemetryEmitter(): Promise<void> {
|
|||
/** @internal */
|
||||
export function __resetTelemetryEmitterForTests(): void {
|
||||
clientPromise = undefined;
|
||||
identifiedProjects.clear();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue