Skip to content

: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.

fix(experiments): Surface shared metric save errors and block double submits (#101842)

Juraj Majerik85925e8118ea
diff --git a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
index a093b2baf814..e47fbf5a5688 100644
--- a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
+++ b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
@@ -90,7 +90,7 @@ function openSaveWithRunningExperimentsDialog(
}
export function SharedMetric(): JSX.Element {
- const { sharedMetric, action } = useValues(sharedMetricLogic)
+ const { sharedMetric, action, metricSaving } = useValues(sharedMetricLogic)
const sceneMenuBarEnabled = useFeatureFlag('SCENE_MENU_BAR')
const { setSharedMetric, createSharedMetric, updateSharedMetric, deleteSharedMetric } =
useActions(sharedMetricLogic)
@@ -118,6 +118,9 @@ export function SharedMetric(): JSX.Element {
}
const handleSave = (): void => {
+ if (metricSaving) {
+ return
+ }
if (['create', 'duplicate'].includes(action)) {
createSharedMetric()
return
@@ -305,6 +308,7 @@ export function SharedMetric(): JSX.Element {
>
<LemonButton
disabledReason={sharedMetric.name ? undefined : 'You must give your metric a name'}
+ loading={metricSaving}
size="small"
type="primary"
onClick={handleSave}
@@ -341,6 +345,7 @@ export function SharedMetric(): JSX.Element {
>
<LemonButton
disabledReason={sharedMetric.name ? undefined : 'You must give your metric a name'}
+ loading={metricSaving}
size="medium"
type="primary"
onClick={handleSave}
diff --git a/frontend/src/scenes/experiments/SharedMetrics/sharedMetricLogic.tsx b/frontend/src/scenes/experiments/SharedMetrics/sharedMetricLogic.tsx
index a4d825da3439..6e5ea09b8397 100644
--- a/frontend/src/scenes/experiments/SharedMetrics/sharedMetricLogic.tsx
+++ b/frontend/src/scenes/experiments/SharedMetrics/sharedMetricLogic.tsx
@@ -60,6 +60,7 @@ export interface sharedMetricLogicValues {
currentProjectId: number | string // teamLogic
action: 'create' | 'duplicate' | 'update'
breadcrumbs: Breadcrumb[]
+ metricSaving: boolean
newSharedMetric: {
created_at?: string | null | undefined
created_by?: UserBasicType | null | undefined
@@ -134,6 +135,9 @@ export interface sharedMetricLogicActions {
}
payload?: any
}
+ setMetricSaving: (saving: boolean) => {
+ saving: boolean
+ }
setSharedMetric: (metric: Partial<SharedMetric>) => {
metric: Partial<SharedMetric>
}
@@ -178,6 +182,7 @@ export const sharedMetricLogic = kea<sharedMetricLogicType>([
createSharedMetric: true,
updateSharedMetric: (redirect?: boolean) => ({ redirect }),
deleteSharedMetric: true,
+ setMetricSaving: (saving: boolean) => ({ saving }),
}),
loaders(({ props, values }) => ({
@@ -199,7 +204,7 @@ export const sharedMetricLogic = kea<sharedMetricLogicType>([
},
})),
- listeners(({ actions, props, values }) => ({
+ listeners(({ actions, props, values, cache }) => ({
/**
* we need to wait for the metric to load to check if we need to modify the name and id
*/
@@ -220,6 +225,10 @@ export const sharedMetricLogic = kea<sharedMetricLogicType>([
}
},
createSharedMetric: async () => {
+ if (values.metricSaving) {
+ return
+ }
+ actions.setMetricSaving(true)
try {
const response = await api.create(
`api/projects/${values.currentProjectId}/experiment_saved_metrics/`,
@@ -233,18 +242,41 @@ export const sharedMetricLogic = kea<sharedMetricLogicType>([
}
} catch (error: any) {
lemonToast.error(error.detail || error.data?.name?.[0] || 'Failed to create shared metric')
+ } finally {
+ actions.setMetricSaving(false)
}
},
updateSharedMetric: async ({ redirect = true }: { redirect?: boolean } = {}) => {
- const response = await api.update(
- `api/projects/${values.currentProjectId}/experiment_saved_metrics/${values.sharedMetricId}`,
- values.sharedMetric
- )
- if (response.id) {
- lemonToast.success('Shared metric updated successfully')
- actions.loadSharedMetrics()
- if (redirect) {
- router.actions.push('/experiments?tab=shared-metrics')
+ if (values.metricSaving) {
+ // Queue a trailing rerun instead of dropping the call: the request reads
+ // values.sharedMetric at send time, so one rerun persists the newest state
+ // (e.g. a tag edit made while an explicit save was in flight)
+ cache.queuedUpdateRedirect = Boolean(cache.queuedUpdateRedirect) || redirect
+ cache.updateQueued = true
+ return
+ }
+ actions.setMetricSaving(true)
+ try {
+ const response = await api.update(
+ `api/projects/${values.currentProjectId}/experiment_saved_metrics/${values.sharedMetricId}`,
+ values.sharedMetric
+ )
+ if (response.id) {
+ lemonToast.success('Shared metric updated successfully')
+ actions.loadSharedMetrics()
+ if (redirect && !cache.updateQueued) {
+ router.actions.push('/experiments?tab=shared-metrics')
+ }
+ }
+ } catch (error: any) {
+ lemonToast.error(error.detail || error.data?.name?.[0] || 'Failed to update shared metric')
+ } finally {
+ actions.setMetricSaving(false)
+ if (cache.updateQueued) {
+ const queuedRedirect = Boolean(cache.queuedUpdateRedirect) || redirect
+ cache.updateQueued = false
+ cache.queuedUpdateRedirect = false
+ actions.updateSharedMetric(queuedRedirect)
}
}
},
@@ -270,6 +302,12 @@ export const sharedMetricLogic = kea<sharedMetricLogicType>([
setSharedMetric: (state, { metric }) => ({ ...state, ...metric }),
},
],
+ metricSaving: [
+ false,
+ {
+ setMetricSaving: (_, { saving }) => saving,
+ },
+ ],
}),
selectors({