Add userId to BE PH identify call

This commit is contained in:
Iván Ovejero 2022-09-08 12:31:25 +02:00
parent c5ae9c4199
commit 895aaa45e5
4 changed files with 51 additions and 8 deletions

View file

@ -380,6 +380,7 @@ export interface IInternalHooksClass {
onServerStarted( onServerStarted(
diagnosticInfo: IDiagnosticInfo, diagnosticInfo: IDiagnosticInfo,
firstWorkflowCreatedAt?: Date, firstWorkflowCreatedAt?: Date,
userId?: string,
): Promise<unknown[]>; ): Promise<unknown[]>;
onPersonalizationSurveySubmitted(userId: string, answers: Record<string, string>): Promise<void>; onPersonalizationSurveySubmitted(userId: string, answers: Record<string, string>): Promise<void>;
onWorkflowCreated(userId: string, workflow: IWorkflowBase, publicApi: boolean): Promise<void>; onWorkflowCreated(userId: string, workflow: IWorkflowBase, publicApi: boolean): Promise<void>;

View file

@ -32,6 +32,7 @@ export class InternalHooksClass implements IInternalHooksClass {
async onServerStarted( async onServerStarted(
diagnosticInfo: IDiagnosticInfo, diagnosticInfo: IDiagnosticInfo,
earliestWorkflowCreatedAt?: Date, earliestWorkflowCreatedAt?: Date,
userId?: string,
): Promise<unknown[]> { ): Promise<unknown[]> {
const info = { const info = {
version_cli: diagnosticInfo.versionCli, version_cli: diagnosticInfo.versionCli,
@ -45,9 +46,11 @@ export class InternalHooksClass implements IInternalHooksClass {
n8n_binary_data_mode: diagnosticInfo.binaryDataMode, n8n_binary_data_mode: diagnosticInfo.binaryDataMode,
n8n_multi_user_allowed: diagnosticInfo.n8n_multi_user_allowed, n8n_multi_user_allowed: diagnosticInfo.n8n_multi_user_allowed,
smtp_set_up: diagnosticInfo.smtp_set_up, smtp_set_up: diagnosticInfo.smtp_set_up,
user_id: userId,
}; };
return Promise.all([ return Promise.all([
this.telemetry.identify(info),
this.telemetry.track('Instance started', { this.telemetry.track('Instance started', {
...info, ...info,
earliest_workflow_created: earliestWorkflowCreatedAt, earliest_workflow_created: earliestWorkflowCreatedAt,

View file

@ -1986,14 +1986,21 @@ export async function start(): Promise<void> {
smtp_set_up: config.getEnv('userManagement.emails.mode') === 'smtp', smtp_set_up: config.getEnv('userManagement.emails.mode') === 'smtp',
}; };
void Db.collections const earliestWorkflow = await Db.collections.Workflow.findOne({
.Workflow!.findOne({ select: ['createdAt', 'id'],
select: ['createdAt'], order: { createdAt: 'ASC' },
order: { createdAt: 'ASC' }, });
})
.then(async (workflow) => const sharing = await Db.collections.SharedWorkflow.findOne({
InternalHooksManager.getInstance().onServerStarted(diagnosticInfo, workflow?.createdAt), where: { workflow: { id: earliestWorkflow?.id } },
); relations: ['workflow'],
});
void InternalHooksManager.getInstance().onServerStarted(
diagnosticInfo,
earliestWorkflow?.createdAt,
sharing?.userId,
);
}); });
server.on('error', (error: Error & { code: string }) => { server.on('error', (error: Error & { code: string }) => {

View file

@ -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( async track(
eventName: string, eventName: string,
properties: ITelemetryTrackProperties = {}, properties: ITelemetryTrackProperties = {},