: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { addUtmTagsToEmail, resolveUtmTags } from './utm'
const TAGS = { utm_source: 'posthog', utm_medium: 'email', utm_campaign: 'Spring sale', utm_content: '' }
const SITE_URL = 'https://us.posthog.com'
const QUERY = 'utm_source=posthog&utm_medium=email&utm_campaign=Spring%20sale'
describe('addUtmTagsToEmail', () => {
it.each<{ case: string; html: string; expected: string; fixedScanner?: boolean }>([
{
case: 'a plain link',
html: '<a href="https://example.com/pricing">x</a>',
expected: `<a href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'a link with a query and a fragment',
html: '<a href="https://example.com/p?a=1&b=2#plans">x</a>',
expected: `<a href="https://example.com/p?a=1&b=2&${QUERY}#plans">x</a>`,
},
{
case: 'a link that already sets its campaign',
html: "<a class='btn' href='https://example.com/?utm_campaign=launch'>x</a>",
expected:
"<a class='btn' href='https://example.com/?utm_campaign=launch&utm_source=posthog&utm_medium=email'>x</a>",
},
{
case: 'a link that already has every tag',
html: '<a href="https://example.com/?utm_source=a&utm_medium=b&utm_campaign=c">x</a>',
expected: '<a href="https://example.com/?utm_source=a&utm_medium=b&utm_campaign=c">x</a>',
},
{
case: 'the unsubscribe link on the PostHog host',
html: '<a href="https://us.posthog.com/messaging-preferences/abc/">Unsubscribe</a>',
expected: '<a href="https://us.posthog.com/messaging-preferences/abc/">Unsubscribe</a>',
},
{
case: 'mailto, relative and anchor links',
html: '<a href="mailto:[email protected]">m</a><a href="/docs">r</a><a href="#top">a</a>',
expected: '<a href="mailto:[email protected]">m</a><a href="/docs">r</a><a href="#top">a</a>',
},
{
case: 'a link marked data-ph-no-utm',
html: '<a data-ph-no-utm href="https://example.com/pricing">x</a>',
expected: '<a data-ph-no-utm href="https://example.com/pricing">x</a>',
},
{
case: 'a link with data-href before href',
html: '<a data-href="x" href="https://example.com/pricing">x</a>',
expected: `<a data-href="x" href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'a link whose title mentions data-ph-no-utm',
html: '<a title="add data-ph-no-utm to skip" href="https://example.com/pricing">x</a>',
expected: `<a title="add data-ph-no-utm to skip" href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'an <abbr> next to a link',
html: '<abbr title="x">PH</abbr><a href="https://example.com/pricing">x</a>',
expected: `<abbr title="x">PH</abbr><a href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'a quoted > inside the tag',
html: '<a title="a > b" href="https://example.com/pricing">x</a>',
expected: `<a title="a > b" href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'an unclosed tag after a link',
html: '<a href="https://example.com/pricing">x</a> <a <a <a',
expected: `<a href="https://example.com/pricing?${QUERY}">x</a> <a <a <a`,
},
{
case: 'an unrendered template tag',
html: '<a href="https://example.com/{{ person.id }}">x</a>',
expected: '<a href="https://example.com/{{ person.id }}">x</a>',
},
{
case: 'an unquoted href',
html: '<a href=https://example.com/pricing>x</a>',
expected: `<a href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'an unquoted href with a query',
html: '<a href=https://example.com/pricing?plan=pro>x</a>',
expected: `<a href="https://example.com/pricing?plan=pro&${QUERY}">x</a>`,
},
{
case: 'a quoted and an unquoted href with the fixed scanner on for the team',
html: '<a href="https://example.com/a">x</a><a href=https://example.com/b>y</a>',
expected: `<a href="https://example.com/a?${QUERY}">x</a><a href="https://example.com/b?${QUERY}">y</a>`,
fixedScanner: true,
},
{
case: "a quoted and an unquoted href with the fixed scanner off for the team, which keeps master's scanner",
html: '<a href="https://example.com/a">x</a><a href=https://example.com/b>y</a>',
expected: `<a href="https://example.com/a?${QUERY}">x</a><a href=https://example.com/b>y</a>`,
fixedScanner: false,
},
{
case: 'an unquoted href with a non-breaking space, which HTML keeps in the value',
html: '<a href=https://example.com/summer sale>x</a>',
expected: `<a href="https://example.com/summer sale?${QUERY}">x</a>`,
},
{
case: 'an href with a trailing space',
html: '<a href="https://example.com/pricing ">x</a>',
expected: `<a href="https://example.com/pricing?${QUERY}">x</a>`,
},
{
case: 'an href ending in a non-breaking space, which is part of the URL',
html: '<a href="https://example.com/pricing ">x</a>',
expected: `<a href="https://example.com/pricing\u00a0?${QUERY}">x</a>`,
},
{
case: 'links after text that grows when lowercased',
html: '<p>İstanbul, İzmir, İzmit</p><a data-ph-no-utm href="https://example.com/a">x</a><a href="https://example.com/b">y</a>',
expected: `<p>İstanbul, İzmir, İzmit</p><a data-ph-no-utm href="https://example.com/a">x</a><a href="https://example.com/b?${QUERY}">y</a>`,
},
])('tags $case', ({ html, expected, fixedScanner = true }) => {
expect(addUtmTagsToEmail(html, TAGS, SITE_URL, fixedScanner)).toEqual(expected)
})
it.each([
{
case: 'no custom values',
rendered: undefined,
expected: { utm_campaign: 'Spring sale', utm_content: 'Send email' },
},
{
case: 'a custom campaign and content',
rendered: { utm_campaign: 'pro-plan', utm_content: ' hero ' },
expected: { utm_campaign: 'pro-plan', utm_content: 'hero' },
},
{
case: 'a value that rendered empty',
rendered: { utm_campaign: ' ' },
expected: { utm_campaign: 'Spring sale' },
},
])('resolves the tags from $case', ({ rendered, expected }) => {
expect(resolveUtmTags({ ...TAGS, utm_content: 'Send email' }, rendered)).toMatchObject({
utm_source: 'posthog',
...expected,
})
})
})