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.

perf(billing): load the payment entry logic when the pay gate is clicked (#127236)

Paul D'Ambra1205de907af6
diff --git a/frontend/bin/check-eager-graph.mjs b/frontend/bin/check-eager-graph.mjs
index 335d65d4367a..3f5e39ae6030 100644
--- a/frontend/bin/check-eager-graph.mjs
+++ b/frontend/bin/check-eager-graph.mjs
@@ -104,8 +104,8 @@ const ROOTS = [
{
root: AUTHENTICATED_SHELL,
label: 'authenticated shell (every logged-in page)',
- // 2026-11-01: 4.06 MiB eager output, stable CSS included. ~0.3% headroom.
- budgetBytes: 4_270_000,
+ // 2026-11-02: 4.02 MiB eager output, stable CSS included. ~0.3% headroom.
+ budgetBytes: 4_230_000,
forbidden: [
'node_modules/monaco-editor/',
'products/product_analytics/frontend/insights/activityDescriptions',
@@ -138,6 +138,8 @@ const ROOTS = [
'src/layout/today/TodayTopBar.tsx',
// The rail's pane actions pull in the new-analytics menu and the canvas block library.
'src/layout/today/todayRailItems.tsx',
+ // The pay gate loads the payment logic on click. It pulls in the billing product logic.
+ 'src/scenes/billing/paymentEntryLogic.ts',
// The rich text editor stack is for the scenes that edit text. Rendered markdown needs none of it.
'node_modules/@tiptap/',
'node_modules/prosemirror-',
@@ -192,8 +194,8 @@ const ROOTS = [
'src/scenes/project-homepage/today/TodayHome.tsx',
],
label: 'today home path',
- // 2026-11-01: 4.97 MiB, stable CSS included. ~0.3% headroom.
- budgetBytes: 5_225_000,
+ // 2026-11-02: 4.93 MiB, stable CSS included. ~0.3% headroom.
+ budgetBytes: 5_186_000,
forbidden: [
'src/scenes/project-homepage/ai-first/AiFirstHomepage.tsx',
'src/scenes/project-homepage/today/TodayReportPage.tsx',
diff --git a/frontend/bin/layer-baseline.txt b/frontend/bin/layer-baseline.txt
index 6bb0156460d3..9c99ebfc834f 100644
--- a/frontend/bin/layer-baseline.txt
+++ b/frontend/bin/layer-baseline.txt
@@ -211,7 +211,6 @@ frontend/src/lib/components/JSSnippet.tsx -> frontend/src/scenes/settings/enviro
frontend/src/lib/components/NotFound/index.tsx -> frontend/src/scenes/activity/explore/defaults.ts
frontend/src/lib/components/NotFound/index.tsx -> frontend/src/scenes/notebooks/Nodes/NotebookNodeContext.ts
frontend/src/lib/components/PathCleanFilters/PathRegexModal.tsx -> frontend/src/scenes/session-recordings/components/AiRegexHelper/AiRegexHelper.tsx
-frontend/src/lib/components/PayGateMini/PayGateButton.tsx -> frontend/src/scenes/billing/paymentEntryLogic.ts
frontend/src/lib/components/PayGateMini/PayGateMini.tsx -> frontend/src/scenes/onboarding/shared/utils.tsx
frontend/src/lib/components/ProductEmptyState/ProductEmptyState.tsx -> frontend/src/scenes/onboarding/shared/useWizardCommand.ts
frontend/src/lib/components/PropertiesTable/PropertiesTable.tsx -> products/persons/frontend/components/NewProperty.tsx
diff --git a/frontend/src/lib/components/PayGateMini/PayGateButton.tsx b/frontend/src/lib/components/PayGateMini/PayGateButton.tsx
index d55e7b3139d7..db1d8b8b0877 100644
--- a/frontend/src/lib/components/PayGateMini/PayGateButton.tsx
+++ b/frontend/src/lib/components/PayGateMini/PayGateButton.tsx
@@ -4,10 +4,6 @@ import posthog from 'posthog-js'
import { IconLock } from '@posthog/icons'
import { LemonButton, LemonButtonProps } from '@posthog/lemon-ui'
-import { paymentEntryLogic } from 'scenes/billing/paymentEntryLogic'
-
-import { BillingProductV2Type } from '~/types'
-
import { PayGateMiniLogicProps, payGateMiniLogic } from './payGateMiniLogic'
import type { payGateMiniLogicType } from './payGateMiniLogic'
@@ -23,18 +19,15 @@ function usePayGateButton({
currentUsage,
onClick,
}: PayGateMiniLogicProps & Pick<LemonButtonProps, 'onClick'>): UsePayGateButtonReturn {
- const { productWithFeature, ctaLink, ctaLabel, ctaDisabledReason, gateVariant, isPaymentEntryFlow } = useValues(
- payGateMiniLogic({ feature, currentUsage })
- )
- const { startPaymentEntryFlow } = useActions(paymentEntryLogic)
+ const logic = payGateMiniLogic({ feature, currentUsage })
+ const { productWithFeature, ctaLink, ctaLabel, ctaDisabledReason, gateVariant, isPaymentEntryFlow } =
+ useValues(logic)
+ const { startPaymentEntryFlow } = useActions(logic)
const clickHandlerProps = isPaymentEntryFlow
? {
onClick: (ev: React.MouseEvent<HTMLButtonElement>) => {
- startPaymentEntryFlow(
- productWithFeature as BillingProductV2Type,
- window.location.pathname + window.location.search
- )
+ startPaymentEntryFlow()
if (onClick) {
onClick(ev)
}
diff --git a/frontend/src/lib/components/PayGateMini/payGateMiniLogic.test.ts b/frontend/src/lib/components/PayGateMini/payGateMiniLogic.test.ts
index d4a7cc445de2..afdd5c3b1da8 100644
--- a/frontend/src/lib/components/PayGateMini/payGateMiniLogic.test.ts
+++ b/frontend/src/lib/components/PayGateMini/payGateMiniLogic.test.ts
@@ -3,12 +3,13 @@ import { expectLogic } from 'kea-test-utils'
import { billingLogic } from 'lib/logic/billing/billingLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
+import { paymentEntryLogic } from 'scenes/billing/paymentEntryLogic'
import { billingJson } from '~/mocks/fixtures/_billing'
import preflightJson from '~/mocks/fixtures/_preflight.json'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
-import { AvailableFeature, BillingProvider, BillingType } from '~/types'
+import { AvailableFeature, BillingProductV2Type, BillingProvider, BillingType } from '~/types'
import { payGateMiniLogic } from './payGateMiniLogic'
@@ -68,4 +69,32 @@ describe('payGateMiniLogic', () => {
expect(logic.values).toMatchObject({ gateVariant: 'add-card', ...expected })
})
+
+ it('starts the payment entry flow for the gated product and the current page after the gate closes', async () => {
+ useMocks({
+ get: {
+ '/_preflight': [200, { ...preflightJson, cloud: true }],
+ '/api/billing': [200, { ...billingJson, customer_id: '', subscription_level: 'free' }],
+ },
+ })
+ initKeaTests()
+ await expectLogic(preflightLogic).toFinishAllListeners()
+ billingLogic.mount()
+ await expectLogic(billingLogic, () => billingLogic.actions.loadBilling()).toFinishAllListeners()
+ const logic = payGateMiniLogic({ feature: AvailableFeature.SUBSCRIPTIONS })
+ logic.mount()
+ const product = logic.values.productWithFeature as BillingProductV2Type
+ window.history.pushState({}, '', '/project/1/insights?tab=all')
+
+ await expectLogic(paymentEntryLogic, () => {
+ logic.actions.startPaymentEntryFlow()
+ logic.unmount()
+ }).toDispatchActions([
+ (action) =>
+ paymentEntryLogic.isMounted() &&
+ action.type === paymentEntryLogic.actionTypes.startPaymentEntryFlow &&
+ action.payload.product === product &&
+ action.payload.redirectPath === '/project/1/insights?tab=all',
+ ])
+ })
})
diff --git a/frontend/src/lib/components/PayGateMini/payGateMiniLogic.tsx b/frontend/src/lib/components/PayGateMini/payGateMiniLogic.tsx
index 13499cecd4a7..381ff5b4d797 100644
--- a/frontend/src/lib/components/PayGateMini/payGateMiniLogic.tsx
+++ b/frontend/src/lib/components/PayGateMini/payGateMiniLogic.tsx
@@ -1,7 +1,11 @@
-import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
+import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+import posthog from 'posthog-js'
+import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { billingLogic } from 'lib/logic/billing/billingLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
+import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
+import { retryImport } from 'lib/utils/retryImport'
import { userLogic } from 'scenes/userLogic'
import { AvailableFeature, BillingProductV2AddonType, BillingProductV2Type } from '~/types'
@@ -55,6 +59,9 @@ export interface payGateMiniLogicActions {
setGateVariant: (gateVariant: GateVariantType) => {
gateVariant: GateVariantType
}
+ startPaymentEntryFlow: () => {
+ value: true
+ }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -154,6 +161,7 @@ export const payGateMiniLogic = kea<payGateMiniLogicType>([
setBypassPaywall: (bypassPaywall: boolean) => ({ bypassPaywall }),
openAddonTrialModal: true,
closeAddonTrialModal: true,
+ startPaymentEntryFlow: true,
}),
selectors(({ values, props }) => ({
productWithFeature: [
@@ -322,4 +330,29 @@ export const payGateMiniLogic = kea<payGateMiniLogicType>([
): boolean => !!gateVariant && !!productWithFeature && !!featureInfo && !bypassPaywall,
],
})),
+ listeners(({ values }) => ({
+ startPaymentEntryFlow: async () => {
+ // The upgrade modal closes on click, so this gate can unmount before the import resolves.
+ const product = values.productWithFeature as BillingProductV2Type
+ const redirectPath = window.location.pathname + window.location.search
+ let paymentEntryModule: typeof import('scenes/billing/paymentEntryLogic')
+ try {
+ // The billing scene's payment logic is large, so it loads only when someone clicks to upgrade.
+ paymentEntryModule = await retryImport(() => import('scenes/billing/paymentEntryLogic'))
+ } catch (error) {
+ if (!isChunkLoadError(error)) {
+ posthog.captureException(error)
+ }
+ lemonToast.error("Couldn't open the payment form. Reload the page and try again.", {
+ button: { label: 'Reload', action: () => window.location.reload() },
+ })
+ return
+ }
+ const { paymentEntryLogic } = paymentEntryModule
+ // Not tied to this gate's lifetime: the payment modal is global and outlives the gate.
+ // GlobalModals keeps this logic mounted for the session too, so the extra mount holds nothing new.
+ paymentEntryLogic.mount()
+ paymentEntryLogic.actions.startPaymentEntryFlow(product, redirectPath)
+ },
+ })),
])