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
18 changes: 17 additions & 1 deletion packages/cli/src/events/__tests__/telemetry-event-relay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ describe('TelemetryEventRelay', () => {
});

describe('workflow execution events', () => {
it('should track on `first-production-workflow-succeeded` event', () => {
it('should track on `first-production-workflow-succeeded` event for personal project', () => {
const event: RelayEventMap['first-production-workflow-succeeded'] = {
projectId: 'project123',
workflowId: 'workflow123',
Expand All @@ -1138,6 +1138,22 @@ describe('TelemetryEventRelay', () => {
});
});

it('should track on `first-production-workflow-succeeded` event for team project with null userId', () => {
const event: RelayEventMap['first-production-workflow-succeeded'] = {
projectId: 'project123',
workflowId: 'workflow123',
userId: null,
};

eventService.emit('first-production-workflow-succeeded', event);

expect(telemetry.track).toHaveBeenCalledWith('Workflow first prod success', {
project_id: 'project123',
workflow_id: 'workflow123',
user_id: undefined,
});
});

it('should track on `first-workflow-data-loaded` event', () => {
const event: RelayEventMap['first-workflow-data-loaded'] = {
userId: 'user123',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/events/maps/relay.event-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type RelayEventMap = {
'first-production-workflow-succeeded': {
projectId: string;
workflowId: string;
userId: string;
userId: string | null;
};

'first-workflow-data-loaded': {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/events/relays/telemetry.event-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ export class TelemetryEventRelay extends EventRelay {
this.telemetry.track('Workflow first prod success', {
project_id: projectId,
workflow_id: workflowId,
user_id: userId,
user_id: userId ?? undefined,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getPersonalProject, createWorkflow, testDb, mockInstance } from '@n8n/backend-test-utils';
import {
getPersonalProject,
createTeamProject,
createWorkflow,
testDb,
mockInstance,
} from '@n8n/backend-test-utils';
import { GlobalConfig } from '@n8n/config';
import type { IWorkflowDb, Project, WorkflowEntity, User } from '@n8n/db';
import { WorkflowStatisticsRepository } from '@n8n/db';
Expand Down Expand Up @@ -243,6 +249,33 @@ describe('WorkflowStatisticsService', () => {
expect(updateSettingsSpy).not.toHaveBeenCalled();
expect(emitSpy).not.toHaveBeenCalled();
});

test('emits first-production-workflow-succeeded with null userId for team project', async () => {
// ARRANGE
const teamProject = await createTeamProject('Team Project');
const teamWorkflow = await createWorkflow({}, teamProject);
const runData: IRun = {
finished: true,
status: 'success',
data: createEmptyRunExecutionData(),
mode: 'internal',
startedAt: new Date(),
};
const emitSpy = jest.spyOn(Container.get(EventService), 'emit');
const updateSettingsSpy = jest.spyOn(userService, 'updateSettings');

// ACT
await workflowStatisticsService.workflowExecutionCompleted(teamWorkflow, runData);

// ASSERT
expect(updateSettingsSpy).not.toHaveBeenCalled();
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy).toHaveBeenCalledWith('first-production-workflow-succeeded', {
projectId: teamProject.id,
workflowId: teamWorkflow.id,
userId: null,
});
});
});

describe('nodeFetchedData', () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/src/services/workflow-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ export class WorkflowStatisticsService extends TypedEmitter<WorkflowStatisticsEv

if (name === StatisticsNames.productionSuccess && upsertResult === 'insert') {
const project = await this.ownershipService.getWorkflowProjectCached(workflowId);
let userId: string | null = null;

if (project.type === 'personal') {
const owner = await this.ownershipService.getPersonalProjectOwnerCached(project.id);
userId = owner?.id ?? null;

if (owner && !owner.settings?.userActivated) {
await this.userService.updateSettings(owner.id, {
Expand All @@ -126,13 +129,13 @@ export class WorkflowStatisticsService extends TypedEmitter<WorkflowStatisticsEv
userActivatedAt: runData.startedAt.getTime(),
});
}

this.eventService.emit('first-production-workflow-succeeded', {
projectId: project.id,
workflowId,
userId: owner!.id,
});
}

this.eventService.emit('first-production-workflow-succeeded', {
projectId: project.id,
workflowId,
userId,
});
}
} catch (error) {
this.logger.debug('Unable to fire first workflow success telemetry event');
Expand Down
Loading