: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(notifications): add experiment_concluded realtime notification type (#56159)
Yasen2a799c073ca3
diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsxindex 412322b21ffe..1a1cf11fbbbf 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx@@ -37,6 +37,10 @@ export const REALTIME_NOTIFICATION_TYPE_META: Record<string, { label: string; delabel: 'Issues assigned',description: 'When an error tracking issue is assigned to you',},+ experiment_concluded: {+ label: 'Experiments concluded',+ description: 'When an experiment you created ends',+ },}export function NotificationRow({diff --git a/frontend/src/lib/components/NotificationsMenu/notificationToasts.tsx b/frontend/src/lib/components/NotificationsMenu/notificationToasts.tsxindex 3b7027f6e9ab..3105888ac199 100644--- a/frontend/src/lib/components/NotificationsMenu/notificationToasts.tsx+++ b/frontend/src/lib/components/NotificationsMenu/notificationToasts.tsx@@ -1,4 +1,12 @@-import { IconBug, IconCheckCircle, IconComment, IconNotification, IconPlug, IconWarning } from '@posthog/icons'+import {+ IconBug,+ IconCheckCircle,+ IconComment,+ IconFlask,+ IconNotification,+ IconPlug,+ IconWarning,+} from '@posthog/icons'import { lemonToast } from '@posthog/lemon-ui'import { notificationsMenuLogic } from 'lib/components/NotificationsMenu/notificationsMenuLogic'@@ -12,6 +20,7 @@ const NOTIFICATION_TYPE_ICONS: Record<string, JSX.Element> = {approval_resolved: <IconCheckCircle className="size-5 text-success shrink-0" />,pipeline_failure: <IconPlug className="size-5 text-danger shrink-0" />,issue_assigned: <IconBug className="size-5 text-primary shrink-0" />,+ experiment_concluded: <IconFlask className="size-5 text-primary shrink-0" />,}export function getNotificationIcon(notificationType: string): JSX.Element {diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex f39537070d30..c52703ac5a1a 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -44,6 +44,14 @@ from products.experiments.backend.models.experiment import (holdout_filters_for_flag,)from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig+from products.notifications.backend.facade.api import (+ NotificationData,+ NotificationType,+ Priority,+ SourceType,+ TargetType,+ create_notification,+)from ee.clickhouse.views.experiment_saved_metrics import ExperimentToSavedMetricSerializer@@ -1181,6 +1189,40 @@ class ExperimentService:request=request,)+ # Skip notifying the creator when they're the one ending the experiment —+ # surfacing a notification for an action they just performed is noise.+ if experiment.created_by_id and experiment.created_by_id != self.user.id:+ try:+ significant = completed_metadata.get("significant")+ body = ""+ if significant is True:+ body = "Primary metric: significant"+ elif significant is False:+ body = "Primary metric: inconclusive"++ create_notification(+ NotificationData(+ team_id=experiment.team_id,+ notification_type=NotificationType.EXPERIMENT_CONCLUDED,+ priority=Priority.NORMAL,+ title=f"Experiment concluded: {experiment.name}"[:100],+ body=body,+ target_type=TargetType.USER,+ target_id=str(experiment.created_by_id),+ resource_type="experiment",+ resource_id=str(experiment.id),+ source_url=f"/project/{self.team.project_id}/experiments/{experiment.id}",+ source_type=SourceType.EXPERIMENT,+ source_id=str(experiment.id),+ )+ )+ except Exception as e:+ logger.exception(+ "experiment_concluded.realtime_failed",+ experiment_id=experiment.id,+ error=str(e),+ )+# ------------------------------------------------------------------# Reset# ------------------------------------------------------------------diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex c76ac2614bbf..791132a33fba 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -1882,6 +1882,122 @@ class TestExperimentService(APIBaseTest):metadata = completed_call.args[2]assert "significant" not in metadata+ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_end_experiment_dispatches_realtime_to_creator(self, mock_create_notification):+ creator = self._create_user("[email protected]")+ experiment = self._create_running_experiment(name="End Notify", feature_flag_key="end-notify-flag")+ experiment.created_by = creator+ experiment.save()++ self._service().end_experiment(experiment, request=self._make_request())++ assert mock_create_notification.call_count == 1+ data = mock_create_notification.call_args.args[0]+ assert data.notification_type.value == "experiment_concluded"+ assert data.target_id == str(creator.id)+ assert data.team_id == self.team.id+ assert data.resource_type == "experiment"+ assert data.resource_id == str(experiment.id)++ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_ship_variant_running_dispatches_realtime(self, mock_create_notification):+ creator = self._create_user("[email protected]")+ experiment = self._create_running_experiment(name="Ship Notify", feature_flag_key="ship-notify-flag")+ experiment.created_by = creator+ experiment.save()++ self._service().ship_variant(experiment, variant_key="control", request=self._make_request())++ assert mock_create_notification.call_count == 1+ data = mock_create_notification.call_args.args[0]+ assert data.notification_type.value == "experiment_concluded"++ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_no_dispatch_when_actor_is_creator(self, mock_create_notification):+ # If the user ending the experiment is the creator, skip the self-notification.+ experiment = self._create_running_experiment(name="Self End", feature_flag_key="self-end-flag")+ assert experiment.created_by_id == self.user.id++ self._service().end_experiment(experiment, request=self._make_request())++ mock_create_notification.assert_not_called()++ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_ship_variant_already_stopped_does_not_dispatch(self, mock_create_notification):+ experiment = self._create_ended_experiment(+ name="Ship Stopped Notify", feature_flag_key="ship-stopped-notify-flag"+ )++ self._service().ship_variant(experiment, variant_key="control", request=self._make_request())++ mock_create_notification.assert_not_called()++ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_no_dispatch_when_experiment_has_no_creator(self, mock_create_notification):+ experiment = self._create_running_experiment(name="No Creator", feature_flag_key="no-creator-flag")+ experiment.created_by = None+ experiment.save()++ self._service().end_experiment(experiment, request=self._make_request())++ mock_create_notification.assert_not_called()++ @patch(+ "products.experiments.backend.experiment_service.create_notification",+ side_effect=RuntimeError("kafka down"),+ )+ def test_realtime_failure_does_not_block_end_experiment(self, _mock_create_notification):+ creator = self._create_user("[email protected]")+ experiment = self._create_running_experiment(name="Notify Failure", feature_flag_key="notify-failure-flag")+ experiment.created_by = creator+ experiment.save()++ # Must not raise.+ self._service().end_experiment(experiment, request=self._make_request())++ experiment.refresh_from_db()+ assert experiment.end_date is not None++ @parameterized.expand(+ [+ ("significant", {"significant": True, "variants": []}, "Primary metric: significant"),+ ("inconclusive", {"significant": False, "variants": []}, "Primary metric: inconclusive"),+ ("no_result", None, ""),+ ]+ )+ @patch("products.experiments.backend.experiment_service.create_notification")+ def test_end_experiment_body_reflects_primary_metric_outcome(+ self,+ _name: str,+ metric_result: dict | None,+ expected_body: str,+ mock_create_notification: MagicMock,+ ) -> None:+ creator = self._create_user(f"creator-body-{_name}@test.com")+ experiment = self._create_running_experiment(+ name=f"End Body {_name}", feature_flag_key=f"end-body-{_name.replace('_', '-')}-flag"+ )+ experiment.created_by = creator+ experiment.save()+ if metric_result is not None:+ assert experiment.metrics is not None+ assert experiment.start_date is not None+ ExperimentMetricResult.objects.create(+ experiment=experiment,+ metric_uuid=experiment.metrics[0]["uuid"],+ query_from=experiment.start_date,+ query_to=timezone.now(),+ status=ExperimentMetricResult.Status.COMPLETED,+ result=metric_result,+ completed_at=timezone.now(),+ )++ self._service().end_experiment(experiment, request=self._make_request())++ assert mock_create_notification.call_count == 1+ data = mock_create_notification.call_args.args[0]+ assert data.body == expected_body+# ------------------------------------------------------------------# Pause / Resume# ------------------------------------------------------------------diff --git a/products/notifications/backend/facade/enums.py b/products/notifications/backend/facade/enums.pyindex c2701d39f474..710823e6c8b8 100644--- a/products/notifications/backend/facade/enums.py+++ b/products/notifications/backend/facade/enums.py@@ -11,6 +11,7 @@ class NotificationType(str, Enum):ISSUE_ASSIGNED = "issue_assigned"APPROVAL_REQUESTED = "approval_requested"APPROVAL_RESOLVED = "approval_resolved"+ EXPERIMENT_CONCLUDED = "experiment_concluded"class Priority(str, Enum):diff --git a/products/notifications/backend/migrations/0009_alter_notificationevent_notification_type.py b/products/notifications/backend/migrations/0009_alter_notificationevent_notification_type.pynew file mode 100644index 000000000000..dfc9ebdaafca--- /dev/null+++ b/products/notifications/backend/migrations/0009_alter_notificationevent_notification_type.py@@ -0,0 +1,27 @@+# Generated by Django 5.2.13 on 2026-05-08 07:02++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("notifications", "0008_alter_notificationevent_notification_type"),+ ]++ operations = [+ migrations.AlterField(+ model_name="notificationevent",+ name="notification_type",+ field=models.CharField(+ choices=[+ ("comment_mention", "COMMENT_MENTION"),+ ("alert_firing", "ALERT_FIRING"),+ ("issue_assigned", "ISSUE_ASSIGNED"),+ ("approval_requested", "APPROVAL_REQUESTED"),+ ("approval_resolved", "APPROVAL_RESOLVED"),+ ("experiment_concluded", "EXPERIMENT_CONCLUDED"),+ ],+ max_length=32,+ ),+ ),+ ]diff --git a/products/notifications/backend/migrations/max_migration.txt b/products/notifications/backend/migrations/max_migration.txtindex ed9c42310dcc..8d979961e0da 100644--- a/products/notifications/backend/migrations/max_migration.txt+++ b/products/notifications/backend/migrations/max_migration.txt@@ -1 +1 @@-0008_alter_notificationevent_notification_type+0009_alter_notificationevent_notification_typediff --git a/tach.toml b/tach.tomlindex a21be1776e7c..de97d3f1c93d 100644--- a/tach.toml+++ b/tach.toml@@ -221,6 +221,7 @@ depends_on = ["ee","posthog","products.event_definitions",+ "products.notifications","products.product_tours","products.surveys",]