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(messaging): support per-link click-tracking opt-out in emails (#67308)

Harley Alexanderacbff0919709
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 c5d580df0a89..279347c5ffb1 100644
--- a/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
@@ -104,6 +104,21 @@ describe('EmailTrackingService', () => {
expect(out).not.toContain('target=')
})
+ it.each([
+ ['clicktracking="off"', '<body><a href="https://example.com" clicktracking="off">x</a></body>'],
+ ['data-ph-no-track', '<body><a href="https://example.com" data-ph-no-track>x</a></body>'],
+ ])('leaves the href untouched when the anchor opts out via %s', (_name, html) => {
+ const out = addTrackingToEmail(html, invocation, signer)
+ expect(out).toContain('href="https://example.com"')
+ expect(out).not.toContain('target=')
+ })
+
+ it('still wraps the link when the opt-out marker is on a child, not the anchor tag', () => {
+ const html = '<body><a href="https://example.com"><span data-ph-no-track>x</span></a></body>'
+ const out = addTrackingToEmail(html, invocation, signer)
+ expect(out).toContain('target=')
+ })
+
const invocationWithDistinctId = {
functionId: 'fn-1',
id: 'inv-1',
diff --git a/nodejs/src/cdp/services/messaging/email-tracking.service.ts b/nodejs/src/cdp/services/messaging/email-tracking.service.ts
index 2c50f1494bfa..74fb6f950c42 100644
--- a/nodejs/src/cdp/services/messaging/email-tracking.service.ts
+++ b/nodejs/src/cdp/services/messaging/email-tracking.service.ts
@@ -22,6 +22,9 @@ export const PIXEL_GIF = Buffer.from('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAA
const LINK_REGEX =
/<a\b[^>]*\bhref\s*=\s*(?:"(?!javascript:)([^"]*)"|'(?!javascript:)([^']*)'|(?!javascript:)([^'">\s]+))[^>]*>([\s\S]*?)<\/a>/gi
+// Anchors carrying either opt-out marker are left unwrapped (no click-tracking redirect).
+const LINK_TRACKING_OPT_OUT_REGEX = /\bclicktracking\s*=\s*["']?off["']?|\bdata-ph-no-track\b/i
+
const trackingEventsCounter = new Counter({
name: 'email_tracking_events_total',
help: 'Total number of email tracking events received',
@@ -110,6 +113,15 @@ export const addTrackingToEmail = (
if (/^\s*javascript:/i.test(href)) {
return m
}
+ // Per-link opt-out. Leaves the href on its own domain so mobile universal links /
+ // app deeplinks resolve - routing through our cross-domain redirect breaks them.
+ // `clicktracking="off"` is the ESP de-facto standard; `data-ph-no-track` is our name.
+ // Match only the opening <a> tag so a marker in the link's inner HTML (a child
+ // element's attribute or literal link text) can't silently disable tracking.
+ const openingTag = m.match(/^<a\b[^>]*>/i)?.[0] ?? ''
+ if (LINK_TRACKING_OPT_OUT_REGEX.test(openingTag)) {
+ return m
+ }
const tracked = signer.redirectUrl(trackingInvocation, href, isTest)
// replace just the href in the original tag to preserve other attributes
diff --git a/products/workflows/skills/designing-email-templates/SKILL.md b/products/workflows/skills/designing-email-templates/SKILL.md
index eeadb0be7219..e98b4a1b3afe 100644
--- a/products/workflows/skills/designing-email-templates/SKILL.md
+++ b/products/workflows/skills/designing-email-templates/SKILL.md
@@ -31,6 +31,16 @@ Marketing emails must include an unsubscribe link — render it with the built-i
(`{{ unsubscribe_url_one_click }}` is also available for one-click list-unsubscribe flows.)
+## Click tracking and opt-out
+
+Every link is automatically rewritten through a click-tracking redirect. This breaks mobile universal links / app deeplinks, which only resolve when the href stays on their own domain. To keep a link untracked, mark its anchor (use an `html` block) with `clicktracking="off"` or `data-ph-no-track`:
+
+```html
+<a href="https://app.example.com/deeplink" data-ph-no-track>Open in app</a>
+```
+
+The marker must be on the `<a>` tag itself, not a child element. Opted-out links get no click metrics.
+
## Creating a template
Call `workflows-create-email-template` with: