Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/lib/command-framework/apify-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B

static hiddenAliases?: string[];

protected telemetryData: TrackEventMap[`cli_command_${string}`] = {} as never;
protected telemetryData: TrackEventMap['cli_command'] = {} as never;

protected flags!: InferFlagsFromCommand<T['flags']>;

Expand Down Expand Up @@ -413,10 +413,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B

this.telemetryData.wasRetried = await checkAndUpdateLastCommand(this.commandString);

await trackEvent(
`cli_command_${this.commandString.replaceAll(' ', '_').toLowerCase()}` as const,
this.telemetryData,
);
await trackEvent('cli_command', this.telemetryData);
}
}
}
Expand Down
72 changes: 37 additions & 35 deletions src/lib/hooks/telemetry/trackEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@ const SEGMENT_API_URL = 'https://api.segment.io/v1/track';

const SEGMENT_WRITE_KEY = metadata.isBeta ? 'rT67mFpIQD5qS9bJBoIYSFbZucrt2DZC' : '2uPK6yhPqjC0eNUFhaY78S26cRKyaa6t';

interface CliCommandEvent {
installMethod: InstallMethod;
osArch: typeof process.arch;
runtime: typeof metadata.runtime.runtime;
runtimeVersion: typeof metadata.runtime.version;
runtimeNodeVersion: typeof metadata.runtime.nodeVersion;

commandString: string;
entrypoint: string;

flagsUsed: string[];

actorLanguage?: 'javascript' | 'python';
actorRuntime?: string;
actorRuntimeVersion?: string;

// create command
fromArchiveUrl?: boolean;
templateId?: string;
templateName?: string;
templateLanguage?: string;

// init command
actorWrapper?: string;

// execution context
exitCode?: number;
durationMs?: number;
aiAgent?: string;
userAgent?: string;
isCi?: boolean;
ciProvider?: string;
isInteractive?: boolean;
wasRetried?: boolean;
}

export interface TrackEventMap {
[key: `cli_command_${string}`]: {
installMethod: InstallMethod;
osArch: typeof process.arch;
runtime: typeof metadata.runtime.runtime;
runtimeVersion: typeof metadata.runtime.version;
runtimeNodeVersion: typeof metadata.runtime.nodeVersion;

commandString: string;
entrypoint: string;

flagsUsed: string[];

actorLanguage?: 'javascript' | 'python';
actorRuntime?: string;
actorRuntimeVersion?: string;

// create command
fromArchiveUrl?: boolean;
templateId?: string;
templateName?: string;
templateLanguage?: string;

// init command
actorWrapper?: string;

// execution context
exitCode?: number;
durationMs?: number;
aiAgent?: string;
userAgent?: string;
isCi?: boolean;
ciProvider?: string;
isInteractive?: boolean;
wasRetried?: boolean;
};
cli_command: CliCommandEvent;
}

export async function trackEvent<Event extends keyof TrackEventMap>(event: Event, eventData: TrackEventMap[Event]) {
Expand Down
51 changes: 51 additions & 0 deletions test/local/lib/command-framework/telemetry-event-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable max-classes-per-file */
import { parseArgs } from 'node:util';

const { trackEventMock } = vi.hoisted(() => ({
trackEventMock: vi.fn(),
}));

vi.mock('../../../../src/lib/hooks/telemetry/trackEvent.js', () => ({
trackEvent: trackEventMock,
}));

vi.mock('../../../../src/lib/hooks/telemetry/useTelemetryState.js', () => ({
checkAndUpdateLastCommand: vi.fn().mockResolvedValue(false),
}));

import {
ApifyCommand,
type BuiltApifyCommand as _BuiltApifyCommand,
} from '../../../../src/lib/command-framework/apify-command.js';

const BuiltApifyCommand = ApifyCommand as typeof _BuiltApifyCommand;

class NoOpCommand extends BuiltApifyCommand {
static override name = 'noop' as const;
static override description = 'Does nothing.';

async run() {
// no-op
}
}

describe('command telemetry event naming', () => {
test('emits only the unified command telemetry event', async () => {
const instance = new NoOpCommand('apify', 'datasets ls', 'datasets');
// eslint-disable-next-line dot-notation
const parserOptions = instance['_buildParseArgsOption']();
const parsed = parseArgs({ ...parserOptions, args: [] });

// eslint-disable-next-line dot-notation
await instance['_run'](parsed);

expect(trackEventMock).toHaveBeenCalledWith(
'cli_command',
expect.objectContaining({
commandString: 'datasets ls',
entrypoint: 'apify',
}),
);
expect(trackEventMock).toHaveBeenCalledTimes(1);
});
});
Loading