Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(workflows): record recipient, domain, opens and clicks per email (#127745)

Daniel Marchuk39a7be29e548
diff --git a/nodejs/src/cdp/cdp-api.serial.test.ts b/nodejs/src/cdp/cdp-api.serial.test.ts
index 6d075614d7d6..f4dd9ccc48d4 100644
--- a/nodejs/src/cdp/cdp-api.serial.test.ts
+++ b/nodejs/src/cdp/cdp-api.serial.test.ts
@@ -2296,7 +2296,13 @@ describe('CDP API', () => {
expect.anything()
)
expect(emailStatusRows()).toEqual([
- expect.objectContaining({ team_id: team.id, message_id: res.body.invocation_id, status: 'queued' }),
+ expect.objectContaining({
+ team_id: team.id,
+ message_id: res.body.invocation_id,
+ status: 'queued',
+ recipient: '[email protected]',
+ function_id: hogFlow.id,
+ }),
])
})
diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.ts
index 3b926ed56ab1..babfcb1e2fd5 100644
--- a/nodejs/src/cdp/cdp-api.ts
+++ b/nodejs/src/cdp/cdp-api.ts
@@ -1378,7 +1378,13 @@ export class CdpApi {
await idempotencyKeys.markQueued(team.id, idempotencyKey, fingerprint, invocation.id)
}
stages.start('metrics_flush')
- this.emailStatusService.queue({ teamId: team.id, messageId: invocation.id, status: 'queued' })
+ this.emailStatusService.queue({
+ teamId: team.id,
+ messageId: invocation.id,
+ status: 'queued',
+ recipient: to,
+ functionId: hogFlow.id,
+ })
// Best-effort rows, so the 202 waits for the hand-off to Kafka (flushed on shutdown) but not the acks.
const warnTelemetryFlushFailed = (error: unknown) =>
logger.warn('⚠️', 'Transactional email telemetry flush failed', {
diff --git a/nodejs/src/cdp/services/messaging/email-status.service.test.ts b/nodejs/src/cdp/services/messaging/email-status.service.test.ts
index bbf9ee57d9f3..fa0628a49291 100644
--- a/nodejs/src/cdp/services/messaging/email-status.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email-status.service.test.ts
@@ -30,6 +30,9 @@ describe('EmailStatusService', () => {
created_at: '2026-01-02 03:04:05.678000',
status_at: '2026-01-03 10:00:00.123000',
reason: 'hard',
+ recipient: '',
+ function_id: '',
+ sending_domain: '',
})
})
diff --git a/nodejs/src/cdp/services/messaging/email-status.service.ts b/nodejs/src/cdp/services/messaging/email-status.service.ts
index 42cccbed5756..36dcfb85de2d 100644
--- a/nodejs/src/cdp/services/messaging/email-status.service.ts
+++ b/nodejs/src/cdp/services/messaging/email-status.service.ts
@@ -21,7 +21,12 @@ const counterEmailStatusWriteFailed = new Counter({
labelNames: ['status'],
})
-export type EmailStatus = 'queued' | 'failed' | 'sent' | 'delivered' | 'bounced' | 'complained'
+export type EmailDeliveryStatus = 'queued' | 'failed' | 'sent' | 'delivered' | 'bounced' | 'complained'
+
+// Only email_status_events keeps these, so they never replace a delivery status.
+export type EmailEngagementStatus = 'opened' | 'clicked'
+
+export type EmailStatus = EmailDeliveryStatus | EmailEngagementStatus
export type EmailStatusReason =
// A bounce type, in the words the `$bounce_type` event property uses.
@@ -63,6 +68,8 @@ const STATUS_RANK: Record<EmailStatus, bigint> = {
delivered: 5n,
bounced: 6n,
complained: 7n,
+ opened: 8n,
+ clicked: 9n,
}
// A run that fails on its email step fails after the step recorded why, for example
@@ -90,6 +97,9 @@ export interface EmailStatusRow {
created_at: string
status_at: string
reason: string
+ recipient: string
+ function_id: string
+ sending_domain: string
}
export interface EmailStatusUpdate {
@@ -102,6 +112,9 @@ export interface EmailStatusUpdate {
statusAt?: Date
// A failure of the whole run rather than of the email step.
runOutcome?: boolean
+ recipient?: string
+ functionId?: string
+ sendingDomain?: string
}
const isoMicroseconds = (date: Date): string => {
@@ -138,6 +151,9 @@ export const buildEmailStatusRow = (update: EmailStatusUpdate): EmailStatusRow =
created_at: isoMicroseconds(uuidtCreatedAt(update.messageId)),
status_at: isoMicroseconds(statusAt),
reason: update.reason ?? '',
+ recipient: update.recipient ?? '',
+ function_id: update.functionId ?? '',
+ sending_domain: update.sendingDomain ?? '',
}
}
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.ts
index 2820f0c621e0..137dfdd05408 100644
--- a/nodejs/src/cdp/services/messaging/email.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts
@@ -1601,8 +1601,19 @@ describe('EmailService', () => {
await emailStatusService.flush()
const rows = produce.mock.calls.map(([, message]) => parseJSON(message.value.toString()))
+ const sendingDomain = status === 'sent' || reason === 'provider_error' ? 'posthog-test.com' : ''
expect(rows).toEqual(
- status ? [expect.objectContaining({ team_id: team.id, message_id: invocation.id, status, reason })] : []
+ status
+ ? [
+ expect.objectContaining({
+ team_id: team.id,
+ message_id: invocation.id,
+ status,
+ reason,
+ sending_domain: sendingDomain,
+ }),
+ ]
+ : []
)
})
diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.ts
index 76e4b06c1216..9765aead3ae0 100644
--- a/nodejs/src/cdp/services/messaging/email.service.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.ts
@@ -516,6 +516,7 @@ export class EmailService {
let rescheduled: boolean = false
let assetRow: MessageAssetRow | null = null
let trackingEnabled = true
+ let sendingDomain = ''
let failureReason: EmailStatusReason = 'provider_error'
try {
@@ -602,6 +603,7 @@ export class EmailService {
}
const from = this.resolveFromSender(integration, params.from, addLog)
+ sendingDomain = from.email.split('@').pop()?.toLowerCase() ?? ''
// An empty `to` comes from the customer's data (a person without an email property, or no
// person at all), not from a fault in the send path. SES rejects it as an invalid address,
@@ -913,6 +915,7 @@ export class EmailService {
messageId: invocation.id,
status: success ? 'sent' : 'failed',
reason: success ? undefined : failureReason,
+ sendingDomain,
})
}
}
diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts b/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
index 4366541e15fd..735a4693f45e 100644
--- a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
@@ -1126,6 +1126,24 @@ describe('SesWebhookHandler', () => {
{ eventType: 'Reject', reject: { reason: 'Bad content' } },
update('failed', transactionalMail.timestamp, 'provider_rejected'),
],
+ [
+ 'an open',
+ {},
+ { eventType: 'Open', open: { timestamp: '2025-10-03T12:04:00Z' } },
+ update('opened', '2025-10-03T12:04:00Z'),
+ ],
+ [
+ 'a click',
+ {},
+ { eventType: 'Click', click: { link: 'https://example.com/reset', timestamp: '2025-10-03T12:05:00Z' } },
+ update('clicked', '2025-10-03T12:05:00Z'),
+ ],
+ [
+ 'an open of an untagged email',
+ { tags: baseMail.tags },
+ { eventType: 'Open', open: { timestamp: '2025-10-03T12:04:00Z' } },
+ [],
+ ],
['a delivery of an untagged email', { tags: baseMail.tags }, delivery(['[email protected]']), []],
['a delivery to the cc recipient only', {}, delivery(['[email protected]']), []],
['a bounce of the cc recipient', {}, bounce('Permanent', '[email protected]'), []],
diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.ts b/nodejs/src/cdp/services/messaging/helpers/ses.ts
index 6bbafadffa33..df850381cb18 100644
--- a/nodejs/src/cdp/services/messaging/helpers/ses.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/ses.ts
@@ -279,7 +279,8 @@ const toHeaderAddresses = (rec: SesEventRecord): Set<string> =>
)
/**
- * The status change a Delivery, Bounce, Complaint or Reject record makes to the email it reports on, or null.
+ * The status change a Delivery, Bounce, Complaint or Reject record makes to the email it reports on, or the
+ * open or click an Open or Click record adds to it, or null.
*
* The status describes the email to its To recipient. SES reports cc and bcc copies under the same
* message, so a record counts only when one of its recipients is in the To header.
@@ -311,6 +312,10 @@ const resolveEmailStatusUpdate = (
case 'Reject':
// SES refuses the whole message, so the email fails for its To recipient too.
return { status: 'failed', reason: 'provider_rejected', timestamp: rec.mail.timestamp }
+ case 'Open':
+ return { status: 'opened', timestamp: rec.open.timestamp }
+ case 'Click':
+ return { status: 'clicked', timestamp: rec.click.timestamp }
default:
return null
}