: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, key, listeners, path, props, reducers, selectors } from 'kea'
import { combineUrl, router } from 'kea-router'
import posthog from 'posthog-js'
import { ApiError } from 'lib/api-error'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { featureFlagsLogic } from 'scenes/feature-flags/featureFlagsLogic'
import { projectLogic } from 'scenes/projectLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { refreshTreeItem } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { isExperimentMetric } from '~/queries/schema-guards'
import {
ExperimentExposureCriteria,
ExperimentMetric,
ProductIntentContext,
ProductKey,
} from '~/queries/schema/schema-general'
import type { Experiment, FeatureFlagFilters, MultivariateFlagVariant } from '~/types'
import { NEW_EXPERIMENT } from 'products/experiments/frontend/constants'
import { experimentsCreate } from 'products/experiments/frontend/generated/api'
import type {
ExperimentApi,
ExperimentFeatureFlagFiltersApi,
ExperimentTypeEnumApi,
} from 'products/experiments/frontend/generated/api.schemas'
import {
alignMetricsWithExposure,
getWarehouseNativeExposure,
} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { visionScannersCreate } from 'products/replay_vision/frontend/generated/api'
import { experimentScannerParams } from 'products/replay_vision/frontend/replay_scanners/experimentTargeting'
import type { ProductCrossSellProperties, ProductIntentProperties } from '../../../lib/utils/product-intents'
import type { ExperimentMetricUnion } from '../../../queries/schema/schema-general'
import type { FeatureFlagType } from '../../../types'
import { FORM_MODES, experimentLogic } from '../experimentLogic'
import { experimentSceneLogic } from '../experimentSceneLogic'
import { experimentScannerBody, flagAggregatesByGroup } from '../replayVisionScanner'
import {
type ExperimentWritePayload,
getExperimentVariants,
toExperimentWritePayload,
toFlagVariantsInput,
} from '../utils'
import { validateExperimentSubmission } from './experimentSubmissionValidation'
import type { FeatureFlagKeyValidation } from './variantsPanelLogic'
import { variantsPanelLogic } from './variantsPanelLogic'
import { validateVariants } from './variantsPanelValidation'
function experimentFromApi(experiment: ExperimentApi): Experiment {
return experiment as unknown as Experiment
}
function experimentTypeForApi(type: Experiment['type']): ExperimentTypeEnumApi | undefined {
return type === 'web' || type === 'product' ? type : undefined
}
function experimentWritePayloadForApi(
experiment: ExperimentWritePayload<Experiment>
): Parameters<typeof experimentsCreate>[1] {
return {
...experiment,
type: experimentTypeForApi(experiment.type),
} as unknown as Parameters<typeof experimentsCreate>[1]
}
export interface CreateExperimentLogicProps {}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface createExperimentLogicValues {
currentProjectId: number | null // projectLogic
featureFlagKeyValidation: FeatureFlagKeyValidation | null // variantsPanelLogic
featureFlagKeyValidationLoading: boolean // variantsPanelLogic
canSubmitExperiment: boolean
createReplayVisionScanner: boolean
experiment: Experiment & {
feature_flag_filters?: FeatureFlagFilters
}
experimentErrors: Record<string, string>
experimentValidationErrors: string | undefined
isExperimentSubmitting: boolean
mode: 'create' | 'link'
sharedMetrics: {
primary: ExperimentMetric[]
secondary: ExperimentMetric[]
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface createExperimentLogicActions {
updateFlag: (flag: FeatureFlagType) => {
flag: FeatureFlagType
} // featureFlagsLogic
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
addProductIntentForCrossSell: (properties: ProductCrossSellProperties) => ProductCrossSellProperties // teamLogic
createExperimentSuccess: () => {
value: true
}
resetExperiment: () => {
value: true
}
saveExperiment: () => {
value: true
}
saveExperimentFailure: () => {
value: true
}
saveExperimentStarted: () => {
value: true
}
saveExperimentSuccess: () => {
value: true
}
setCreateReplayVisionScanner: (enabled: boolean) => {
enabled: boolean
}
setExperiment: (experiment: Experiment) => {
experiment: Experiment
}
setExperimentErrors: (errors: Record<string, string>) => {
errors: Record<string, string>
}
setExperimentValue: (
name: string,
value: any
) => {
name: string
value: any
}
setExposureCriteria: (criteria: ExperimentExposureCriteria) => {
criteria: ExperimentExposureCriteria
}
setFeatureFlagConfig: (config: {
ensure_experience_continuity?: boolean
feature_flag_key?: string
rollout_percentage?: number
variants?: MultivariateFlagVariant[]
}) => {
config: {
ensure_experience_continuity?: boolean | undefined
feature_flag_key?: string | undefined
rollout_percentage?: number | undefined
variants?: MultivariateFlagVariant[] | undefined
}
}
setSharedMetrics: (sharedMetrics: { primary: ExperimentMetric[]; secondary: ExperimentMetric[] }) => {
sharedMetrics: {
primary: ExperimentMetricUnion[]
secondary: ExperimentMetricUnion[]
}
}
validateField: (field: 'name') => {
field: 'name'
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface createExperimentLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
canSubmitExperiment: (
experiment: Experiment & {
feature_flag_filters?: FeatureFlagFilters
},
featureFlagKeyValidation: FeatureFlagKeyValidation | null,
mode: 'create' | 'link',
experimentErrors: Record<string, string>
) => boolean
experimentValidationErrors: (
experiment: Experiment & {
feature_flag_filters?: FeatureFlagFilters
},
featureFlagKeyValidation: FeatureFlagKeyValidation | null,
mode: 'create' | 'link',
experimentErrors: Record<string, string>
) => string | undefined
mode: (
experiment: Experiment & {
feature_flag_filters?: FeatureFlagFilters
}
) => 'create' | 'link'
}
}
export type createExperimentLogicType = MakeLogicType<
createExperimentLogicValues,
createExperimentLogicActions,
CreateExperimentLogicProps,
createExperimentLogicMeta
>
export const createExperimentLogic = kea<createExperimentLogicType>([
props({} as CreateExperimentLogicProps),
key(() => 'create-experiment'),
path((key) => ['scenes', 'experiments', 'create', 'createExperimentLogic', key]),
connect(() => ({
values: [
variantsPanelLogic({ experiment: { ...NEW_EXPERIMENT }, disabled: false }),
['featureFlagKeyValidation', 'featureFlagKeyValidationLoading'],
projectLogic,
['currentProjectId'],
],
actions: [featureFlagsLogic, ['updateFlag'], teamLogic, ['addProductIntent', 'addProductIntentForCrossSell']],
})),
actions(() => ({
setExperiment: (experiment: Experiment) => ({ experiment }),
setExperimentValue: (name: string, value: any) => ({ name, value }),
resetExperiment: true,
setExposureCriteria: (criteria: ExperimentExposureCriteria) => ({ criteria }),
setCreateReplayVisionScanner: (enabled: boolean) => ({ enabled }),
setFeatureFlagConfig: (config: {
feature_flag_key?: string
variants?: MultivariateFlagVariant[]
rollout_percentage?: number
ensure_experience_continuity?: boolean
}) => ({ config }),
saveExperiment: true,
saveExperimentStarted: true,
saveExperimentSuccess: true,
saveExperimentFailure: true,
createExperimentSuccess: true,
setExperimentErrors: (errors: Record<string, string>) => ({ errors }),
validateField: (field: 'name') => ({ field }),
setSharedMetrics: (sharedMetrics: { primary: ExperimentMetric[]; secondary: ExperimentMetric[] }) => ({
sharedMetrics,
}),
})),
reducers(() => ({
experiment: [
{ ...NEW_EXPERIMENT } as Experiment & { feature_flag_filters?: FeatureFlagFilters },
{
setExperiment: (_, { experiment }) => experiment,
setExperimentValue: (state, { name, value }) => ({ ...state, [name]: value }),
setExposureCriteria: (state, { criteria }) => {
const exposureCriteria = { ...state.exposure_criteria, ...criteria }
const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)
return {
...state,
exposure_criteria: exposureCriteria,
// The API rejects a metric on another connection or unit than the exposure
...(warehouseExposure && {
metrics: alignMetricsWithExposure(state.metrics ?? [], warehouseExposure),
metrics_secondary: alignMetricsWithExposure(
state.metrics_secondary ?? [],
warehouseExposure
),
}),
}
},
setFeatureFlagConfig: (state, { config }) => {
const currentConfig = state.feature_flag_config ?? {}
const filters: ExperimentFeatureFlagFiltersApi = { ...currentConfig.filters }
if (config.variants !== undefined) {
filters.multivariate = { variants: toFlagVariantsInput(config.variants) }
}
if (config.rollout_percentage !== undefined) {
filters.groups = [{ properties: [], rollout_percentage: config.rollout_percentage }]
}
return {
...state,
...(config.feature_flag_key !== undefined && {
feature_flag_key: config.feature_flag_key,
}),
feature_flag_config: {
...currentConfig,
filters,
...(config.ensure_experience_continuity !== undefined && {
ensure_experience_continuity: config.ensure_experience_continuity,
}),
},
}
},
updateFeatureFlagKey: (state, { key }) => ({ ...state, feature_flag_key: key }),
resetExperiment: () => ({ ...NEW_EXPERIMENT }),
},
],
createReplayVisionScanner: [
false,
{
setCreateReplayVisionScanner: (_, { enabled }) => enabled,
resetExperiment: () => false,
saveExperimentSuccess: () => false,
},
],
sharedMetrics: [
{ primary: [], secondary: [] } as { primary: ExperimentMetric[]; secondary: ExperimentMetric[] },
{
setSharedMetrics: (_, { sharedMetrics }) => sharedMetrics,
},
],
isExperimentSubmitting: [
false,
{
saveExperimentStarted: () => true,
saveExperimentSuccess: () => false,
saveExperimentFailure: () => false,
},
],
experimentErrors: [
{} as Record<string, string>,
{
setExperimentErrors: (_, { errors }) => errors,
saveExperimentSuccess: () => ({}),
createExperimentSuccess: () => ({}),
resetExperiment: () => ({}),
},
],
})),
selectors(() => ({
canSubmitExperiment: [
(s) => [s.experiment, s.featureFlagKeyValidation, s.mode, s.experimentErrors],
(
experiment: Experiment,
featureFlagKeyValidation: FeatureFlagKeyValidation | null,
mode: 'create' | 'link',
experimentErrors: Record<string, string>
) => {
const validation = validateExperimentSubmission({
experiment,
featureFlagKeyValidation,
mode,
experimentErrors,
})
return validation.isValid
},
],
experimentValidationErrors: [
(s) => [s.experiment, s.featureFlagKeyValidation, s.mode, s.experimentErrors],
(
experiment: Experiment,
featureFlagKeyValidation: FeatureFlagKeyValidation | null,
mode: 'create' | 'link',
experimentErrors: Record<string, string>
): string | undefined => {
const validation = validateExperimentSubmission({
experiment,
featureFlagKeyValidation,
mode,
experimentErrors,
})
return validation.errors.length > 0 ? validation.errors.join(', ') : undefined
},
],
mode: [
(s) => [s.experiment],
(): 'create' | 'link' => {
return variantsPanelLogic({ experiment: { ...NEW_EXPERIMENT }, disabled: false }).values.mode
},
],
})),
events(({ actions, values }) => ({
afterMount: () => {
if (values.experiment.id !== 'new') {
return
}
try {
const { searchParams } = router.values.currentLocation
const { metric, name } = searchParams
const parsedMetric = typeof metric === 'string' ? JSON.parse(metric) : metric
if (name && isExperimentMetric(parsedMetric)) {
actions.setExperiment({
...NEW_EXPERIMENT,
metrics: parsedMetric ? [parsedMetric] : [],
name: name ?? '',
})
lemonToast.success('Metric added successfully!')
return
}
} catch (error) {
console.error('Error parsing metric from URL', error)
lemonToast.error('Error parsing metric from URL')
}
},
})),
listeners(({ values, actions }) => ({
setExperiment: () => {},
setExperimentValue: () => {},
validateField: ({ field }) => {
if (field === 'name') {
const name = values.experiment.name
if (!name || name.trim().length === 0) {
actions.setExperimentErrors({ name: 'Name is required' })
} else {
actions.setExperimentErrors({})
}
}
},
saveExperiment: async () => {
// Prevent double submission
if (values.isExperimentSubmitting) {
return
}
// Check if async validation is still loading
if (values.featureFlagKeyValidationLoading) {
lemonToast.error('Please wait for validation to complete')
actions.saveExperimentFailure()
return
}
// Clear previous errors before validation is triggered
actions.setExperimentErrors({})
// Validate using canSubmitExperiment
if (!values.canSubmitExperiment) {
// Set field errors
const errors: Record<string, string> = {}
if (!values.experiment.name?.trim()) {
errors.name = 'Name is required'
}
actions.setExperimentErrors(errors)
// Show toast with what's wrong
const validation = validateVariants({
flagKey: values.experiment.feature_flag_key,
variants: getExperimentVariants(values.experiment),
featureFlagKeyValidation: values.featureFlagKeyValidation,
mode: values.mode,
})
if (validation.hasErrors) {
lemonToast.error('Please fix variants configuration')
} else {
lemonToast.error('Experiment is not valid')
}
actions.saveExperimentFailure()
return
}
// Set loading state after all validation passes
actions.saveExperimentStarted()
// Whether the key is available decides whether flag config is sent (an available key
// means the backend creates the flag and applies the config; an existing key means the
// experiment links to that flag as-is and explicit config is rejected). A missing or
// stale availability check must not decide this silently: re-run it, and fail loud if
// it still can't complete.
const flagKey = values.experiment.feature_flag_key
const panelLogic = variantsPanelLogic({ experiment: { ...NEW_EXPERIMENT }, disabled: false })
if (values.featureFlagKeyValidation?.key !== flagKey) {
try {
await panelLogic.asyncActions.validateFeatureFlagKey(flagKey)
} catch {
// Handled below: featureFlagKeyValidation stays stale or null.
}
}
const keyValidation = panelLogic.values.featureFlagKeyValidation
if (keyValidation?.key !== flagKey) {
lemonToast.error("Couldn't verify the feature flag key. Please try again.")
actions.saveExperimentFailure()
return
}
try {
const savedMetrics = [
...values.sharedMetrics.primary.map((metric) => ({
id: metric.sharedMetricId!,
metadata: {
type: 'primary' as const,
},
})),
...values.sharedMetrics.secondary.map((metric) => ({
id: metric.sharedMetricId!,
metadata: {
type: 'secondary' as const,
},
})),
]
// A key confirmed available means the backend will create the flag and can apply
// config to it; a taken key means the experiment links to the pre-existing flag
// as-is, and the API rejects explicit flag config.
const experimentPayload = {
...toExperimentWritePayload(values.experiment, {
omitFlagConfig: keyValidation.valid !== true,
}),
saved_metrics_ids: savedMetrics,
}
const response = experimentFromApi(
await experimentsCreate(
String(values.currentProjectId),
experimentWritePayloadForApi(experimentPayload)
)
)
if (response.id) {
// Refresh tree navigation
refreshTreeItem('experiment', String(response.id))
if (response.feature_flag?.id) {
refreshTreeItem('feature_flag', String(response.feature_flag.id))
}
// Update our own state with the server response
// This ensures we have the full experiment data including feature_flag, etc.
actions.setExperiment(response)
actions.addProductIntent({
product_type: ProductKey.EXPERIMENTS,
intent_context: ProductIntentContext.EXPERIMENT_CREATED,
})
actions.createExperimentSuccess()
globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.CreateExperiment)
let replayScannerId: string | null = null
let replayScannerCreationFailed = false
// The wizard shows the option off for a group flag, but the option can still be on
// when the flag was linked after it was ticked.
if (values.createReplayVisionScanner && !flagAggregatesByGroup(response.feature_flag)) {
try {
const replayScanner = await visionScannersCreate(
String(values.currentProjectId),
experimentScannerBody(response)
)
replayScannerId = replayScanner.id
actions.addProductIntentForCrossSell({
from: ProductKey.EXPERIMENTS,
to: ProductKey.REPLAY_VISION,
intent_context: ProductIntentContext.EXPERIMENT_REPLAY_VISION_SCANNER_CREATED,
})
} catch (scannerError) {
// A missing org AI consent is a user-correctable config state, not a defect, so it
// stays out of error tracking. The checkbox gates on consent, but a stale client can
// still reach here. Other failures (quota, access) are still captured. Without that,
// a systematic break is invisible, because the experiment itself still succeeds.
if (
!(
scannerError instanceof ApiError &&
scannerError.code === 'ai_data_processing_not_approved'
)
) {
posthog.captureException(scannerError)
}
replayScannerCreationFailed = true
}
}
if (replayScannerCreationFailed) {
lemonToast.error("Experiment created, but the experiment scanner wasn't.", {
button: {
label: 'Set up experiment scanner',
action: () =>
router.actions.push(
combineUrl(urls.replayVisionScannerDetails('new'), {
...experimentScannerParams({
experimentId: response.id as number,
variantKey: null,
}),
entry_point: 'experiment_create_wizard_retry',
}).url
),
},
})
} else if (replayScannerId) {
lemonToast.success(
'Experiment created. The experiment scanner turns on when you launch the experiment.',
{
button: {
label: 'View scanner',
action: () => router.actions.push(urls.replayVision(replayScannerId)),
},
}
)
} else {
lemonToast.success('Experiment created successfully!')
}
tryShowMCPHint('experiments.create', {
derivedPrompt: response.name ? `Create an A/B experiment called ${response.name}` : undefined,
})
// Don't reset - we just set the fresh data above
actions.saveExperimentSuccess()
const sceneLogicInstance = experimentSceneLogic.findMounted()
if (sceneLogicInstance) {
sceneLogicInstance.actions.setSceneState(response.id, FORM_MODES.update)
const logicRef = sceneLogicInstance.values.experimentLogicRef
if (logicRef) {
logicRef.logic.actions.loadExperimentSuccess(response)
} else {
experimentLogic({
experimentId: response.id,
}).actions.loadExperimentSuccess(response)
}
} else {
const viewLogic = experimentLogic({ experimentId: response.id })
viewLogic.actions.loadExperimentSuccess(response)
router.actions.push(urls.experiment(response.id))
}
}
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to save experiment')
actions.saveExperimentFailure()
}
},
})),
])