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(onboarding): keep the query string when redirecting to onboarding (#128127)

Chris Shine2227aa621ee6
diff --git a/frontend/src/scenes/coupons/utils.test.ts b/frontend/src/scenes/coupons/utils.test.ts
index 6cf4ab7db03f..2fa342d2ae4d 100644
--- a/frontend/src/scenes/coupons/utils.test.ts
+++ b/frontend/src/scenes/coupons/utils.test.ts
@@ -19,6 +19,13 @@ describe('parseCouponCampaign', () => {
['/coupons/lenny?next=/home', 'lenny'],
['/onboarding/coupons/lenny?foo=bar', 'lenny'],
+ // With a hash
+ ['/coupons/lenny#terms', 'lenny'],
+
+ // Coupon path only inside the query string or hash
+ ['/home?ref=/coupons/lenny', null],
+ ['/home#/coupons/lenny', null],
+
// With trailing slash
['/coupons/lenny/', 'lenny'],
diff --git a/frontend/src/scenes/coupons/utils.ts b/frontend/src/scenes/coupons/utils.ts
index 631588a1866d..61fc72c5cf7e 100644
--- a/frontend/src/scenes/coupons/utils.ts
+++ b/frontend/src/scenes/coupons/utils.ts
@@ -1,14 +1,17 @@
/**
* 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 match = path.match(/\/coupons\/([^/?]+)/)
+ const pathname = path.split(/[?#]/)[0]
+ const match = pathname.match(/\/coupons\/([^/]+)/)
return match?.[1] ?? null
}
diff --git a/frontend/src/scenes/sceneLogic.test.ts b/frontend/src/scenes/sceneLogic.test.ts
index 643f83632f33..d45d3e49f832 100644
--- a/frontend/src/scenes/sceneLogic.test.ts
+++ b/frontend/src/scenes/sceneLogic.test.ts
@@ -1,6 +1,27 @@
-import { isOnboardingNotRequiredForPath, withForwardedSearchParams } from './sceneLogic'
+import { getOnboardingRedirectNextPath, isOnboardingNotRequiredForPath, withForwardedSearchParams } from './sceneLogic'
describe('sceneLogic', () => {
+ describe('getOnboardingRedirectNextPath', () => {
+ it.each<[string, string, string | undefined, string]>([
+ [
+ 'keeps the query string of the page that was blocked',
+ '/project/123/data-warehouse/new-source?kind=Vercel&utm_source=wizard',
+ undefined,
+ '/data-warehouse/new-source?kind=Vercel&utm_source=wizard',
+ ],
+ ['prefers an explicit next param', '/project/123/insights?a=1', '/dashboard?b=2', '/dashboard?b=2'],
+ [
+ 'ignores an off-site next param',
+ '/project/123/insights?a=1',
+ 'https://evil.example.com/dashboard',
+ '/insights?a=1',
+ ],
+ ])('%s', (_label, href, nextParam, expected) => {
+ const location = new URL(href, 'https://us.posthog.com') as unknown as Location
+ expect(getOnboardingRedirectNextPath(nextParam, location)).toBe(expected)
+ })
+ })
+
describe('isOnboardingNotRequiredForPath', () => {
// Regression guard: mid-onboarding users hitting auth/account round-trips (email-change
// confirmation, credential/passkey review) must not be bounced to /onboarding before the
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsx
index 0581b65488c3..4cde412d9ee6 100644
--- a/frontend/src/scenes/sceneLogic.tsx
+++ b/frontend/src/scenes/sceneLogic.tsx
@@ -216,6 +216,12 @@ export function isOnboardingNotRequiredForPath(pathname: string): boolean {
return pathPrefixesOnboardingNotRequiredFor.some((prefix) => path.startsWith(prefix))
}
+export function getOnboardingRedirectNextPath(nextParam: string | null | undefined, location: Location): string {
+ return (
+ getRelativeNextPath(nextParam, location) ?? `${removeProjectIdIfPresent(location.pathname)}${location.search}`
+ )
+}
+
const DelayedLoadingSpinner = (): JSX.Element => {
const [show, setShow] = useState(false)
useEffect(() => {
@@ -1035,14 +1041,12 @@ export const sceneLogic = kea<sceneLogicType>([
!isOnboardingRedirectSuppressed(user) &&
!isOnboardingNotRequiredForPath(location.pathname)
) {
- const nextUrl =
- getRelativeNextPath(params.searchParams.next, location) ??
- removeProjectIdIfPresent(location.pathname)
+ const nextUrl = getOnboardingRedirectNextPath(params.searchParams.next, location)
// Check if user is coming from a coupon campaign link
- const campaign = nextUrl ? parseCouponCampaign(nextUrl) : null
- if (campaign) {
- router.actions.replace(urls.onboarding({ campaign }), { next: nextUrl })
+ const couponCampaign = nextUrl ? parseCouponCampaign(nextUrl) : null
+ if (couponCampaign) {
+ router.actions.replace(urls.onboarding({ campaign: couponCampaign }), { next: nextUrl })
return
}