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.

feat(web-analytics): gate session id set rollout behind a feature flag (#72093)

Lucas Ricoyfd1f9142cce8
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_overview.py b/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
index 625c563f3e1a..062cfe06285b 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
@@ -1252,6 +1252,33 @@ class TestWebOverviewSessionIdSetFastPath(ClickhouseTestMixin, APIBaseTest):
runner = self._make_runner(**query_kwargs)
assert runner.should_use_session_id_set == expected
+ @parameterized.expand(
+ [
+ ("flag_expands_beyond_allowlist", False, True, True),
+ ("flag_off_fails_closed", False, False, False),
+ ("allowlist_independent_of_flag", True, False, True),
+ ("flag_evaluation_error_fails_closed", False, RuntimeError("flag service down"), False),
+ ]
+ )
+ def test_session_id_set_feature_flag_gating(
+ self, _name: str, in_allowlist: bool, flag_behavior: bool | Exception, expected: bool
+ ):
+ allowlist = [self.team.pk] if in_allowlist else []
+ flag_mock_kwargs = (
+ {"side_effect": flag_behavior} if isinstance(flag_behavior, Exception) else {"return_value": flag_behavior}
+ )
+ with (
+ override_settings(WEB_ANALYTICS_SESSION_ID_SET_TEAM_IDS=allowlist),
+ patch(
+ "products.web_analytics.backend.hogql_queries.web_analytics_query_runner.posthoganalytics.feature_enabled",
+ **flag_mock_kwargs,
+ ),
+ ):
+ runner = self._make_runner(
+ properties=[EventPropertyFilter(key="$pathname", operator=PropertyOperator.EXACT, value="/pricing")]
+ )
+ assert runner.should_use_session_id_set == expected
+
@parameterized.expand(
[
(
diff --git a/products/web_analytics/backend/hogql_queries/web_analytics_query_runner.py b/products/web_analytics/backend/hogql_queries/web_analytics_query_runner.py
index cc49238b47e9..e7d50fa60065 100644
--- a/products/web_analytics/backend/hogql_queries/web_analytics_query_runner.py
+++ b/products/web_analytics/backend/hogql_queries/web_analytics_query_runner.py
@@ -9,6 +9,7 @@ from zoneinfo import ZoneInfo
from django.conf import settings
import structlog
+import posthoganalytics
from prometheus_client import Counter
from structlog.contextvars import bound_contextvars
@@ -71,6 +72,10 @@ WEB_ANALYTICS_NO_JOIN_SERVED = Counter(
# under half the join's typical footprint — with margin before the crossover.
SESSION_ID_SET_MAX_MATCHING_SESSIONS = 10_000_000
+# Expands the env allowlist without a deploy: per-team/percent targeting and the
+# kill switch live in the flag UI. The allowlist stays as the flag-independent base.
+SESSION_ID_SET_FEATURE_FLAG_KEY = "web-analytics-session-id-set"
+
WebQueryNode = Union[
WebOverviewQuery,
WebStatsTableQuery,
@@ -250,6 +255,34 @@ class WebAnalyticsQueryRunner(AnalyticsQueryRunner[WAR], ABC):
# path for everyone on a team, so the rollout unit is the team, not the user.
return percent > 0 and self.team.pk % 100 < percent
+ @cached_property
+ def _session_id_set_flag_enabled(self) -> bool:
+ """Evaluate the rollout flag locally — fails closed on flag-service errors.
+
+ A raised exception here must never fail the query: the fast path is an
+ optimization, so any flag-evaluation failure degrades to the join path.
+ """
+ try:
+ return bool(
+ posthoganalytics.feature_enabled(
+ SESSION_ID_SET_FEATURE_FLAG_KEY,
+ str(self.team.uuid),
+ groups={
+ "organization": str(self.team.organization_id),
+ "project": str(self.team.id),
+ },
+ group_properties={
+ "organization": {"id": str(self.team.organization_id)},
+ "project": {"id": str(self.team.id)},
+ },
+ only_evaluate_locally=True,
+ send_feature_flag_events=False,
+ )
+ )
+ except Exception as e:
+ logger.warning("web_analytics_session_id_set_flag_evaluation_failed", error=e, team_id=self.team.pk)
+ return False
+
def _session_id_set_common_eligibility(self) -> bool:
"""Shared gates for the session-id-set fast paths (filtered two-scan shape).
@@ -258,7 +291,7 @@ class WebAnalyticsQueryRunner(AnalyticsQueryRunner[WAR], ABC):
person-on-events). Session/cohort filters can't feed the id collection
and keep the join path. Runners add their own shape-specific gates on top.
"""
- if self.team.pk not in settings.WEB_ANALYTICS_SESSION_ID_SET_TEAM_IDS:
+ if self.team.pk not in settings.WEB_ANALYTICS_SESSION_ID_SET_TEAM_IDS and not self._session_id_set_flag_enabled:
return False
if getattr(self.query, "conversionGoal", None):
return False