mirror of
https://github.com/wassname/talk.git
synced 2026-07-19 11:28:50 +08:00
fix: ensure anonymousId or userId is added to the payload (#3075)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent
2bbfbc7ddd
commit
b97ec638b2
@@ -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");
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
+1
@@ -1,6 +1,7 @@
|
||||
declare module "@rudderstack/rudder-sdk-node" {
|
||||
export interface Event {
|
||||
userId?: string;
|
||||
anonymousId?: string;
|
||||
event: string;
|
||||
properties: Record<string, any>;
|
||||
timestamp?: Date;
|
||||
|
||||
Reference in New Issue
Block a user