Skip to content

Commit

Permalink
⚡ Add userId to BE PH identify call
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Sep 8, 2022
1 parent c5ae9c4 commit 895aaa4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ export interface IInternalHooksClass {
onServerStarted(
diagnosticInfo: IDiagnosticInfo,
firstWorkflowCreatedAt?: Date,
userId?: string,
): Promise<unknown[]>;
onPersonalizationSurveySubmitted(userId: string, answers: Record<string, string>): Promise<void>;
onWorkflowCreated(userId: string, workflow: IWorkflowBase, publicApi: boolean): Promise<void>;
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/InternalHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class InternalHooksClass implements IInternalHooksClass {
async onServerStarted(
diagnosticInfo: IDiagnosticInfo,
earliestWorkflowCreatedAt?: Date,
userId?: string,
): Promise<unknown[]> {
const info = {
version_cli: diagnosticInfo.versionCli,
Expand All @@ -45,9 +46,11 @@ export class InternalHooksClass implements IInternalHooksClass {
n8n_binary_data_mode: diagnosticInfo.binaryDataMode,
n8n_multi_user_allowed: diagnosticInfo.n8n_multi_user_allowed,
smtp_set_up: diagnosticInfo.smtp_set_up,
user_id: userId,
};

return Promise.all([
this.telemetry.identify(info),
this.telemetry.track('Instance started', {
...info,
earliest_workflow_created: earliestWorkflowCreatedAt,
Expand Down
23 changes: 15 additions & 8 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1986,14 +1986,21 @@ export async function start(): Promise<void> {
smtp_set_up: config.getEnv('userManagement.emails.mode') === 'smtp',
};

void Db.collections
.Workflow!.findOne({
select: ['createdAt'],
order: { createdAt: 'ASC' },
})
.then(async (workflow) =>
InternalHooksManager.getInstance().onServerStarted(diagnosticInfo, workflow?.createdAt),
);
const earliestWorkflow = await Db.collections.Workflow.findOne({
select: ['createdAt', 'id'],
order: { createdAt: 'ASC' },
});

const sharing = await Db.collections.SharedWorkflow.findOne({
where: { workflow: { id: earliestWorkflow?.id } },
relations: ['workflow'],
});

void InternalHooksManager.getInstance().onServerStarted(
diagnosticInfo,
earliestWorkflow?.createdAt,
sharing?.userId,
);
});

server.on('error', (error: Error & { code: string }) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,38 @@ export class Telemetry {
});
}

async identify(traits?: {
[key: string]: string | number | boolean | object | undefined | null;
}): Promise<void> {
return new Promise<void>((resolve) => {
if (this.postHog && traits?.user_id) {
this.postHog.identify({
distinctId: [this.instanceId, traits.user_id].join('#'),
properties: {
...traits,
instanceId: this.instanceId,
},
});
}

if (this.rudderStack) {
this.rudderStack.identify(
{
userId: this.instanceId,
anonymousId: '000000000000',
traits: {
...traits,
instanceId: this.instanceId,
},
},
resolve,
);
} else {
resolve();
}
});
}

async track(
eventName: string,
properties: ITelemetryTrackProperties = {},
Expand Down

0 comments on commit 895aaa4

Please sign in to comment.