Skip to content

(EAI-950) Segment - Send data feed to Segment #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 22, 2025
Merged

(EAI-950) Segment - Send data feed to Segment #679

merged 11 commits into from
Apr 22, 2025

Conversation

nlarew
Copy link
Collaborator

@nlarew nlarew commented Apr 19, 2025

Jira: (EAI-950) Segment - Send data feed to Segment

Changes

  • Defines a new useSegmentIds middleware to pull out Segment user IDs from request headers
  • Defines segment handlers that send chatbot events to Segment
    • Uses the SEGMENT_WRITE_KEY env var to route requests
  • Updates post-request trace handlers to also send segment events

Comment on lines 257 to 258
segmentUserId: res.locals.customData.segmentUserId ?? "NOPE",
segmentAnonymousId: res.locals.customData.segmentAnonymousId ?? "NOPE",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do null rather than 'NOPE'? or does 'NOPE' mean something to segment

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops did that for testing purposes and used a silly word so that I'd notice it later. Good catch!

Comment on lines 62 to 90
if (userMessage) {
segmentTrackUserSentMessage?.({
userId,
anonymousId,
conversationId: conversation._id,
origin: userMessage.customData?.origin as string,
createdAt: userMessage.createdAt,
tags: tracingData.tags,
});
}

const assistantMessage = tracingData.assistantMessage;
if (userMessage && assistantMessage) {
segmentTrackAssistantResponded?.({
userId,
anonymousId,
conversationId: conversation._id,
origin: userMessage.customData?.origin as string,
createdAt: assistantMessage.createdAt,
isVerifiedAnswer: tracingData.isVerifiedAnswer ?? false,
rejectionReason: tracingData.rejectQuery
? (assistantMessage.customData?.rejectionReason as
| string
| undefined) ?? "Unknown rejection reason"
: undefined,
});
}

logger.updateSpan({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that there are 2 separate things happening in this trace func, should we wrap each w/ a try/catch block?

i know both aren't async, since the processing happening in background..but still.

not sure what right approach is, but wanted to raise the question

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - added try catch. Also needed a mongodb-chatbot-server update so that we pass the reqId for logging

Copy link
Collaborator

@mongodben mongodben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me. see comment on handling the 2 traces with try/catch blocks

Comment on lines +11 to +14
res.locals.customData.segmentUserId = userId;
}
if (anonymousId) {
res.locals.customData.segmentAnonymousId = anonymousId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by including this, does it mean that we're now tracking the segment user and anon IDs in the DB?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we will track these in user message customData. I think they classify as PII so don't want to keep them in scrubbed_messages.

@mongodben
Copy link
Collaborator

out of curiousity, whaat do the segment ID and anon IDs look like? and where are they coming from? can we connect a segment ID back to a real user, like w/ email or smthn?

@nlarew
Copy link
Collaborator Author

nlarew commented Apr 22, 2025

out of curiousity, whaat do the segment ID and anon IDs look like? and where are they coming from?

They're UUIDs with slightly different formats, e.g.

  • ajs_anonymous_id: "f32c22fe-510d-4d22-9122-5ea4af6b228d"
  • ajs_user_id: "71ff22b6cf2cf2175312465f166a9a92f4f801fcef24d12c4aa79c65e8c20dab"

They're stored in localStorage on MongoDB web properties that include Segment's AnalyticsJS (pretty much all of our sites, including docs)

can we connect a segment ID back to a real user, like w/ email or smthn?

I think the userId can - will confirm with MOPS

@nlarew nlarew merged commit 46d0979 into main Apr 22, 2025
1 check passed
@nlarew nlarew deleted the segment branch April 22, 2025 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants