: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
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'
import type { BillingFeatureType, BillingPlanType, BillingType, UserType } from '../../../types'
export interface PayGateMiniLogicProps {
feature: AvailableFeature
currentUsage?: number
}
export type GateVariantType = 'add-card' | 'contact-sales' | 'move-to-cloud' | null
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface payGateMiniLogicValues {
billing: BillingType | null // billingLogic
billingLoading: boolean // billingLogic
selfServeBillingDisabledReason: string | null // billingLogic
isCloudOrDev: boolean | undefined // preflightLogic
availableFeature: (feature: AvailableFeature) => BillingFeatureType | null | undefined // userLogic
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
user: UserType | null // userLogic
addonTrialModalOpen: boolean
bypassPaywall: boolean
ctaDisabledReason: string | null
ctaLabel: 'Contact sales' | 'Move to PostHog Cloud' | 'Upgrade now' | 'View plans'
ctaLink: string | undefined
featureAvailableOnOrg: BillingFeatureType | null | undefined
featureInfo: BillingFeatureType | undefined
featureInfoOnNextPlan: BillingFeatureType | undefined
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null
isAddonProduct: boolean | undefined
isGated: boolean
isPaymentEntryFlow: boolean
minimumPlanWithFeature: BillingPlanType | undefined
nextPlanWithFeature: BillingPlanType | null | undefined
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface payGateMiniLogicActions {
closeAddonTrialModal: () => {
value: true
}
openAddonTrialModal: () => {
value: true
}
setBypassPaywall: (bypassPaywall: boolean) => {
bypassPaywall: boolean
}
setGateVariant: (gateVariant: GateVariantType) => {
gateVariant: GateVariantType
}
startPaymentEntryFlow: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface payGateMiniLogicMeta {
key: AvailableFeature
__keaTypeGenInternalSelectorTypes: {
productWithFeature: (
billing: BillingType | null
) => BillingProductV2AddonType | BillingProductV2Type | undefined
isAddonProduct: (
billing: BillingType | null,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
) => boolean | undefined
featureInfo: (
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
) => BillingFeatureType | undefined
featureAvailableOnOrg: (user: UserType | null, arg: any) => BillingFeatureType | null | undefined
minimumPlanWithFeature: (
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
) => BillingPlanType | undefined
nextPlanWithFeature: (
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
) => BillingPlanType | null | undefined
featureInfoOnNextPlan: (
nextPlanWithFeature: BillingPlanType | null | undefined
) => BillingFeatureType | undefined
gateVariant: (
billingLoading: boolean,
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean,
minimumPlanWithFeature: BillingPlanType | undefined,
arg: any,
arg2: any
) => 'add-card' | 'contact-sales' | 'move-to-cloud' | null
ctaLink: (
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined,
featureInfo: BillingFeatureType | undefined
) => string | undefined
ctaLabel: (
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
isPaymentEntryFlow: boolean
) => 'Contact sales' | 'Move to PostHog Cloud' | 'Upgrade now' | 'View plans'
isPaymentEntryFlow: (
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
isAddonProduct: boolean | undefined,
billing: BillingType | null
) => boolean
ctaDisabledReason: (isPaymentEntryFlow: boolean, selfServeBillingDisabledReason: string | null) => string | null
isGated: (
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined,
featureInfo: BillingFeatureType | undefined,
bypassPaywall: boolean
) => boolean
}
}
export type payGateMiniLogicType = MakeLogicType<
payGateMiniLogicValues,
payGateMiniLogicActions,
PayGateMiniLogicProps,
payGateMiniLogicMeta
>
export const payGateMiniLogic = kea<payGateMiniLogicType>([
props({} as PayGateMiniLogicProps),
path(['lib', 'components', 'payGateMini', 'payGateMiniLogic']),
key((props) => props.feature),
connect(() => ({
values: [
billingLogic,
['billing', 'billingLoading', 'selfServeBillingDisabledReason'],
userLogic,
['user', 'hasAvailableFeature', 'availableFeature'],
preflightLogic,
['isCloudOrDev'],
],
actions: [],
})),
reducers({
bypassPaywall: [
false,
{
setBypassPaywall: (_, { bypassPaywall }) => bypassPaywall,
},
],
addonTrialModalOpen: [
false,
{
openAddonTrialModal: () => true,
closeAddonTrialModal: () => false,
},
],
}),
actions({
setGateVariant: (gateVariant: GateVariantType) => ({ gateVariant }),
setBypassPaywall: (bypassPaywall: boolean) => ({ bypassPaywall }),
openAddonTrialModal: true,
closeAddonTrialModal: true,
startPaymentEntryFlow: true,
}),
selectors(({ values, props }) => ({
productWithFeature: [
(s) => [s.billing],
(billing: null | import('~/types').BillingType) => {
// TODO(@zach): revisit this logic after subscribe to all products is released
// There are some features where we want to check the product first
const checkProductFirst = [AvailableFeature.ORGANIZATIONS_PROJECTS]
let foundProduct: BillingProductV2Type | BillingProductV2AddonType | undefined = undefined
if (checkProductFirst.includes(props.feature)) {
foundProduct = billing?.products
.filter((plan) => !plan.legacy_product || plan.subscribed)
.find((product) => product.features?.some((f) => f.key === props.feature))
}
// Check addons first (if not included in checkProductFirst) since their features are rolled up into the parent
const allAddons = billing?.products?.map((product) => product.addons).flat() || []
if (!foundProduct) {
foundProduct = allAddons
.filter((plan) => !plan.legacy_product || plan.subscribed)
.find((addon) => addon.features?.some((f) => f.key === props.feature))
}
if (!foundProduct) {
foundProduct = billing?.products
.filter((plan) => !plan.legacy_product || plan.subscribed)
.find((product) => product.features?.some((f) => f.key === props.feature))
}
return foundProduct
},
],
isAddonProduct: [
(s) => [s.billing, s.productWithFeature],
(
billing: null | import('~/types').BillingType,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined
) =>
billing?.products?.some((product) =>
product.addons?.some((addon) => addon.type === productWithFeature?.type)
),
],
featureInfo: [
(s) => [s.productWithFeature],
(productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined) =>
productWithFeature?.features.find((f) => f.key === props.feature),
],
featureAvailableOnOrg: [
(s) => [s.user, (_, props) => props.feature],
(_user: null | import('~/types').UserType, feature) => {
return values.availableFeature(feature)
},
],
minimumPlanWithFeature: [
(s) => [s.productWithFeature],
(productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined) =>
productWithFeature?.plans.find((plan) => plan.features?.some((f) => f.key === props.feature)),
],
nextPlanWithFeature: [
(s) => [s.productWithFeature],
(productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined) => {
const currentPlanIndex = productWithFeature?.plans.findIndex((plan) => plan.current_plan)
if (currentPlanIndex === undefined || currentPlanIndex < 0) {
return null
}
return productWithFeature?.plans[currentPlanIndex + 1]
},
],
featureInfoOnNextPlan: [
(s) => [s.nextPlanWithFeature],
(nextPlanWithFeature: null | import('~/types').BillingPlanType | undefined) =>
nextPlanWithFeature?.features.find((f) => f.key === props.feature),
],
gateVariant: [
(s) => [
s.billingLoading,
s.hasAvailableFeature,
s.minimumPlanWithFeature,
(_, props) => props.feature,
(_, props) => props.currentUsage,
],
(
billingLoading: boolean,
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean,
minimumPlanWithFeature: import('~/types').BillingPlanType | undefined,
feature,
currentUsage
) => {
if (hasAvailableFeature(feature, currentUsage)) {
return null
}
if (billingLoading) {
return null
}
if (values.isCloudOrDev) {
if (!minimumPlanWithFeature || minimumPlanWithFeature.contact_support) {
return 'contact-sales'
}
return 'add-card'
}
return 'move-to-cloud'
},
],
ctaLink: [
(s) => [s.gateVariant, s.productWithFeature, s.featureInfo],
(
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined,
featureInfo: import('~/types').BillingFeatureType | undefined
) => {
// product activation is already handled in the startPaymentEntryFlow,
// ctaLink is used only when isPaymentEntryFlow is false
if (gateVariant === 'add-card') {
return `/organization/billing${productWithFeature?.type ? `?products=${productWithFeature.type}` : ''}`
} else if (gateVariant === 'contact-sales') {
return `mailto:[email protected]?subject=Inquiring about ${featureInfo?.name}`
} else if (gateVariant === 'move-to-cloud') {
return 'https://us.posthog.com/signup?utm_medium=in-product&utm_campaign=move-to-cloud'
}
return undefined
},
],
ctaLabel: [
(s) => [s.gateVariant, s.isPaymentEntryFlow],
(gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null, isPaymentEntryFlow: boolean) => {
if (gateVariant === 'contact-sales') {
return 'Contact sales'
}
if (gateVariant === 'move-to-cloud') {
return 'Move to PostHog Cloud'
}
if (isPaymentEntryFlow) {
return 'Upgrade now'
}
if (gateVariant === 'add-card') {
return 'View plans'
}
return 'Upgrade now'
},
],
isPaymentEntryFlow: [
(s) => [s.gateVariant, s.isAddonProduct, s.billing],
(
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
isAddonProduct: boolean | undefined,
billing: null | import('~/types').BillingType
): boolean => {
// Show payment entry flow only for free customers trying to upgrade to a paid plan
// to use core features (not addons)
return gateVariant === 'add-card' && !isAddonProduct && billing?.subscription_level === 'free'
},
],
ctaDisabledReason: [
(s) => [s.isPaymentEntryFlow, s.selfServeBillingDisabledReason],
(isPaymentEntryFlow: boolean, selfServeBillingDisabledReason: string | null): string | null =>
isPaymentEntryFlow ? selfServeBillingDisabledReason : null,
],
isGated: [
(s) => [s.gateVariant, s.productWithFeature, s.featureInfo, s.bypassPaywall],
(
gateVariant: 'add-card' | 'contact-sales' | 'move-to-cloud' | null,
productWithFeature: BillingProductV2AddonType | BillingProductV2Type | undefined,
featureInfo: import('~/types').BillingFeatureType | undefined,
bypassPaywall: boolean
): 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)
},
})),
])