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): stop redirect loop after completing onboarding (#72294)

Fernando Gomes7bd899a75a72
diff --git a/frontend/src/scenes/onboarding/legacy/billing/OnboardingUpgradeStep.tsx b/frontend/src/scenes/onboarding/legacy/billing/OnboardingUpgradeStep.tsx
index 9a779ecdaea6..5ce091d4e414 100644
--- a/frontend/src/scenes/onboarding/legacy/billing/OnboardingUpgradeStep.tsx
+++ b/frontend/src/scenes/onboarding/legacy/billing/OnboardingUpgradeStep.tsx
@@ -58,13 +58,14 @@ export const OnboardingUpgradeStep: OnboardingStepComponentType<OnboardingUpgrad
<OnboardingStep
title="Select a plan"
stepKey={OnboardingStepKey.PLANS}
- // The packages screen carries its own heading and its own Skip/Next nav (below the cards),
- // so the "Select a plan" title (misleading once subscribed) and the bottom Next are dropped there.
- hideTitle={showPlatformPackages}
+ // Once subscribed there's no plan left to select — both the celebration screen and the
+ // packages screen (which carries its own heading) contradict the "Select a plan" title,
+ // so drop it. The packages screen also brings its own Skip/Next nav below the cards.
+ hideTitle={!!product.subscribed}
showContinue={!!product.subscribed && !showPlatformPackages}
>
{!product.subscribed && <PlanCards product={product} />}
- {product.subscribed && !showPlatformPackages && <SubscribedCelebration product={product} />}
+ {product.subscribed && !showPlatformPackages && <SubscribedCelebration />}
{showPlatformPackages && platformProduct && (
<PlatformPackagesUpsell
platformProduct={platformProduct}
@@ -79,7 +80,7 @@ export const OnboardingUpgradeStep: OnboardingStepComponentType<OnboardingUpgrad
}
OnboardingUpgradeStep.stepKey = OnboardingStepKey.PLANS
-const SubscribedCelebration = ({ product }: { product: BillingProductV2Type }): JSX.Element => {
+const SubscribedCelebration = (): JSX.Element => {
const { trigger, HogfettiComponent } = useHogfetti({ count: 100, duration: 3000 })
useEffect(() => {
@@ -104,9 +105,8 @@ const SubscribedCelebration = ({ product }: { product: BillingProductV2Type }):
</div>
<h3 className="text-2xl font-bold mt-6">Go forth and build amazing products!</h3>
- <p className="text-gray-700 dark:text-gray-400">
- You've unlocked all features for <strong>{product.name}</strong>.
- </p>
+ {/* Subscribing unlocks every product, so don't scope this to the product being onboarded. */}
+ <p className="text-gray-700 dark:text-gray-400">You're all signed up. You've unlocked all features.</p>
</div>
)
}
diff --git a/frontend/src/scenes/teamLogic.test.ts b/frontend/src/scenes/teamLogic.test.ts
index 999d94c21bd4..e7e1726faa2a 100644
--- a/frontend/src/scenes/teamLogic.test.ts
+++ b/frontend/src/scenes/teamLogic.test.ts
@@ -3,8 +3,9 @@ import { MOCK_DEFAULT_PROJECT, MOCK_DEFAULT_TEAM, MOCK_TEAM_ID } from 'lib/api.m
import { expectLogic } from 'kea-test-utils'
import { useMocks } from '~/mocks/jest'
+import { ProductKey } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
-import { AppContext } from '~/types'
+import { AppContext, TeamType } from '~/types'
import { projectLogic } from './projectLogic'
import { teamLogic } from './teamLogic'
@@ -68,6 +69,75 @@ describe('teamLogic', () => {
})
})
+ describe('product intent loaders', () => {
+ beforeEach(() => {
+ initKeaTests()
+ useMocks({
+ get: {
+ '/api/environments/:id/user_product_list': () => [200, { results: [], count: 0 }],
+ },
+ patch: {
+ // Simulates the race hit in production: `complete_product_onboarding` serializes
+ // the team from a snapshot taken before the concurrent onboarding-completion
+ // PATCH committed, so its response arrives without the completion fields.
+ '/api/environments/:id/complete_product_onboarding': () => [
+ 200,
+ {
+ ...MOCK_DEFAULT_TEAM,
+ completed_snippet_onboarding: false,
+ has_completed_onboarding_for: {},
+ product_intents: [{ product_type: 'session_replay' }],
+ },
+ ],
+ },
+ })
+ logic = teamLogic()
+ logic.mount()
+ })
+
+ it('keeps onboarding-completion fields when a stale team snapshot comes back', async () => {
+ await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])
+
+ await expectLogic(logic, () => {
+ logic.actions.recordProductIntentOnboardingComplete({ product_type: ProductKey.SESSION_REPLAY })
+ }).toDispatchActions(['recordProductIntentOnboardingCompleteSuccess'])
+
+ // Fresh local fields survive; only product_intents is taken from the response.
+ // A wholesale replace here would flip hasOnboardedAnyProduct back to false and
+ // send the user into the /onboarding redirect loop.
+ expect(logic.values.currentTeam?.completed_snippet_onboarding).toBe(true)
+ expect(logic.values.currentTeam?.has_completed_onboarding_for).toEqual({ product_analytics: true })
+ expect(logic.values.hasOnboardedAnyProduct).toBe(true)
+ expect((logic.values.currentTeam as TeamType)?.product_intents).toEqual([
+ { product_type: 'session_replay' },
+ ])
+ })
+
+ it('ignores a response for a different team (team switched mid-flight)', async () => {
+ await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])
+ useMocks({
+ patch: {
+ '/api/environments/:id/complete_product_onboarding': () => [
+ 200,
+ {
+ ...MOCK_DEFAULT_TEAM,
+ id: MOCK_TEAM_ID + 1,
+ product_intents: [{ product_type: 'surveys' }],
+ },
+ ],
+ },
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.recordProductIntentOnboardingComplete({ product_type: ProductKey.SURVEYS })
+ }).toDispatchActions(['recordProductIntentOnboardingCompleteSuccess'])
+
+ // The stale team's intents must not be grafted onto the team that is now active.
+ expect(logic.values.currentTeam?.id).toBe(MOCK_TEAM_ID)
+ expect((logic.values.currentTeam as TeamType)?.product_intents).toBeUndefined()
+ })
+ })
+
describe('before team is loaded', () => {
beforeEach(() => {
initKeaTests(false)
diff --git a/frontend/src/scenes/teamLogic.tsx b/frontend/src/scenes/teamLogic.tsx
index a369a3ebfbdb..58b47733a155 100644
--- a/frontend/src/scenes/teamLogic.tsx
+++ b/frontend/src/scenes/teamLogic.tsx
@@ -50,6 +50,29 @@ export function isAuthenticatedTeam(team: TeamType | TeamPublicType | undefined
return !!team && 'api_token' in team
}
+/**
+ * Product-intent endpoints echo a full team serialized from a snapshot taken when their request
+ * started. Replacing `currentTeam` wholesale with that snapshot can clobber fields committed by a
+ * concurrent team PATCH — e.g. the onboarding-completion PATCH racing `complete_product_onboarding`
+ * loses `has_completed_onboarding_for`, so `hasOnboardedAnyProduct` flips back to false and
+ * sceneLogic bounces the user into /onboarding on every navigation. Keep the local team and take
+ * only `product_intents`, the one field these endpoints actually change.
+ */
+function withProductIntentsFrom(
+ currentTeam: TeamType | TeamPublicType | null,
+ response: TeamType | null
+): TeamType | TeamPublicType | null {
+ if (!currentTeam || !response) {
+ return response ?? currentTeam
+ }
+ if (response.id !== currentTeam.id) {
+ // The response is for a different team than the one now active (the team switched while
+ // the request was in flight) — don't graft the old team's intents onto the current one.
+ return currentTeam
+ }
+ return { ...currentTeam, product_intents: response.product_intents }
+}
+
export interface FrequentMistakeAdvice {
key: string
type: 'event' | 'person'
@@ -116,17 +139,17 @@ export interface teamLogicActions {
errorObject?: any
}
addProductIntentForCrossSellSuccess: (
- currentTeam: TeamType | null,
+ currentTeam: TeamPublicType | TeamType | null,
payload?: ProductCrossSellProperties
) => {
- currentTeam: TeamType | null
+ currentTeam: TeamPublicType | TeamType | null
payload?: ProductCrossSellProperties
}
addProductIntentSuccess: (
- currentTeam: TeamType | null,
+ currentTeam: TeamPublicType | TeamType | null,
payload?: ProductIntentProperties
) => {
- currentTeam: TeamType | null
+ currentTeam: TeamPublicType | TeamType | null
payload?: ProductIntentProperties
}
createTeam: ({ name, is_demo }: { is_demo: boolean; name: string }) => {
@@ -450,13 +473,13 @@ export const teamLogic = kea<teamLogicType>([
const result = await addProductIntent(properties)
actions.loadCustomProducts()
- return result
+ return withProductIntentsFrom(values.currentTeam, result)
},
addProductIntentForCrossSell: async (properties: ProductCrossSellProperties) => {
const result = await addProductIntentForCrossSell(properties)
actions.loadCustomProducts()
- return result
+ return withProductIntentsFrom(values.currentTeam, result)
},
recordProductIntentOnboardingComplete: async ({ product_type }: { product_type: ProductKey }) => {
const result = await api.update(
@@ -467,7 +490,7 @@ export const teamLogic = kea<teamLogicType>([
)
actions.loadCustomProducts()
- return result
+ return withProductIntentsFrom(values.currentTeam, result)
},
},
],