: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
feat(cdp): support the unsubscribe_url liquid tag in emails (#123310)
Harley Alexander67f4ed6e358a
diff --git a/nodejs/src/cdp/utils/liquid.test.ts b/nodejs/src/cdp/utils/liquid.test.tsindex 05ef8f8e8d32..ddaa138ce0e5 100644--- a/nodejs/src/cdp/utils/liquid.test.ts+++ b/nodejs/src/cdp/utils/liquid.test.ts@@ -131,6 +131,30 @@ describe('LiquidRenderer', () => {})})+ describe('unsubscribe_url tag', () => {+ const url = 'https://example.com/preferences?token=abc&team=1'+ const escapedUrl = 'https://example.com/preferences?token=abc&team=1'++ // Each tag row sits next to the variable row it has to match.+ it.each([+ ['<a href="{{ unsubscribe_url }}">x</a>', true, url, `<a href="${escapedUrl}">x</a>`],+ ['<a href="{% unsubscribe_url %}">x</a>', true, url, `<a href="${escapedUrl}">x</a>`],+ ['<a href=" {%- unsubscribe_url -%} ">x</a>', true, url, `<a href="${escapedUrl}">x</a>`],+ ['Unsubscribe: {{ unsubscribe_url }}', false, url, `Unsubscribe: ${url}`],+ ['Unsubscribe: {% unsubscribe_url %}', false, url, `Unsubscribe: ${url}`],+ ['<a href="{{ unsubscribe_url }}">x</a>', true, undefined, '<a href="">x</a>'],+ ['<a href="{% unsubscribe_url %}">x</a>', true, undefined, '<a href="">x</a>'],+ ])('renders %s with escapeHtml %s', (template, escapeHtml, unsubscribe_url, expected) => {+ const result = LiquidRenderer.renderWithHogFunctionGlobals(+ template,+ { ...globals, unsubscribe_url },+ undefined,+ { escapeHtml }+ )+ expect(result).toBe(expected)+ })+ })+describe('HTML decoding', () => {it('decodes HTML entities', () => {const template = '{{ "<div>Hello & World</div>" }}'diff --git a/nodejs/src/cdp/utils/liquid.ts b/nodejs/src/cdp/utils/liquid.tsindex 44f3c87d76ba..602c32ee7456 100644--- a/nodejs/src/cdp/utils/liquid.ts+++ b/nodejs/src/cdp/utils/liquid.ts@@ -1,4 +1,4 @@-import { Liquid } from 'liquidjs'+import { Context, Emitter, Liquid, Tag, TagToken, Template, TopLevelToken } from 'liquidjs'import { rowScopedTriggerTypeForEvent } from '../schema/hogflow'import { HogFunctionInvocationGlobalsWithInputs } from '../types'@@ -186,6 +186,22 @@ const formatCurrency = (value: unknown, first?: unknown, second?: unknown): unknreturn currencyFormatter(first, second)?.format(amount) ?? value}+// Customer.io templates write the unsubscribe link as a tag, `{% unsubscribe_url %}`, where we expose a+// variable. The tag renders `{{ unsubscribe_url }}`, so it gets the same escaping as the variable and+// renders empty when the send has no unsubscribe URL.+class UnsubscribeUrlTag extends Tag {+ private readonly templates: Template[]++ constructor(token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) {+ super(token, remainTokens, liquid)+ this.templates = liquid.parse('{{ unsubscribe_url }}')+ }++ *render(ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {+ yield this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)+ }+}+const createLiquid = (outputEscape: 'escape' | undefined): Liquid => {const liquid = new Liquid({outputEscape,@@ -202,6 +218,7 @@ const createLiquid = (outputEscape: 'escape' | undefined): Liquid => {})}liquid.registerFilter('currency', formatCurrency)+ liquid.registerTag('unsubscribe_url', UnsubscribeUrlTag)return liquid}