: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, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { router } from 'kea-router'
import posthog from 'posthog-js'
import { getRelativeNextPath } from 'lib/utils/url'
import { onboardingLogic } from 'scenes/onboarding/onboardingLogic'
import { ONBOARDING_EVENT_PROPS, reportOnboardingStarted } from 'scenes/onboarding/onboardingUsage'
import { USE_CASE_OPTIONS, UseCaseOption, getRecommendedProducts } from 'scenes/onboarding/productRecommendations'
import { availableOnboardingProducts } from 'scenes/onboarding/shared/utils'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { OnboardingStepKey } from '~/types'
import type { ProductIntentProperties } from '../../../lib/utils/product-intents'
import type { TeamPublicType, TeamType } from '../../../types'
import type { UseCaseDefinition } from '../productRecommendations'
import {
getBrowsingHistoryFromPostHog,
getBrowsingHistoryLabels,
mapBrowsingHistoryToProducts,
} from './browsingHistoryMapping'
function reportOnboardingProductSelectionPath(
path: 'ai' | 'use_case' | 'browsing_history' | 'manual',
properties?: {
useCase?: string
recommendedProducts?: string[]
hasBrowsingHistory?: boolean
}
): void {
posthog.capture('onboarding product selection path', {
path,
use_case: properties?.useCase,
recommended_products: properties?.recommendedProducts,
has_browsing_history: properties?.hasBrowsingHistory,
})
}
export type OnboardingStep = 'choose_path' | 'product_selection'
export type RecommendationSource = 'use_case' | 'browsing_history' | 'manual' | 'simplified' | 'multiproduct'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface productSelectionLogicValues {
currentTeam: TeamPublicType | TeamType | null // teamLogic
browsingHistory: string[]
browsingHistoryLabels: string[]
browsingHistoryProducts: ProductKey[]
canContinue: boolean
currentStep: OnboardingStep
firstProductOnboarding: ProductKey | null
hasBrowsingHistory: boolean
otherProducts: ProductKey[]
recommendationSource: RecommendationSource
recommendationSourceLabel: string
recommendedProducts: ProductKey[]
selectedProducts: ProductKey[]
selectedUseCase: UseCaseOption | null
showAllProducts: boolean
useCaseProducts: ProductKey[]
useCases: readonly UseCaseDefinition[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface productSelectionLogicActions {
setOnCompleteOnboardingRedirectUrl: (url: string | null) => {
url: string | null
} // onboardingLogic
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
clearUseCase: () => {
value: true
}
handleStartOnboarding: () => {
value: true
}
selectPickMyself: () => {
value: true
}
selectSingleProduct: (productKey: ProductKey) => {
productKey: ProductKey
}
selectUseCase: (useCase: UseCaseOption) => {
useCase: UseCaseOption
}
setBrowsingHistory: (browsingHistory: string[]) => {
browsingHistory: string[]
}
setFirstProductOnboarding: (productKey: ProductKey | null) => {
productKey: ProductKey | null
}
setRecommendationSource: (source: RecommendationSource) => {
source: RecommendationSource
}
setSelectedProducts: (productKeys: ProductKey[]) => {
productKeys: ProductKey[]
}
setShowAllProducts: (show: boolean) => {
show: boolean
}
setStep: (step: OnboardingStep) => {
step: OnboardingStep
}
toggleProduct: (productKey: ProductKey) => {
productKey: ProductKey
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface productSelectionLogicMeta {
__keaTypeGenInternalSelectorTypes: {
browsingHistoryProducts: (browsingHistory: string[]) => ProductKey[]
browsingHistoryLabels: (browsingHistory: string[]) => string[]
hasBrowsingHistory: (browsingHistory: string[]) => boolean
useCaseProducts: (selectedUseCase: UseCaseOption | null) => ProductKey[]
recommendedProducts: (
recommendationSource: RecommendationSource,
browsingHistoryProducts: ProductKey[],
useCaseProducts: ProductKey[]
) => ProductKey[]
otherProducts: (recommendedProducts: ProductKey[]) => ProductKey[]
canContinue: (selectedProducts: ProductKey[]) => boolean
recommendationSourceLabel: (recommendationSource: RecommendationSource) => string
}
}
export type productSelectionLogicType = MakeLogicType<
productSelectionLogicValues,
productSelectionLogicActions,
Record<string, any>,
productSelectionLogicMeta
>
export const productSelectionLogic = kea<productSelectionLogicType>([
path(['scenes', 'onboarding', 'productSelection', 'productSelectionLogic']),
connect(() => ({
actions: [teamLogic, ['addProductIntent'], onboardingLogic, ['setOnCompleteOnboardingRedirectUrl']],
values: [teamLogic, ['currentTeam']],
})),
actions({
// Step navigation
setStep: (step: OnboardingStep) => ({ step }),
// Browsing history
setBrowsingHistory: (browsingHistory: string[]) => ({ browsingHistory }),
// Use case selection
selectUseCase: (useCase: UseCaseOption) => ({ useCase }),
clearUseCase: true,
// Product selection
toggleProduct: (productKey: ProductKey) => ({ productKey }),
setSelectedProducts: (productKeys: ProductKey[]) => ({ productKeys }),
setFirstProductOnboarding: (productKey: ProductKey | null) => ({ productKey }),
setRecommendationSource: (source: RecommendationSource) => ({ source }),
// Pick myself path
selectPickMyself: true,
// Simplified single-select (picks one product and starts onboarding immediately)
selectSingleProduct: (productKey: ProductKey) => ({ productKey }),
// Continue to onboarding
handleStartOnboarding: true,
// Show all products toggle
setShowAllProducts: (show: boolean) => ({ show }),
}),
reducers({
currentStep: [
'choose_path' as OnboardingStep,
{
setStep: (_, { step }) => step,
},
],
browsingHistory: [
[] as string[],
{
setBrowsingHistory: (_, { browsingHistory }) => browsingHistory,
},
],
selectedUseCase: [
null as UseCaseOption | null,
{
selectUseCase: (_, { useCase }) => useCase,
clearUseCase: () => null,
},
],
selectedProducts: [
[] as ProductKey[],
{
toggleProduct: (state, { productKey }) =>
state.includes(productKey) ? state.filter((k) => k !== productKey) : [...state, productKey],
setSelectedProducts: (_, { productKeys }) => productKeys,
},
],
firstProductOnboarding: [
null as ProductKey | null,
{
setFirstProductOnboarding: (_, { productKey }) => productKey,
setSelectedProducts: (_, { productKeys }) => productKeys[0] || null,
},
],
recommendationSource: [
'browsing_history' as RecommendationSource,
{
setRecommendationSource: (_, { source }) => source,
},
],
showAllProducts: [
false,
{
setShowAllProducts: (_, { show }) => show,
},
],
}),
selectors({
browsingHistoryProducts: [
(s) => [s.browsingHistory],
(browsingHistory: string[]): ProductKey[] => mapBrowsingHistoryToProducts(browsingHistory),
],
browsingHistoryLabels: [
(s) => [s.browsingHistory],
(browsingHistory: string[]): string[] => getBrowsingHistoryLabels(browsingHistory),
],
hasBrowsingHistory: [
(s) => [s.browsingHistory],
(browsingHistory: string[]): boolean => browsingHistory.length > 0,
],
useCaseProducts: [
(s) => [s.selectedUseCase],
(selectedUseCase: UseCaseOption | null): ProductKey[] => {
if (!selectedUseCase) {
return []
}
return [...getRecommendedProducts(selectedUseCase)]
},
],
recommendedProducts: [
(s) => [s.recommendationSource, s.browsingHistoryProducts, s.useCaseProducts],
(
source: RecommendationSource,
browsingProducts: ProductKey[],
useCaseProducts: ProductKey[]
): ProductKey[] => {
switch (source) {
case 'use_case':
return [...new Set([...useCaseProducts, ...browsingProducts])]
case 'browsing_history':
case 'manual':
default:
return browsingProducts
}
},
],
otherProducts: [
(s) => [s.recommendedProducts],
(recommendedProducts: ProductKey[]): ProductKey[] => {
const allProducts = Object.keys(availableOnboardingProducts) as ProductKey[]
return allProducts.filter((p) => !recommendedProducts.includes(p))
},
],
useCases: [() => [], () => USE_CASE_OPTIONS],
canContinue: [
(s) => [s.selectedProducts],
(selectedProducts: ProductKey[]): boolean => selectedProducts.length > 0,
],
recommendationSourceLabel: [
(s) => [s.recommendationSource],
(source: RecommendationSource): string => {
switch (source) {
case 'use_case':
return 'based on your goal'
case 'browsing_history':
return 'based on your browsing'
case 'manual':
default:
return ''
}
},
],
}),
listeners(({ actions, values }) => ({
selectPickMyself: () => {
const hasBrowsingHistory = values.hasBrowsingHistory
actions.setRecommendationSource(hasBrowsingHistory ? 'browsing_history' : 'manual')
if (hasBrowsingHistory) {
actions.setSelectedProducts(values.browsingHistoryProducts)
}
actions.setShowAllProducts(true)
actions.setStep('product_selection')
// Analytics
reportOnboardingProductSelectionPath(hasBrowsingHistory ? 'browsing_history' : 'manual', {
recommendedProducts: hasBrowsingHistory ? values.browsingHistoryProducts : [],
hasBrowsingHistory,
})
},
selectUseCase: ({ useCase }) => {
actions.setRecommendationSource('use_case')
// Get products from use case + browsing history
const useCaseProducts = [...getRecommendedProducts(useCase)]
const browsingProducts = values.browsingHistoryProducts
const mergedProducts = [...new Set([...useCaseProducts, ...browsingProducts])]
actions.setSelectedProducts(mergedProducts)
actions.setStep('product_selection')
// Analytics
reportOnboardingProductSelectionPath('use_case', {
useCase,
recommendedProducts: mergedProducts,
hasBrowsingHistory: values.hasBrowsingHistory,
})
},
toggleProduct: ({ productKey }) => {
const isNowSelected = values.selectedProducts.includes(productKey)
if (isNowSelected && values.firstProductOnboarding === null) {
actions.setFirstProductOnboarding(productKey)
} else if (!isNowSelected && values.firstProductOnboarding === productKey) {
const remaining = values.selectedProducts.filter((k) => k !== productKey)
actions.setFirstProductOnboarding(remaining[0] || null)
}
posthog.capture('onboarding product toggled', {
product_key: productKey,
selected: isNowSelected,
recommendation_source: values.recommendationSource,
...ONBOARDING_EVENT_PROPS,
})
},
selectSingleProduct: ({ productKey }) => {
actions.setSelectedProducts([productKey])
actions.setFirstProductOnboarding(productKey)
actions.setRecommendationSource('simplified')
actions.handleStartOnboarding()
},
handleStartOnboarding: () => {
const nextUrl = getRelativeNextPath(router.values.searchParams['next'], location)
if (nextUrl && nextUrl !== '/') {
actions.setOnCompleteOnboardingRedirectUrl(nextUrl)
}
if (!values.firstProductOnboarding) {
return
}
const isFromWizard = router.values.searchParams['source'] === 'wizard'
const requiresFurtherSetup = [ProductKey.ERROR_TRACKING, ProductKey.FEATURE_FLAGS, ProductKey.EXPERIMENTS]
const secondStepKey =
values.firstProductOnboarding === ProductKey.WEB_ANALYTICS
? OnboardingStepKey.AUTHORIZED_DOMAINS
: OnboardingStepKey.PRODUCT_CONFIGURATION
const stepKey =
values.firstProductOnboarding === ProductKey.DATA_WAREHOUSE
? OnboardingStepKey.LINK_DATA
: isFromWizard && !requiresFurtherSetup.includes(values.firstProductOnboarding)
? secondStepKey
: OnboardingStepKey.INSTALL
const secondaryProducts = values.selectedProducts.filter(
(k) => k !== values.firstProductOnboarding
) as string[]
router.actions.push(
urls.onboarding({
productKey: values.firstProductOnboarding,
stepKey,
withProducts: secondaryProducts,
})
)
values.selectedProducts.forEach((productKey) => {
actions.addProductIntent({
product_type: productKey,
intent_context:
values.firstProductOnboarding === productKey
? ProductIntentContext.ONBOARDING_PRODUCT_SELECTED_PRIMARY
: ProductIntentContext.ONBOARDING_PRODUCT_SELECTED_SECONDARY,
})
})
// Analytics
window.posthog?.capture('onboarding_products_confirmed', {
recommendation_source: values.recommendationSource,
selected_products: values.selectedProducts,
first_product: values.firstProductOnboarding,
browsing_history: values.browsingHistory,
})
},
})),
afterMount(({ actions }) => {
const browsingHistory = getBrowsingHistoryFromPostHog()
if (browsingHistory.length > 0) {
actions.setBrowsingHistory(browsingHistory)
// Pre-select products based on browsing history
const browsingProducts = mapBrowsingHistoryToProducts(browsingHistory)
if (browsingProducts.length > 0) {
actions.setSelectedProducts(browsingProducts)
}
}
reportOnboardingStarted()
}),
])