: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 { 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 { DEFAULT_MDE } from 'products/experiments/frontend/constants'
export interface ExperimentsConfig {
experiment_recalculation_times: string[] | null
default_experiment_confidence_level: number | null
default_experiment_stats_method: string | null
default_only_count_matured_users: boolean
default_cuped_enabled: boolean
default_cuped_lookback_days: number | null
default_minimum_detectable_effect: number
default_sequential_testing_enabled: boolean
default_sequential_tuning_parameter: number | null
flag_cleanup_repository: string | null
}
// Repository names identify the customer's codebase, so only report whether one is set.
const redactSettingValue = (setting: string, value: unknown): unknown =>
setting === 'flag_cleanup_repository' ? value !== null : value
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentsConfigLogicValues {
currentTeamId: number | null // teamLogic
defaultMinimumDetectableEffect: number
experimentsConfig: ExperimentsConfig | null
experimentsConfigLoading: boolean
experimentsConfigUpdating: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentsConfigLogicActions {
reportExperimentsSettingChanged: (
setting: string,
value: unknown
) => {
setting: string
value: unknown
} // eventUsageLogic
loadExperimentsConfig: () => any
loadExperimentsConfigFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadExperimentsConfigSuccess: (
experimentsConfig: ExperimentsConfig,
payload?: any
) => {
experimentsConfig: ExperimentsConfig
payload?: any
}
updateExperimentsConfig: (payload: Partial<ExperimentsConfig>) => {
payload: Partial<ExperimentsConfig>
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentsConfigLogicMeta {
__keaTypeGenInternalSelectorTypes: {
defaultMinimumDetectableEffect: (experimentsConfig: ExperimentsConfig | null) => number
}
}
export type experimentsConfigLogicType = MakeLogicType<
experimentsConfigLogicValues,
experimentsConfigLogicActions,
Record<string, any>,
experimentsConfigLogicMeta
>
export const experimentsConfigLogic = kea<experimentsConfigLogicType>([
path(['scenes', 'settings', 'environment', 'experimentsConfigLogic']),
connect(() => ({
values: [teamLogic, ['currentTeamId']],
actions: [eventUsageLogic, ['reportExperimentsSettingChanged']],
})),
actions({
updateExperimentsConfig: (payload: Partial<ExperimentsConfig>) => ({ payload }),
}),
loaders(({ values }) => ({
experimentsConfig: [
null as ExperimentsConfig | null,
{
loadExperimentsConfig: async (): Promise<ExperimentsConfig> => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. organizationsProjectsExperimentsConfigRetrieve() from '~/generated/core/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
return await api.get(`api/projects/${values.currentTeamId}/experiments_config/`)
},
},
],
})),
reducers({
experimentsConfig: {
updateExperimentsConfig: (state, { payload }) => (state ? { ...state, ...payload } : state),
},
experimentsConfigUpdating: [
false,
{
updateExperimentsConfig: () => true,
loadExperimentsConfigSuccess: () => false,
loadExperimentsConfigFailure: () => false,
},
],
}),
selectors({
defaultMinimumDetectableEffect: [
(s) => [s.experimentsConfig],
(config: ExperimentsConfig | null): number => config?.default_minimum_detectable_effect ?? DEFAULT_MDE,
],
}),
listeners(({ actions, values }) => ({
updateExperimentsConfig: async ({ payload }) => {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. organizationsProjectsExperimentsConfigPartialUpdate() from '~/generated/core/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
await api.update(`api/projects/${values.currentTeamId}/experiments_config/`, payload)
for (const [setting, value] of Object.entries(payload)) {
actions.reportExperimentsSettingChanged(setting, redactSettingValue(setting, value))
}
} catch (error: any) {
lemonToast.error(error.data?.detail || 'Failed to update experiment settings. Please try again.')
} finally {
actions.loadExperimentsConfig()
}
},
})),
afterMount(({ actions }) => {
actions.loadExperimentsConfig()
}),
])