: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
feat(experiments): make metric reloading an explicit action. (#57714)
Rodrigo Ilorof9a18ac97084
diff --git a/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx b/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsxindex 2007d7720c7a..f800bd0eee01 100644--- a/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx+++ b/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx@@ -145,6 +145,7 @@ export function ExperimentView({ tabId }: Pick<ExperimentSceneLogicProps, 'tabIdupdateExperimentMetrics,addSharedMetricsToExperiment,removeSharedMetricFromExperiment,+ removeMetric,} = useActions(experimentLogic)if (!tabId) {@@ -265,11 +266,7 @@ export function ExperimentView({ tabId }: Pick<ExperimentSceneLogicProps, 'tabIdreturn}- setExperiment({- [context.field]: experiment[context.field].filter((m) => m.uuid !== metric.uuid),- })-- updateExperimentMetrics()+ removeMetric(metric.uuid, context.type)closeExperimentMetricModal()}}/>diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsxindex 5541d22f5910..2748914ee32a 100644--- a/frontend/src/scenes/experiments/experimentLogic.tsx+++ b/frontend/src/scenes/experiments/experimentLogic.tsx@@ -849,6 +849,8 @@ export const experimentLogic = kea<experimentLogicType>([isSecondary,newUuid,}),+ // Semantic metric actions - each controls its own reload behavior+ removeMetric: (uuid: string, context: 'primary' | 'secondary') => ({ uuid, context }),updateMetricBreakdown: (uuid: string, breakdown: Breakdown) => ({ uuid, breakdown }),removeMetricBreakdown: (uuid: string, index: number, breakdown: Breakdown) => ({ uuid, index, breakdown }),// METRICS RESULTS@@ -1595,12 +1597,14 @@ export const experimentLogic = kea<experimentLogicType>([}},changeExperimentStartDate: async ({ startDate }) => {- actions.updateExperiment({ start_date: startDate, update_feature_flag_params: false })+ await asyncActions.updateExperiment({ start_date: startDate, update_feature_flag_params: false })values.experiment && eventUsageLogic.actions.reportExperimentStartDateChange(values.experiment, startDate)+ actions.refreshExperimentResults(true, 'config_change')},changeExperimentEndDate: async ({ endDate }) => {- actions.updateExperiment({ end_date: endDate, update_feature_flag_params: false })+ await asyncActions.updateExperiment({ end_date: endDate, update_feature_flag_params: false })values.experiment && eventUsageLogic.actions.reportExperimentEndDateChange(values.experiment, endDate)+ actions.refreshExperimentResults(true, 'config_change')},endExperiment: async () => {try {@@ -1789,11 +1793,13 @@ export const experimentLogic = kea<experimentLogicType>([}},updateExperimentMetrics: async () => {- actions.updateExperiment({+ await asyncActions.updateExperiment({metrics: values.experiment.metrics,metrics_secondary: values.experiment.metrics_secondary,update_feature_flag_params: false,})+ // Reload results for added/edited metrics+ actions.refreshExperimentResults(true, 'config_change')},updateExperimentCollectionGoal: async () => {const { recommendedRunningTime, recommendedSampleSize, minimumDetectableEffect } = values@@ -1830,22 +1836,18 @@ export const experimentLogic = kea<experimentLogicType>([lemonToast.error(error.detail || 'Failed to reset experiment')}},- updateExperimentSuccess: async ({ experiment, payload }) => {- actions.updateExperiments(experiment)- if (payload?.update_feature_flag_params && experiment.feature_flag) {- actions.updateFlagFromPartial(experiment.feature_flag)- }- if (isLaunched(experiment)) {- // For launched experiments, refresh results if any of these fields are updated- const forceRefresh =- payload?.start_date !== undefined ||- payload?.end_date !== undefined ||- payload?.metrics !== undefined ||- payload?.metrics_secondary !== undefined ||- payload?.stats_config !== undefined ||- payload?.only_count_matured_users !== undefined- actions.refreshExperimentResults(forceRefresh, 'config_change')+ updateExperimentSuccess: async ({ experimentUpdate, payload }) => {+ if (experimentUpdate) {+ actions.updateExperiments(experimentUpdate)+ if (payload?.update_feature_flag_params && experimentUpdate.feature_flag) {+ actions.updateFlagFromPartial(experimentUpdate.feature_flag)+ }}+ // NOTE: No implicit metric reload here. Each action that calls updateExperiment+ // is responsible for triggering its own reload if needed. This prevents:+ // 1. Unnecessary reloads (e.g., removing a metric doesn't need to re-query)+ // 2. Double reloads (callers that explicitly reload were also getting implicit reload)+ // See the semantic actions (removeMetric, etc.) for explicit reload patterns.},createExposureCohortSuccess: ({ exposureCohort }) => {if (exposureCohort && exposureCohort.id !== 'new') {@@ -2310,6 +2312,18 @@ export const experimentLogic = kea<experimentLogicType>([}}},+ removeMetric: ({ uuid, context }) => {+ const isPrimary = context === 'primary'+ const field = isPrimary ? 'metrics' : 'metrics_secondary'+ const currentMetrics: (ExperimentMetric | ExperimentTrendsQuery | ExperimentFunnelsQuery)[] =+ values.experiment[field] || []+ const filtered = currentMetrics.filter((m) => m.uuid !== uuid)++ actions.updateExperiment({+ [field]: filtered,+ update_feature_flag_params: false,+ })+ },updateMetricBreakdown: async ({ uuid, breakdown }) => {const isPrimary = values.experiment.metrics.some((m) => m.uuid === uuid)@@ -2457,17 +2471,25 @@ export const experimentLogic = kea<experimentLogicType>([}return NEW_EXPERIMENT},- updateExperiment: async (update: Partial<Experiment> & { update_feature_flag_params?: boolean }) => {- const response: Experiment = await api.update(- `api/projects/${values.currentProjectId}/experiments/${values.experimentId}`,- update- )- const responseWithMetricsOrdering = initializeMetricOrdering(response)- refreshTreeItem('experiment', String(values.experimentId))- actions.setUnmodifiedExperiment(structuredClone(responseWithMetricsOrdering))- return responseWithMetricsOrdering- },},+ // Separate loader for updates to avoid triggering experimentLoading+ experimentUpdate: [+ null as Experiment | null,+ {+ updateExperiment: async (update: Partial<Experiment> & { update_feature_flag_params?: boolean }) => {+ const response: Experiment = await api.update(+ `api/projects/${values.currentProjectId}/experiments/${values.experimentId}`,+ update+ )+ const responseWithMetricsOrdering = initializeMetricOrdering(response)+ refreshTreeItem('experiment', String(values.experimentId))+ actions.setUnmodifiedExperiment(structuredClone(responseWithMetricsOrdering))+ // Also update the main experiment state+ actions.setExperiment(responseWithMetricsOrdering)+ return responseWithMetricsOrdering+ },+ },+ ],exposureCohort: [null as CohortType | null,{