From b97ec638b24c45670d6d15ce000e5aaf61bbccc4 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 6 Aug 2020 10:35:02 -0600 Subject: [PATCH] fix: ensure anonymousId or userId is added to the payload (#3075) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- src/core/server/events/listeners/analytics.ts | 95 ++++++++++++------- src/types/@rudderstack/rudder-sdk-node.d.ts | 1 + 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/core/server/events/listeners/analytics.ts b/src/core/server/events/listeners/analytics.ts index bbb22e67d..42422b42a 100644 --- a/src/core/server/events/listeners/analytics.ts +++ b/src/core/server/events/listeners/analytics.ts @@ -53,6 +53,18 @@ export class AnalyticsCoralEventListener this.analytics = new Analytics(key, relativeTo("/v1/batch", url)); } + private track(event: Event) { + return new Promise((resolve, reject) => { + this.analytics.track(event, (err) => { + if (err) { + return reject(err); + } + + return resolve(); + }); + }); + } + private filter(event: AnalyticsCoralEventListenerPayloads): boolean { switch (event.type) { case CoralEventType.COMMENT_CREATED: @@ -178,44 +190,57 @@ export class AnalyticsCoralEventListener public initialize: CoralEventPublisherFactory< AnalyticsCoralEventListenerPayloads - > = (ctx) => { - return async (event) => { - // Check to see if we should process this event. - if (!this.filter(event)) { - // The event should not be processed. - return; - } + > = (ctx) => async (event) => { + // Check to see if we should process this event. + if (!this.filter(event)) { + // The event should not be processed. + return; + } - // Create the event payload. - const details = await this.create(ctx, event); - if (!details) { - return; - } + // Create the event payload. + const details = await this.create(ctx, event); + if (!details) { + return; + } - // Pull some properties out of the context. - const { - // Sometimes, the user isn't defined (an anonymous request), so default - // so we don't get any spread errors. - user: { id: userId } = {}, - tenant: { id: tenantID, domain: tenantDomain }, - } = ctx; + // Pull some properties out of the context. + const { + id, + user, + tenant: { id: tenantID, domain: tenantDomain }, + } = ctx; - // Assemble the track payload. - const payload: Event = { - event: details.event, - userId, - properties: { - ...details.properties, - tenantID, - tenantDomain, - }, - timestamp: event.createdAt, - }; - - logger.debug({ payload }, "sending analytics event"); - - // Send the event payload to analytics. - return this.analytics.track(payload); + // Assemble the track payload. + const payload: Event = { + event: details.event, + properties: { + ...details.properties, + tenantID, + tenantDomain, + }, + timestamp: event.createdAt, }; + + if (user) { + // If the user is available, attach it as the userId. + payload.userId = user.id; + } else { + // Otherwise use the traceID of the request as the anonomousId. + payload.anonymousId = id; + } + + logger.debug({ payload }, "sending analytics event"); + + // Send the event payload to analytics. We don't await on the event to be + // sent because the analytics event system will batch these operations + // and may not send them for a few seconds (we can't make the user wait that + // long). + this.track(payload) + .then(() => { + logger.debug({ payload }, "analytics event sent"); + }) + .catch((err) => { + logger.error({ err, payload }, "could not send analytics event"); + }); }; } diff --git a/src/types/@rudderstack/rudder-sdk-node.d.ts b/src/types/@rudderstack/rudder-sdk-node.d.ts index c00652843..d1def7cc1 100644 --- a/src/types/@rudderstack/rudder-sdk-node.d.ts +++ b/src/types/@rudderstack/rudder-sdk-node.d.ts @@ -1,6 +1,7 @@ declare module "@rudderstack/rudder-sdk-node" { export interface Event { userId?: string; + anonymousId?: string; event: string; properties: Record; timestamp?: Date;