: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, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'
import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import type { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { teamLogic } from 'scenes/teamLogic'
import { ProductKey } from '~/queries/schema/schema-general'
import { experimentsList } from 'products/experiments/frontend/generated/api'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentsSetupLogicValues {
setupStatus: ProductSetupStatus // productSetupStatusLogic
currentProjectId: number | string // teamLogic
experimentCount: number | null
experimentCountLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentsSetupLogicActions {
setDetectedStatus: (status: ProductSetupStatus) => {
status: ProductSetupStatus
} // productSetupStatusLogic
loadExperimentCount: (_: void) => void
loadExperimentCountFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadExperimentCountSuccess: (
experimentCount: number,
payload?: void
) => {
experimentCount: number
payload?: void
}
}
export type experimentsSetupLogicType = MakeLogicType<experimentsSetupLogicValues, experimentsSetupLogicActions>
/**
* Setup detection for the experiments empty state. Experiments are a creation-first
* product, so "set up" simply means the project has at least one experiment - no
* event or traffic signal involved. Re-checks on every mount (the scene gate mounts
* it), which covers returning from the experiment creation page.
*/
export const experimentsSetupLogic = kea<experimentsSetupLogicType>([
path(['products', 'experiments', 'frontend', 'emptyState', 'experimentsSetupLogic']),
connect(() => ({
actions: [productSetupStatusLogic({ productKey: ProductKey.EXPERIMENTS }), ['setDetectedStatus']],
values: [
productSetupStatusLogic({ productKey: ProductKey.EXPERIMENTS }),
['status as setupStatus'],
teamLogic,
['currentProjectId'],
],
})),
loaders(({ values }) => ({
experimentCount: [
null as number | null,
{
loadExperimentCount: async (_: void, breakpoint): Promise<number> => {
// The list excludes archived experiments unless asked for them, and no value of
// `archived` returns both, so a project that archived all of its experiments
// would count zero and lose the scene it unarchives them from.
const [live, archived] = await Promise.all([
experimentsList(String(values.currentProjectId), { limit: 1 }),
experimentsList(String(values.currentProjectId), { limit: 1, archived: true }),
])
breakpoint()
return live.count + archived.count
},
},
],
})),
listeners(({ actions, values }) => ({
loadExperimentCountSuccess: ({ experimentCount }) => {
actions.setDetectedStatus(experimentCount !== null && experimentCount > 0 ? 'has-data' : 'needs-setup')
},
loadExperimentCountFailure: () => {
// Never strand the gate on its spinner: if nothing has answered yet, fail
// open to the real scene. Don't downgrade an existing answer on a blip.
if (values.setupStatus === 'loading') {
actions.setDetectedStatus('unknown')
}
},
})),
afterMount(({ actions }) => {
actions.loadExperimentCount()
}),
])