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): skip launch event for imported, already-ended experiments (#120079)

Marcel Poelker58b510e58d7f
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.py
index ae196499e7cd..03acd969c91e 100644
--- a/products/experiments/backend/experiment_service.py
+++ b/products/experiments/backend/experiment_service.py
@@ -161,6 +161,14 @@ ExperimentCreationMode = Literal["new", "duplicate", "copy_to_project"]
# The launch action is not the only way to launch. A create call or an update that sets the start date launches too.
ExperimentLaunchPath = Literal["launch_endpoint", "create_request", "update_start_date"]
+# A create whose start_date is older than this is a backdated create (e.g. a move from another tool).
+BACKDATED_LAUNCH_THRESHOLD = timedelta(days=1)
+
+
+def _is_imported_concluded(experiment: Experiment) -> bool:
+ """An experiment created with an end_date that has passed was never launched in PostHog: it is an import."""
+ return experiment.end_date is not None and experiment.end_date <= timezone.now()
+
def _parse_tag_names(value: Any) -> list[str]:
"""Parse a tags query param that arrives as a list or a JSON-encoded string.
@@ -1331,7 +1339,7 @@ class ExperimentService:
creation_mode=creation_mode,
analytics_properties=analytics_properties,
)
- if experiment.start_date is not None:
+ if experiment.start_date is not None and not _is_imported_concluded(experiment):
self._report_experiment_launched(
experiment,
launch_path="create_request",
@@ -1385,6 +1393,11 @@ class ExperimentService:
analytics_metadata = experiment.get_analytics_metadata()
analytics_metadata["creation_mode"] = creation_mode
+ analytics_metadata["is_imported"] = _is_imported_concluded(experiment)
+ if experiment.start_date is not None:
+ analytics_metadata["start_date"] = experiment.start_date.isoformat()
+ if experiment.end_date is not None:
+ analytics_metadata["end_date"] = experiment.end_date.isoformat()
if event_source is not None:
analytics_metadata["source"] = event_source
if allow_unknown_events:
@@ -1423,6 +1436,9 @@ class ExperimentService:
extra_metadata={
"launch_date": experiment.start_date.isoformat() if experiment.start_date else None,
"launch_path": launch_path,
+ "is_backdated": launch_path == "create_request"
+ and experiment.start_date is not None
+ and timezone.now() - experiment.start_date > BACKDATED_LAUNCH_THRESHOLD,
"flag_age_seconds": int(flag_age.total_seconds()),
"health_finding_codes": [finding.code.value for finding in findings],
# pinned: `code:subcode`, as on `experiment viewed`. The code prefix keeps a subcode
diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.py
index 4b7cca61e96a..f363d332af51 100644
--- a/products/experiments/backend/test/test_experiment_service.py
+++ b/products/experiments/backend/test/test_experiment_service.py
@@ -4017,6 +4017,38 @@ class TestExperimentService(APIBaseTest):
for event in launched
] == expected
+ @parameterized.expand(
+ [
+ ("current", timedelta(0), None, False, [False]),
+ ("backdated_running", timedelta(days=90), None, False, [True]),
+ ("backdated_future_end", timedelta(days=90), timedelta(days=10), False, [True]),
+ ("imported_concluded", timedelta(days=90), timedelta(days=-30), True, []),
+ ]
+ )
+ @patch("products.experiments.backend.experiment_service.report_user_action")
+ def test_create_with_dates_reports_imports_and_backdated_launches(
+ self, _name, start_ago, end_from_now, expected_imported, expected_backdated, mock_report_user_action
+ ):
+ now = timezone.now()
+ with time_machine.travel(now, tick=False), self.captureOnCommitCallbacks(execute=True):
+ self._service().create_experiment(
+ name=f"Imported {_name}",
+ feature_flag_key=f"imported-{_name.replace('_', '-')}",
+ start_date=now - start_ago,
+ end_date=now + end_from_now if end_from_now is not None else None,
+ event_source=EventSource.API,
+ )
+
+ events = {call.args[1]: call.args[2] for call in mock_report_user_action.call_args_list}
+ launched = [
+ call.args[2]["is_backdated"]
+ for call in mock_report_user_action.call_args_list
+ if call.args[1] == "experiment launched"
+ ]
+ assert events["experiment created"]["is_imported"] is expected_imported
+ assert events["experiment created"]["start_date"] == (now - start_ago).isoformat()
+ assert launched == expected_backdated
+
@parameterized.expand(
[
(
diff --git a/products/experiments/backend/test/test_max_tools.py b/products/experiments/backend/test/test_max_tools.py
index c45b26e3c22e..ff928d171abc 100644
--- a/products/experiments/backend/test/test_max_tools.py
+++ b/products/experiments/backend/test/test_max_tools.py
@@ -181,6 +181,7 @@ class TestCreateExperimentTool(APIBaseTest):
"variant_count": 2,
"created_at": ANY,
"creation_mode": "new",
+ "is_imported": False,
"source": EventSource.POSTHOG_AI,
}
assert mock_report_user_action.call_args.kwargs["team"] == self.team
diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.py
index dfe3ea8652a9..7077df71ce41 100644
--- a/products/experiments/backend/test/test_presentation_api.py
+++ b/products/experiments/backend/test/test_presentation_api.py
@@ -1377,6 +1377,7 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
"variant_count": 2,
"created_at": ANY,
"creation_mode": "new",
+ "is_imported": False,
"experiment_create_deprecated_fields": ["filters"],
},
)
@@ -4985,6 +4986,7 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
"variant_count": 2,
"created_at": ANY,
"creation_mode": expected_mode,
+ "is_imported": False,
"allow_unknown_events": True,
"experiment_create_deprecated_fields": [],
},