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): respect decrease goal in significance notifications (#73285)

Thomas Obermüller2abfe797866b
diff --git a/posthog/temporal/experiments/test_significance_transition.py b/posthog/temporal/experiments/test_significance_transition.py
index 281acd4b04ea..8109f6dccd32 100644
--- a/posthog/temporal/experiments/test_significance_transition.py
+++ b/posthog/temporal/experiments/test_significance_transition.py
@@ -16,7 +16,7 @@ METRIC_DICT = {
"uuid": "metric-123",
"metric_type": "mean",
"source": {"kind": "EventsNode", "event": "$pageview"},
- "goal_direction": "increase",
+ "goal": "increase",
}
@@ -147,6 +147,73 @@ class TestCheckSignificanceTransition(BaseTest):
)
assert event.properties["variant_key"] == "control"
+ @parameterized.expand(
+ [
+ ("increase_goal", "increase", "increase", "99%"),
+ ("decrease_goal", "decrease", "decrease", "1%"),
+ ]
+ )
+ @patch("posthog.temporal.experiments.utils.produce_internal_event")
+ def test_goal_direction_and_chance_to_win(
+ self,
+ _name: str,
+ goal: str,
+ expected_goal_direction: str,
+ expected_chance_to_win: str,
+ mock_produce: MagicMock,
+ ) -> None:
+ flag = FeatureFlag.objects.create(team=self.team, key=f"test-flag-{goal}", created_by=self.user)
+ experiment = Experiment.objects.create(
+ team=self.team,
+ name="Goal Experiment",
+ feature_flag=flag,
+ start_date=datetime(2024, 1, 1, tzinfo=ZoneInfo("UTC")),
+ metrics=[{**METRIC_DICT, "goal": goal}],
+ )
+
+ result_dict = _make_result(["test"])
+ check_significance_transition(
+ experiment, "metric-123", "fp", result_dict, datetime(2024, 1, 10, tzinfo=ZoneInfo("UTC"))
+ )
+
+ mock_produce.assert_called_once()
+ event = (
+ mock_produce.call_args.kwargs.get("event")
+ or mock_produce.call_args[1].get("event")
+ or mock_produce.call_args[0][1]
+ )
+ assert event.properties["goal_direction"] == expected_goal_direction
+ assert event.properties["chance_to_win"] == expected_chance_to_win
+
+ @parameterized.expand([("increase",), ("decrease",)])
+ @patch("posthog.temporal.experiments.utils.produce_internal_event")
+ def test_missing_chance_to_win_reports_na(self, goal: str, mock_produce: MagicMock) -> None:
+ # Frequentist results have no chance_to_win; it must not be inverted into a fabricated 100%
+ flag = FeatureFlag.objects.create(team=self.team, key=f"test-flag-na-{goal}", created_by=self.user)
+ experiment = Experiment.objects.create(
+ team=self.team,
+ name="No Probability Experiment",
+ feature_flag=flag,
+ start_date=datetime(2024, 1, 1, tzinfo=ZoneInfo("UTC")),
+ metrics=[{**METRIC_DICT, "goal": goal}],
+ )
+
+ result_dict = _make_result(["test"])
+ for variant in result_dict["variant_results"]:
+ variant.pop("chance_to_win")
+
+ check_significance_transition(
+ experiment, "metric-123", "fp", result_dict, datetime(2024, 1, 10, tzinfo=ZoneInfo("UTC"))
+ )
+
+ mock_produce.assert_called_once()
+ event = (
+ mock_produce.call_args.kwargs.get("event")
+ or mock_produce.call_args[1].get("event")
+ or mock_produce.call_args[0][1]
+ )
+ assert event.properties["chance_to_win"] == "N/A"
+
@patch("posthog.temporal.experiments.utils.produce_internal_event")
def test_metric_name_fallback_to_event_name(self, mock_produce: MagicMock) -> None:
flag = FeatureFlag.objects.create(
@@ -164,7 +231,7 @@ class TestCheckSignificanceTransition(BaseTest):
"uuid": "metric-456",
"metric_type": "mean",
"source": {"kind": "EventsNode", "event": "purchase_completed"},
- "goal_direction": "increase",
+ "goal": "increase",
}
],
)
diff --git a/posthog/temporal/experiments/utils.py b/posthog/temporal/experiments/utils.py
index 8e4f2ec29ebf..ec379421a8d6 100644
--- a/posthog/temporal/experiments/utils.py
+++ b/posthog/temporal/experiments/utils.py
@@ -141,12 +141,19 @@ def check_significance_transition(
metric_dict = _find_metric_dict(experiment, metric_uuid)
metric_name = _get_metric_name(metric_dict) if metric_dict else "Untitled metric"
- goal_direction = metric_dict.get("goal_direction", "increase") if metric_dict else "increase"
+ goal_direction = metric_dict.get("goal", "increase") if metric_dict else "increase"
for variant_key in newly_significant:
variant_result = _get_variant_result(result_dict, variant_key)
- chance_to_win_raw = variant_result.get("chance_to_win", 0) if variant_result else 0
- chance_to_win = f"{round(chance_to_win_raw * 100)}%"
+ chance_to_win_raw = variant_result.get("chance_to_win") if variant_result else None
+ if chance_to_win_raw is None:
+ # No probability computed (e.g. frequentist results) — don't fabricate one
+ chance_to_win = "N/A"
+ else:
+ # chance_to_win is P(variant > control); invert it for decrease goals so lower is better
+ if goal_direction == "decrease":
+ chance_to_win_raw = 1 - chance_to_win_raw
+ chance_to_win = f"{round(chance_to_win_raw * 100)}%"
raw_change = _get_relative_change(result_dict, variant_key)
relative_change = f"({raw_change})" if raw_change else ""