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(cdp): emit per-type bounce sub-metrics for SES bounces (#72810)

Meikel Ratzce8cb2c21b95
diff --git a/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts b/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
index 670fc344ba10..aaf6bb514615 100644
--- a/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
@@ -350,7 +350,8 @@ describe('EmailTrackingService', () => {
})
const metrics = mockProducerObserver.getProducedKafkaMessagesForTopic(KAFKA_APP_METRICS_2)
- expect(metrics).toHaveLength(1)
+ // Permanent bounces emit the rollup plus the hard-only sub-metric
+ expect(metrics.map((m) => m.value.metric_name)).toEqual(['email_bounced', 'email_bounced_hard'])
expect(metrics[0].value).toMatchObject({
team_id: team.id,
metric_name: 'email_bounced',
@@ -364,7 +365,8 @@ describe('EmailTrackingService', () => {
await waitForExpect(() => {
const metrics = mockProducerObserver.getProducedKafkaMessagesForTopic(KAFKA_APP_METRICS_2)
- expect(metrics).toHaveLength(1)
+ // Permanent bounces emit the rollup plus the hard-only sub-metric
+ expect(metrics.map((m) => m.value.metric_name)).toEqual(['email_bounced', 'email_bounced_hard'])
expect(metrics[0].value).toMatchObject({
team_id: team.id,
metric_name: 'email_bounced',
diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts b/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
index 4801a165c6ed..200d80cacb32 100644
--- a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/ses.test.ts
@@ -324,7 +324,8 @@ describe('SesWebhookHandler', () => {
]
const result = await handler.handleWebhook({ body, headers: {} })
expect(result.status).toBe(200)
- expect(result.metrics?.[0].metricName).toBe('email_bounced')
+ // Hard bounces emit both the catch-all metric and the AWS-comparable hard-only one
+ expect(result.metrics?.map((m) => m.metricName)).toEqual(['email_bounced', 'email_bounced_hard'])
expect(result.metrics?.[0].distinctId).toBe('user-123')
expect(result.hardBounceRecipients).toEqual([
{ teamId: '1', emailAddresses: ['[email protected]'], diagnostic: 'bad' },
@@ -349,7 +350,8 @@ describe('SesWebhookHandler', () => {
]
const result = await handler.handleWebhook({ body, headers: {} })
expect(result.status).toBe(200)
- expect(result.metrics?.[0].metricName).toBe('email_bounced')
+ // Transient bounces must NOT emit email_bounced_hard — AWS's account rate excludes them
+ expect(result.metrics?.map((m) => m.metricName)).toEqual(['email_bounced', 'email_bounced_transient'])
expect(result.metrics?.[0].distinctId).toBe('user-123')
expect(result.hardBounceRecipients).toEqual([])
expect(result.transientBounceRecipients).toEqual([
diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.ts b/nodejs/src/cdp/services/messaging/helpers/ses.ts
index 58c3e5249b91..6699724b4424 100644
--- a/nodejs/src/cdp/services/messaging/helpers/ses.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/ses.ts
@@ -153,6 +153,13 @@ export type SesEventRecord = z.infer<typeof SesEventRecordSchema>
// email_sent is recorded synchronously in email.service.ts when the email is sent,
// so we don't record it again from SES Send webhooks to avoid double counting.
+const BOUNCE_TYPE_TO_METRIC_NAME: Record<'Permanent' | 'Transient' | 'Undetermined', MinimalAppMetric['metric_name']> =
+ {
+ Permanent: 'email_bounced_hard',
+ Transient: 'email_bounced_transient',
+ Undetermined: 'email_bounced_undetermined',
+ }
+
const EVENT_TYPE_TO_METRIC_NAME: Partial<Record<SesEventRecord['eventType'], MinimalAppMetric['metric_name']>> = {
Open: 'email_opened',
Click: 'email_link_clicked',
@@ -593,6 +600,24 @@ export class SesWebhookHandler {
properties,
timestamp,
})
+
+ // email_bounced stays the catch-all rollup; each bounce additionally emits a
+ // per-type sub-metric (hard + transient + undetermined = email_bounced). AWS's
+ // account bounce rate — what the email reputation feature calibrates against —
+ // counts only hard (Permanent) bounces, so reputation reads email_bounced_hard;
+ // the others exist for diagnosis (transient spikes are a leading indicator).
+ if (rec.eventType === 'Bounce') {
+ metrics.push({
+ functionId,
+ invocationId,
+ actionId,
+ parentRunId,
+ distinctId,
+ metricName: BOUNCE_TYPE_TO_METRIC_NAME[rec.bounce.bounceType],
+ properties,
+ timestamp,
+ })
+ }
}
// Allowlist actionId before interpolating into the [Action:…] rich-log token,
diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.ts
index f531989bbec5..14ed9f0bbf41 100644
--- a/nodejs/src/cdp/types.ts
+++ b/nodejs/src/cdp/types.ts
@@ -236,6 +236,9 @@ export type MinimalAppMetric = {
| 'email_opened'
| 'email_link_clicked'
| 'email_bounced'
+ | 'email_bounced_hard'
+ | 'email_bounced_transient'
+ | 'email_bounced_undetermined'
| 'email_bounce_prevented'
| 'email_suppressed'
| 'email_blocked'