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(dashboards): preserve widget flag evaluation context (#87373)

Matt Puaa99d634b3d68
diff --git a/posthog/api/test/dashboards/test_dashboard.py b/posthog/api/test/dashboards/test_dashboard.py
index 9f2ad55eeab0..e68a1fa03d17 100644
--- a/posthog/api/test/dashboards/test_dashboard.py
+++ b/posthog/api/test/dashboards/test_dashboard.py
@@ -679,7 +679,7 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):
"products.dashboards.backend.feature_flags.get_flags_from_service",
return_value={"flags": {"dashboard-customization": {"enabled": True}}},
)
- def test_dashboard_customization_uses_remote_flag_evaluation(self, _mock_get_flags: MagicMock) -> None:
+ def test_dashboard_customization_uses_remote_flag_evaluation(self, mock_get_flags: MagicMock) -> None:
dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})
_, updated = self.dashboard_api.update_dashboard(
@@ -699,13 +699,10 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):
internal_request_token=settings.INTERNAL_REQUEST_TOKEN,
evaluation_runtime="all",
)
- self.assertGreater(len(_mock_get_flags.call_args_list), 0)
- self.assertTrue(all(flag_call == expected_flag_call for flag_call in _mock_get_flags.call_args_list))
+ self.assertGreater(len(mock_get_flags.call_args_list), 0)
+ self.assertTrue(all(flag_call == expected_flag_call for flag_call in mock_get_flags.call_args_list))
- @patch(
- "products.dashboards.backend.feature_flags.get_flags_from_service",
- side_effect=ConnectionError,
- )
+ @patch("products.dashboards.backend.feature_flags.get_flags_from_service", side_effect=ConnectionError)
def test_dashboard_customization_fails_closed_when_remote_evaluation_fails(
self, _mock_get_flags: MagicMock
) -> None:
diff --git a/products/dashboards/backend/feature_flags.py b/products/dashboards/backend/feature_flags.py
index 5f8cf24c16ed..53ae99f043c1 100644
--- a/products/dashboards/backend/feature_flags.py
+++ b/products/dashboards/backend/feature_flags.py
@@ -4,6 +4,8 @@ from typing import TYPE_CHECKING
from django.conf import settings
+import posthoganalytics
+
from posthog.api.services.flags_service import get_flags_from_service
from posthog.permissions import _FORCE_ENABLED_FLAGS
@@ -16,7 +18,28 @@ DASHBOARD_CUSTOMIZATION_FLAG = "dashboard-customization"
def widget_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> bool:
- """Match in-app flag evaluation: user distinct_id plus project/org groups."""
+ """Match the existing in-app widget flag evaluation."""
+ if flag in _FORCE_ENABLED_FLAGS:
+ return True
+
+ distinct_id = (user.distinct_id or str(user.uuid)) if user is not None else str(team.uuid)
+ organization_id = str(team.organization_id)
+ project_id = str(team.id)
+
+ return bool(
+ posthoganalytics.feature_enabled(
+ flag,
+ distinct_id,
+ groups={"organization": organization_id, "project": project_id},
+ group_properties={"organization": {"id": organization_id}, "project": {"id": project_id}},
+ only_evaluate_locally=False,
+ send_feature_flag_events=False,
+ )
+ )
+
+
+def _remote_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> bool:
+ """Evaluate flags whose rollout depends on cohort membership."""
if flag in _FORCE_ENABLED_FLAGS:
return True
@@ -25,7 +48,6 @@ def widget_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> b
project_id = str(team.id)
try:
- # Cohort targeting needs the remote evaluator; process-local definitions do not include membership.
result = get_flags_from_service(
team.api_token,
distinct_id,
@@ -44,4 +66,4 @@ def dashboard_widgets_enabled(*, team: Team, user: User | None = None) -> bool:
def dashboard_customization_enabled(*, team: Team, user: User | None = None) -> bool:
- return widget_flag_enabled(DASHBOARD_CUSTOMIZATION_FLAG, team=team, user=user)
+ return _remote_flag_enabled(DASHBOARD_CUSTOMIZATION_FLAG, team=team, user=user)