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.

fix(workflows): only show back in app after a real return (#130839)

Daniel Marchuk020d9ebfe9f9
diff --git a/products/workflows/frontend/Transactional/log/TransactionalEmailActivityTimeline.tsx b/products/workflows/frontend/Transactional/log/TransactionalEmailActivityTimeline.tsx
index e48ce782addd..4c1d37062b18 100644
--- a/products/workflows/frontend/Transactional/log/TransactionalEmailActivityTimeline.tsx
+++ b/products/workflows/frontend/Transactional/log/TransactionalEmailActivityTimeline.tsx
@@ -103,7 +103,7 @@ export function TransactionalEmailActivityTimeline({
failed={['bounced', 'complained', 'failed'].includes(email.status)}
sendLabel={sendLabel(email, sentAt)}
sendDetail={email.template_name || null}
- comeback={describeComeback(activity.after, email.timeline)}
+ comeback={describeComeback(activity, email.timeline)}
emptyAfter={emptyAfterText(activity, sentAt)}
hiddenBefore={hiddenBefore}
onShowEarlier={showAllBefore}
diff --git a/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryProgress.stories.tsx b/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryProgress.stories.tsx
index 02250e8b42d3..cf712045258b 100644
--- a/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryProgress.stories.tsx
+++ b/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryProgress.stories.tsx
@@ -57,6 +57,19 @@ OpenedAndClicked.args = {
]),
}
+export const SensitiveClicksNotTracked = Template.bind({})
+SensitiveClicksNotTracked.args = {
+ email: {
+ ...email('delivered', [
+ { kind: 'queued', at: '2026-06-15T09:00:00Z', reason: null },
+ { kind: 'sent', at: '2026-06-15T09:00:01Z', reason: null },
+ { kind: 'delivered', at: '2026-06-15T09:00:03Z', reason: null },
+ { kind: 'opened', at: '2026-06-15T09:12:40Z', reason: null },
+ ]),
+ sensitive: true,
+ },
+}
+
export const Bounced = Template.bind({})
Bounced.args = {
email: email('bounced', [
diff --git a/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryStep.tsx b/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryStep.tsx
index 4e86f6a88bd9..1a27a3443ce5 100644
--- a/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryStep.tsx
+++ b/products/workflows/frontend/Transactional/log/TransactionalEmailDeliveryStep.tsx
@@ -1,17 +1,56 @@
import clsx from 'clsx'
-import { IconCheck, IconX } from '@posthog/icons'
+import { IconCheck, IconInfo, IconMinus, IconX } from '@posthog/icons'
import { Tooltip } from '@posthog/lemon-ui'
import { TZLabel } from 'lib/components/TZLabel'
import { DeliveryStep, gapLabel } from './emailDeliverySteps'
+const NOT_TRACKED_EXPLANATION =
+ "Links in sensitive emails aren't tracked, because they can contain login codes or reset tokens. Older sends of this template can still record clicks."
+
function StepDetail({ detail }: { detail: string }): JSX.Element {
const text = <span className="text-xs text-secondary truncate">{detail}</span>
return detail.length > 40 ? <Tooltip title={detail}>{text}</Tooltip> : text
}
+function StepMarker({ state }: { state: DeliveryStep['state'] }): JSX.Element {
+ return (
+ <span
+ className={clsx(
+ 'flex items-center justify-center size-6 rounded-full shrink-0 border',
+ state === 'done' && 'bg-success border-success text-white',
+ state === 'failed' && 'bg-danger border-danger text-white',
+ state === 'pending' && 'bg-surface-primary border-dashed border-border-bold',
+ state === 'untracked' && 'bg-surface-secondary border-border text-tertiary'
+ )}
+ >
+ {state === 'done' && <IconCheck className="size-4" />}
+ {state === 'failed' && <IconX className="size-4" />}
+ {state === 'untracked' && <IconMinus className="size-4" />}
+ </span>
+ )
+}
+
+function NotTrackedNote(): JSX.Element {
+ return (
+ <span className="text-xs text-secondary flex items-center gap-1">
+ Not tracked
+ <Tooltip title={NOT_TRACKED_EXPLANATION} openOnClick>
+ <button
+ type="button"
+ aria-label={NOT_TRACKED_EXPLANATION}
+ className="inline-flex cursor-help text-sm"
+ data-attr="transactional-email-clicks-not-tracked"
+ >
+ <IconInfo />
+ </button>
+ </Tooltip>
+ </span>
+ )
+}
+
/** One step of the delivery progress, and the line that leads to the next step. */
export function TransactionalEmailDeliveryStep({
step,
@@ -24,22 +63,12 @@ export function TransactionalEmailDeliveryStep({
return (
<li className="flex @2xl:flex-col gap-3 @2xl:gap-2 flex-1 min-w-0">
<div className="flex items-center @2xl:w-full">
- <span
- className={clsx(
- 'flex items-center justify-center size-6 rounded-full shrink-0 border',
- step.state === 'done' && 'bg-success border-success text-white',
- step.state === 'failed' && 'bg-danger border-danger text-white',
- step.state === 'pending' && 'bg-surface-primary border-dashed border-border-bold'
- )}
- >
- {step.state === 'done' && <IconCheck className="size-4" />}
- {step.state === 'failed' && <IconX className="size-4" />}
- </span>
+ <StepMarker state={step.state} />
{next && (
<span
className={clsx(
'hidden @2xl:block h-px flex-1 mx-2',
- next.state === 'pending' ? 'bg-border' : 'bg-border-bold'
+ next.state === 'pending' || next.state === 'untracked' ? 'bg-border' : 'bg-border-bold'
)}
/>
)}
@@ -48,7 +77,7 @@ export function TransactionalEmailDeliveryStep({
<span
className={clsx(
'text-sm font-semibold',
- step.state === 'pending' && 'text-secondary',
+ (step.state === 'pending' || step.state === 'untracked') && 'text-secondary',
step.state === 'failed' && 'text-danger'
)}
>
@@ -66,6 +95,7 @@ export function TransactionalEmailDeliveryStep({
</span>
)
)}
+ {step.state === 'untracked' && <NotTrackedNote />}
{step.detail && step.state !== 'failed' && <StepDetail detail={step.detail} />}
</div>
</li>
diff --git a/products/workflows/frontend/Transactional/log/emailActivity.test.ts b/products/workflows/frontend/Transactional/log/emailActivity.test.ts
index c3a7a376a958..8c0947f9a837 100644
--- a/products/workflows/frontend/Transactional/log/emailActivity.test.ts
+++ b/products/workflows/frontend/Transactional/log/emailActivity.test.ts
@@ -50,18 +50,77 @@ describe('emailActivity', () => {
it('does not count a server-side event as the person coming back', () => {
const clicked = { kind: 'clicked' as const, at: '2026-06-15T09:02:00Z', reason: null }
- expect(describeComeback([event('server', '2026-06-15T09:03:00Z', null, 'invoice_paid')], [clicked])).toBeNull()
- expect(describeComeback([event('page', '2026-06-15T09:03:00Z', null)], [clicked])).toBe(
+ expect(
+ describeComeback({ before: [], after: [event('server', '2026-06-15T09:03:00Z', null, 'invoice_paid')] }, [
+ clicked,
+ ])
+ ).toBeNull()
+ expect(describeComeback({ before: [], after: [event('page', '2026-06-15T09:03:00Z', null)] }, [clicked])).toBe(
'Back in your app 1m after clicking the email'
)
})
it('keeps a return to the app that came before a later open of the email', () => {
+ const comeback = describeComeback({ before: [], after: [event('return', '2026-06-15T09:03:00Z', 's1')] }, [
+ { kind: 'clicked', at: '2026-06-15T09:02:00Z', reason: null },
+ { kind: 'opened', at: '2026-06-15T10:00:00Z', reason: null },
+ ])
+
+ expect(comeback).toBe('Back in your app 1m after clicking the email')
+ })
+
+ it.each<[string, ActivityEvent[], ActivityEvent[], string | null]>([
+ [
+ 'same session across the send and the open',
+ [event('before', '2026-06-15T08:59:00Z', 's1')],
+ [event('stayed', '2026-06-15T09:03:00Z', 's1')],
+ null,
+ ],
+ [
+ 'session that started after the send but before the open',
+ [],
+ [event('reading', '2026-06-15T09:01:00Z', 's1'), event('stayed', '2026-06-15T09:03:00Z', 's1')],
+ null,
+ ],
+ [
+ 'new session after the open',
+ [event('before', '2026-06-15T08:59:00Z', 's1')],
+ [event('stayed', '2026-06-15T09:03:00Z', 's1'), event('return', '2026-06-15T09:04:00Z', 's2')],
+ 'Back in your app 2m after opening the email',
+ ],
+ [
+ 'event without a session soon after other activity',
+ [event('before', '2026-06-15T08:45:00Z', null)],
+ [event('stayed', '2026-06-15T09:03:00Z', null)],
+ null,
+ ],
+ [
+ 'event without a session a full join window after other activity',
+ [event('before', '2026-06-15T08:33:00Z', null)],
+ [event('return', '2026-06-15T09:03:00Z', null)],
+ 'Back in your app 1m after opening the email',
+ ],
+ [
+ 'server-side event does not keep a later page view from being a return',
+ [event('server', '2026-06-15T09:00:00Z', null, 'invoice_paid')],
+ [event('return', '2026-06-15T09:03:00Z', null)],
+ 'Back in your app 1m after opening the email',
+ ],
+ ])('after only an open: %s', (_, before, after, expected) => {
+ const opened = { kind: 'opened' as const, at: '2026-06-15T09:02:00Z', reason: null }
+
+ expect(describeComeback({ before, after }, [opened])).toBe(expected)
+ })
+
+ it('counts activity in the same session after a click', () => {
const comeback = describeComeback(
- [event('return', '2026-06-15T09:03:00Z', 's1')],
+ {
+ before: [event('before', '2026-06-15T08:59:00Z', 's1')],
+ after: [event('stayed', '2026-06-15T09:03:00Z', 's1')],
+ },
[
+ { kind: 'opened', at: '2026-06-15T09:01:00Z', reason: null },
{ kind: 'clicked', at: '2026-06-15T09:02:00Z', reason: null },
- { kind: 'opened', at: '2026-06-15T10:00:00Z', reason: null },
]
)
diff --git a/products/workflows/frontend/Transactional/log/emailActivity.ts b/products/workflows/frontend/Transactional/log/emailActivity.ts
index 36f6d26a3ff0..d9ec85f5d1cb 100644
--- a/products/workflows/frontend/Transactional/log/emailActivity.ts
+++ b/products/workflows/frontend/Transactional/log/emailActivity.ts
@@ -177,15 +177,33 @@ function isPersonInApp(event: ActivityEvent): boolean {
return !!event.sessionId || event.event === '$pageview' || event.event === '$screen'
}
+/** The person may have had the app open all along when they opened the email, so only a fresh start counts as coming back. */
+function cameBack(event: ActivityEvent, earlier: ActivityEvent[]): boolean {
+ if (event.sessionId) {
+ return !earlier.some((other) => other.sessionId === event.sessionId)
+ }
+ return !earlier.some(
+ (other) =>
+ isPersonInApp(other) &&
+ dayjs(event.timestamp).diff(dayjs(other.timestamp), 'minute') < ACTIVITY_JOIN_MINUTES
+ )
+}
+
function firstReturnToApp(
- after: ActivityEvent[],
+ activity: Pick<PersonActivity, 'before' | 'after'>,
timeline: readonly TransactionalEmailLogEventApi[]
): { event: ActivityEvent; engagement: TransactionalEmailLogEventApi } | null {
const engagements = timeline.filter((event) => event.kind === 'clicked' || event.kind === 'opened').reverse()
- for (const event of after.filter(isPersonInApp)) {
- const engagement = engagements.find((candidate) => dayjs(event.timestamp).isAfter(candidate.at))
- if (engagement) {
- return { event, engagement }
+ const loaded = [...activity.before, ...activity.after]
+ for (const event of activity.after.filter(isPersonInApp)) {
+ const earlierEngagements = engagements.filter((candidate) => dayjs(event.timestamp).isAfter(candidate.at))
+ const click = earlierEngagements.find((candidate) => candidate.kind === 'clicked')
+ if (click) {
+ return { event, engagement: click }
+ }
+ const earlier = loaded.filter((other) => dayjs(other.timestamp).isBefore(event.timestamp))
+ if (earlierEngagements.length && cameBack(event, earlier)) {
+ return { event, engagement: earlierEngagements[0] }
}
}
return null
@@ -193,10 +211,10 @@ function firstReturnToApp(
/** For example "Back in your app 3m after clicking the email", when the person returned after engaging with it. */
export function describeComeback(
- after: ActivityEvent[],
+ activity: Pick<PersonActivity, 'before' | 'after'>,
timeline: readonly TransactionalEmailLogEventApi[]
): string | null {
- const firstReturn = firstReturnToApp(after, timeline)
+ const firstReturn = firstReturnToApp(activity, timeline)
if (!firstReturn) {
return null
}
diff --git a/products/workflows/frontend/Transactional/log/emailDeliverySteps.test.ts b/products/workflows/frontend/Transactional/log/emailDeliverySteps.test.ts
index a71cb4059c1f..d3192d8ffc83 100644
--- a/products/workflows/frontend/Transactional/log/emailDeliverySteps.test.ts
+++ b/products/workflows/frontend/Transactional/log/emailDeliverySteps.test.ts
@@ -7,7 +7,8 @@ import { buildDeliverySteps } from './emailDeliverySteps'
function email(
status: TransactionalEmailLogDetailApi['status'],
- kinds: TransactionalEmailLogEventApi['kind'][]
+ kinds: TransactionalEmailLogEventApi['kind'][],
+ sensitive = false
): TransactionalEmailLogDetailApi {
return {
message_id: '01977a3c-9a00-7000-8000-000000000001',
@@ -30,7 +31,7 @@ function email(
subject: 'Welcome',
distinct_id: 'user_1',
action_id: 'action_1',
- sensitive: false,
+ sensitive,
}
}
@@ -64,6 +65,21 @@ describe('buildDeliverySteps', () => {
email('delivered', ['queued', 'delivered', 'sent']),
['sent:done', 'delivered:done', 'opened:pending', 'clicked:pending'],
],
+ [
+ 'sensitive, so a click can never be recorded',
+ email('delivered', ['queued', 'sent', 'delivered', 'opened'], true),
+ ['sent:done', 'delivered:done', 'opened:done', 'clicked:untracked'],
+ ],
+ [
+ 'sensitive with a click recorded anyway',
+ email('delivered', ['queued', 'sent', 'delivered', 'clicked'], true),
+ ['sent:done', 'delivered:done', 'opened:pending', 'clicked:done'],
+ ],
+ [
+ 'sensitive and bounced',
+ email('bounced', ['queued', 'sent', 'bounced'], true),
+ ['sent:done', 'bounced:failed'],
+ ],
])('%s', (_, input, expected) => {
expect(buildDeliverySteps(input).map((step) => `${step.key}:${step.state}`)).toEqual(expected)
})
diff --git a/products/workflows/frontend/Transactional/log/emailDeliverySteps.ts b/products/workflows/frontend/Transactional/log/emailDeliverySteps.ts
index dad6072cdd86..6ccf3c66759c 100644
--- a/products/workflows/frontend/Transactional/log/emailDeliverySteps.ts
+++ b/products/workflows/frontend/Transactional/log/emailDeliverySteps.ts
@@ -9,7 +9,7 @@ import type {
import { EMAIL_STATUS_LABEL, emailReasonText } from './emailLogStatus'
-export type DeliveryStepState = 'done' | 'failed' | 'pending'
+export type DeliveryStepState = 'done' | 'failed' | 'pending' | 'untracked'
export interface DeliveryStep {
key: string
@@ -45,6 +45,19 @@ function repeated(count: number, verb: string): string | null {
return count > 1 ? `${verb} ${count} times` : null
}
+function clickedStep(email: TransactionalEmailLogDetailApi, previousAt: string | null): DeliveryStep {
+ const clicked = firstOf(email.timeline, 'clicked')
+ const clickStep = step(
+ 'clicked',
+ 'Clicked',
+ clicked,
+ previousAt,
+ repeated(countOf(email.timeline, 'clicked'), 'Clicked')
+ )
+ // A send from before an email was marked sensitive can still have a recorded click.
+ return email.sensitive && !clicked ? { ...clickStep, state: 'untracked' } : clickStep
+}
+
/** The steps an email went through, ending where it stopped. */
export function buildDeliverySteps(email: TransactionalEmailLogDetailApi): DeliveryStep[] {
const { timeline } = email
@@ -96,13 +109,7 @@ export function buildDeliverySteps(email: TransactionalEmailLogDetailApi): Deliv
sentStep,
deliveredStep,
step('opened', 'Opened', opened, delivered?.at ?? null, repeated(countOf(timeline, 'opened'), 'Opened')),
- step(
- 'clicked',
- 'Clicked',
- firstOf(timeline, 'clicked'),
- opened?.at ?? delivered?.at ?? null,
- repeated(countOf(timeline, 'clicked'), 'Clicked')
- ),
+ clickedStep(email, opened?.at ?? delivered?.at ?? null),
]
}