: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, events, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { teamLogic } from 'scenes/teamLogic'
import { ExperimentHoldoutType } from '~/types'
export const NEW_HOLDOUT: ExperimentHoldoutType = {
id: null,
name: '',
description: null,
filters: [
{
properties: [],
rollout_percentage: 10,
variant: 'holdout',
},
],
created_by: null,
created_at: null,
updated_at: null,
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface holdoutsLogicValues {
currentProjectId: number | string // teamLogic
holdout: ExperimentHoldoutType
holdouts: ExperimentHoldoutType[]
holdoutsLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface holdoutsLogicActions {
reportExperimentHoldoutCreated: (holdout: ExperimentHoldoutType) => {
holdout: ExperimentHoldoutType
} // eventUsageLogic
createHoldout: () => {
value: true
}
createHoldoutFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
createHoldoutSuccess: (
holdouts: ExperimentHoldoutType[],
payload?: {
value: true
}
) => {
holdouts: ExperimentHoldoutType[]
payload?: {
value: true
}
}
deleteHoldout: (id: number | null) => {
id: number | null
}
deleteHoldoutFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
deleteHoldoutSuccess: (
holdouts: ExperimentHoldoutType[],
payload?: {
id: number | null
}
) => {
holdouts: ExperimentHoldoutType[]
payload?: {
id: number | null
}
}
loadHoldouts: () => any
loadHoldoutsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadHoldoutsSuccess: (
holdouts: ExperimentHoldoutType[],
payload?: any
) => {
holdouts: ExperimentHoldoutType[]
payload?: any
}
setHoldout: (holdout: Partial<ExperimentHoldoutType>) => {
holdout: Partial<ExperimentHoldoutType>
}
updateHoldout: (
id: number | null,
holdout: Partial<ExperimentHoldoutType>
) => {
holdout: Partial<ExperimentHoldoutType>
id: number | null
}
updateHoldoutFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updateHoldoutSuccess: (
holdouts: ExperimentHoldoutType[],
payload?: {
holdout: Partial<ExperimentHoldoutType>
id: number | null
}
) => {
holdouts: ExperimentHoldoutType[]
payload?: {
holdout: Partial<ExperimentHoldoutType>
id: number | null
}
}
}
export type holdoutsLogicType = MakeLogicType<holdoutsLogicValues, holdoutsLogicActions>
export const holdoutsLogic = kea<holdoutsLogicType>([
path(['scenes', 'experiments', 'holdoutsLogic']),
actions({
setHoldout: (holdout: Partial<ExperimentHoldoutType>) => ({ holdout }),
createHoldout: true,
updateHoldout: (id: number | null, holdout: Partial<ExperimentHoldoutType>) => ({ id, holdout }),
deleteHoldout: (id: number | null) => ({ id }),
}),
connect(() => ({
actions: [eventUsageLogic, ['reportExperimentHoldoutCreated']],
values: [teamLogic, ['currentProjectId']],
})),
reducers({
holdout: [
NEW_HOLDOUT,
{
setHoldout: (state, { holdout }) => ({ ...state, ...holdout }),
},
],
}),
loaders(({ actions, values }) => ({
holdouts: [
[] as ExperimentHoldoutType[],
{
loadHoldouts: async () => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentHoldoutsList() from 'products/experiments/frontend/generated/api' instead.
const response = await api.get(`api/projects/${values.currentProjectId}/experiment_holdouts/`)
return response.results as ExperimentHoldoutType[]
},
createHoldout: async () => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentHoldoutsCreate() from 'products/experiments/frontend/generated/api' instead.
const response = await api.create(
`api/projects/${values.currentProjectId}/experiment_holdouts/`,
values.holdout
)
actions.reportExperimentHoldoutCreated(response)
return [...values.holdouts, response] as ExperimentHoldoutType[]
},
updateHoldout: async ({ id, holdout }) => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentHoldoutsPartialUpdate() from 'products/experiments/frontend/generated/api' instead.
const response = await api.update(
`api/projects/${values.currentProjectId}/experiment_holdouts/${id}/`,
holdout
)
return values.holdouts.map((h) => (h.id === id ? response : h)) as ExperimentHoldoutType[]
},
deleteHoldout: async ({ id }) => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. experimentHoldoutsDestroy() from 'products/experiments/frontend/generated/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
await api.delete(`api/projects/${values.currentProjectId}/experiment_holdouts/${id}/`)
return values.holdouts.filter((h) => h.id !== id)
},
},
],
})),
listeners(({ actions }) => ({
createHoldoutSuccess: () => {
lemonToast.success('Holdout created')
actions.loadHoldouts()
},
updateHoldoutSuccess: () => {
lemonToast.success('Holdout updated')
actions.loadHoldouts()
},
deleteHoldoutSuccess: () => {
lemonToast.success('Holdout deleted')
actions.loadHoldouts()
},
})),
events(({ actions }) => ({
afterMount: () => {
actions.loadHoldouts()
},
})),
])