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.

764 B
Raw Blame
/**
 * Extracts campaign slug from a coupon URL path.
 * Matches both `/coupons/:campaign` and `/onboarding/coupons/:campaign` patterns.
 * Ignores the query string and hash, so a query value such as `?ref=/coupons/x` doesn't start a coupon onboarding.
 *
 * @example
 * parseCouponCampaign('/coupons/lenny') // 'lenny'
 * parseCouponCampaign('/onboarding/coupons/lenny') // 'lenny'
 * parseCouponCampaign('/project/123/onboarding/coupons/lenny') // 'lenny'
 * parseCouponCampaign('/other/path') // null
 * parseCouponCampaign('/home?ref=/coupons/lenny') // null
 */
export function parseCouponCampaign(path: string): string | null {
    const pathname = path.split(/[?#]/)[0]
    const match = pathname.match(/\/coupons\/([^/]+)/)
    return match?.[1] ?? null
}