: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(web-analytics): gate lazy precompute on org-level feature flag (#59510)
Lucas Ricoy1d7438aa90d1
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex b4f5da8d963c..be82fbd777f0 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -40089,7 +40089,7 @@"type": "boolean"},"useWebAnalyticsPrecompute": {- "description": "Opt this specific query into the web_overview_query precompute path. Requires the team to be in the `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` allowlist for the gate to pass. *",+ "description": "Opt this specific query into the web_overview_query precompute path. Requires the `web-analytics-precompute-toggle` PostHog feature flag to be on for the team's organization for the gate to pass. *","type": "boolean"},"version": {diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex e07621cbb4c0..572353f04c27 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -2287,7 +2287,7 @@ interface WebAnalyticsQueryBase<R extends Record<string, any>> extends DataNode<export interface WebOverviewQuery extends WebAnalyticsQueryBase<WebOverviewQueryResponse> {kind: NodeKind.WebOverviewQuery- /** Opt this specific query into the web_overview_query precompute path. Requires the team to be in the `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` allowlist for the gate to pass. **/+ /** Opt this specific query into the web_overview_query precompute path. Requires the `web-analytics-precompute-toggle` PostHog feature flag to be on for the team's organization for the gate to pass. **/useWebAnalyticsPrecompute?: boolean}diff --git a/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx b/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsxindex 5cab78829133..be2cabb6cf7b 100644--- a/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx+++ b/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx@@ -60,7 +60,7 @@ export const WebAnalyticsMenu = (): JSX.Element => {Filter out internal and test users</ButtonPrimitive>{featureFlags[FEATURE_FLAGS.WEB_ANALYTICS_PRECOMPUTE_TOGGLE] && (- <Tooltip title="Serve eligible Web Overview queries from the precomputed cache. Only takes effect for teams enrolled by the admin via WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS.">+ <Tooltip title="Serve eligible Web Overview queries from the precomputed cache. Only takes effect when the web-analytics-precompute-toggle feature flag is on for the team's organization."><ButtonPrimitivemenuItemonClick={() => {diff --git a/posthog/hogql_queries/web_analytics/test/test_web_overview_lazy_precompute.py b/posthog/hogql_queries/web_analytics/test/test_web_overview_lazy_precompute.pyindex d9f03b175486..b9f6ca81232f 100644--- a/posthog/hogql_queries/web_analytics/test/test_web_overview_lazy_precompute.py+++ b/posthog/hogql_queries/web_analytics/test/test_web_overview_lazy_precompute.py@@ -21,7 +21,6 @@ from posthog.schema import ()from posthog.hogql_queries.web_analytics.web_overview import WebOverviewQueryRunner-from posthog.models.instance_setting import override_instance_configfrom posthog.models.utils import uuid7from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import LazyComputationResult@@ -35,7 +34,13 @@ class TestWebOverviewLazyPrecompute(ClickhouseTestMixin, APIBaseTest):PreaggregationJob.objects.filter(team_id=self.team.pk).delete()def _enable_lazy(self):- return override_instance_config("WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS", [self.team.pk])+ # Mock the org-level feature flag check to True so the gate accepts our test+ # team. Outside this context manager the default `posthoganalytics.feature_enabled`+ # returns False (no API key in tests), which models a flag-disabled org.+ return patch(+ "posthog.hogql_queries.web_analytics.web_overview_lazy_precompute.posthoganalytics.feature_enabled",+ return_value=True,+ )def _seed_two_sessions(self) -> None:# p1 has two pageviews in one session, p2 has a single pageview (bounce).@@ -207,27 +212,27 @@ class TestWebOverviewLazyPrecompute(ClickhouseTestMixin, APIBaseTest):@freeze_time("2024-01-15T12:00:00Z")def test_disabled_team_falls_through(self):- # Both gates closed: not in allowlist AND modifier not set.+ # Both gates closed: org feature flag off AND query opt-in not set.self._seed_two_sessions()self._run(self._build_query(opt_in_precompute=False))assert PreaggregationJob.objects.filter(team_id=self.team.pk).count() == 0@freeze_time("2024-01-15T12:00:00Z")- def test_query_optin_alone_falls_through_when_team_not_enrolled(self):- # `query.useWebAnalyticsPrecompute=True` BUT team is not in the- # `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` allowlist. Should fall- # through — the env var is the operator-controlled rollout gate.+ def test_query_optin_alone_falls_through_when_org_flag_disabled(self):+ # `query.useWebAnalyticsPrecompute=True` BUT the+ # `web-analytics-precompute-toggle` feature flag is off. Should+ # fall through — the flag is the operator-controlled rollout gate.self._seed_two_sessions()self._run(self._build_query(opt_in_precompute=True))assert PreaggregationJob.objects.filter(team_id=self.team.pk).count() == 0@freeze_time("2024-01-15T12:00:00Z")- def test_allowlist_alone_falls_through_when_query_not_opted_in(self):- # Team is in the allowlist BUT the query param is not set (the team- # hasn't enabled the "Allow precompute" toggle in the ScenePanel).- # Should fall through — the param is the per-team opt-in / kill switch.+ def test_org_flag_alone_falls_through_when_query_not_opted_in(self):+ # Org feature flag is on BUT the query param is not set (the team hasn't+ # enabled the "Allow precompute" toggle in the ScenePanel). Should fall+ # through — the param is the per-team opt-in / kill switch.self._seed_two_sessions()with self._enable_lazy():self._run(self._build_query(opt_in_precompute=False))diff --git a/posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py b/posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.pyindex 9df8da6527c1..6ae2e2205981 100644--- a/posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py+++ b/posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py@@ -3,6 +3,7 @@ from datetime import UTC, datetime, timedeltafrom typing import TYPE_CHECKING, Optionalimport structlog+import posthoganalyticsfrom prometheus_client import Counterfrom posthog.schema import EventPropertyFilter, PropertyOperator@@ -16,7 +17,6 @@ from posthog.clickhouse.preaggregation.web_overview_preaggregated_sql import (DISTRIBUTED_WEB_OVERVIEW_PREAGGREGATED_TABLE,)from posthog.clickhouse.query_tagging import Feature, Product, tag_queries-from posthog.models.instance_setting import get_instance_settingfrom products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (LazyComputationResult,@@ -67,7 +67,7 @@ class LazyPrecomputeIneligible(Exception):"""-class TeamNotInAllowlist(LazyPrecomputeIneligible):+class OrgFeatureFlagDisabled(LazyPrecomputeIneligible):pass@@ -150,14 +150,32 @@ def _check_lazy_precompute_eligible(runner: "WebOverviewQueryRunner") -> None:the lazy path. Returns None on success."""query = runner.query- # Rollout gate: instance allowlist AND per-query opt-in.- # - `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` (admin-controlled instance- # setting): scope of teams eligible to opt in.+ # Rollout gate: shared PostHog feature flag AND per-query opt-in.+ # - `web-analytics-precompute-toggle` (PostHog feature flag): the same+ # flag the frontend already uses to show/hide the "Allow precompute"+ # button in the Web Analytics ScenePanel. One switch controls both the+ # UI surface and the backend gate. The flag is evaluated at the+ # organization level — set up an org-level release condition on the+ # flag to enable rollout per org. The SDK swallows its own exceptions+ # and returns None (falsy) on failure, so a flag-service outage+ # fails-closed (gate raised, fall back to v2 / raw) automatically.# - `query.useWebAnalyticsPrecompute` (per-query parameter set by the- # "Allow precompute" toggle in the Web Analytics ScenePanel).- enabled_team_ids = get_instance_setting("WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS") or []- if runner.team.pk not in enabled_team_ids:- raise TeamNotInAllowlist()+ # "Allow precompute" toggle).+ if not posthoganalytics.feature_enabled(+ "web-analytics-precompute-toggle",+ str(runner.team.uuid),+ groups={+ "organization": str(runner.team.organization_id),+ "project": str(runner.team.id),+ },+ group_properties={+ "organization": {"id": str(runner.team.organization_id)},+ "project": {"id": str(runner.team.id)},+ },+ only_evaluate_locally=True,+ send_feature_flag_events=False,+ ):+ raise OrgFeatureFlagDisabled()if query.useWebAnalyticsPrecompute is not True:raise PerQueryOptInNotSet()diff --git a/posthog/schema.py b/posthog/schema.pyindex 420a4f544ccc..95183a3445c2 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -18702,8 +18702,8 @@ class WebOverviewQuery(BaseModel):default=None,description=("Opt this specific query into the web_overview_query precompute path."- " Requires the team to be in the `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS`"- " allowlist for the gate to pass. *"+ " Requires the `web-analytics-precompute-toggle` PostHog feature flag to be"+ " on for the team's organization for the gate to pass. *"),)version: float | None = Field(default=None, description="version of the node, used for schema migrations")diff --git a/posthog/settings/dynamic_settings.py b/posthog/settings/dynamic_settings.pyindex b8adb6977cbc..aca278bc6dfe 100644--- a/posthog/settings/dynamic_settings.py+++ b/posthog/settings/dynamic_settings.py@@ -279,11 +279,6 @@ CONSTANCE_CONFIG = {"Team IDs that use prefiltered events subqueries in web analytics bounce/scroll queries for better granule pruning",list[int],),- "WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS": (- get_from_env("WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS", default=[], type_cast=list[int]),- "Team IDs enrolled in the web_overview_query lazy precompute path. Admin-controlled rollout gate.",- list[int],- ),}SETTINGS_ALLOWING_API_OVERRIDE = (@@ -333,7 +328,6 @@ SETTINGS_ALLOWING_API_OVERRIDE = ("CLICKHOUSE_KILL_SWITCH_FULL_TEAMS","CLICKHOUSE_HEDGED_APP_QUERIES","WEB_ANALYTICS_EVENTS_PREFILTER_TEAM_IDS",- "WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS","REDIRECT_APP_TO_US","WEB_ANALYTICS_WARMING_DAYS","WEB_ANALYTICS_WARMING_MIN_QUERY_COUNT",diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.tsindex e6669e587162..70a85baf7138 100644--- a/products/product_analytics/frontend/generated/api.schemas.ts+++ b/products/product_analytics/frontend/generated/api.schemas.ts@@ -2848,7 +2848,7 @@ export interface WebOverviewQueryApi {samplingFactor?: number | nulltags?: QueryLogTagsApi | nulluseSessionsTable?: boolean | null- /** Opt this specific query into the web_overview_query precompute path. Requires the team to be in the `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` allowlist for the gate to pass. * */+ /** Opt this specific query into the web_overview_query precompute path. Requires the `web-analytics-precompute-toggle` PostHog feature flag to be on for the team's organization for the gate to pass. * */useWebAnalyticsPrecompute?: boolean | null/** version of the node, used for schema migrations */version?: number | nulldiff --git a/products/web_analytics/PRECOMPUTATION.md b/products/web_analytics/PRECOMPUTATION.mdindex 3b368816434f..351075945fb1 100644--- a/products/web_analytics/PRECOMPUTATION.md+++ b/products/web_analytics/PRECOMPUTATION.md@@ -15,12 +15,12 @@ DAG-warmed ClickHouse tables (`web_pre_aggregated_stats`, `web_pre_aggregated_bo### Lazy computation-The newer general-purpose framework at `products/analytics_platform/backend/lazy_computation/`. Computes precomputed buckets on first read, caches them in a dedicated CH table per query family, and serves subsequent reads from the cache. Gated per-team by `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` instance setting (defaults to empty).+The newer general-purpose framework at `products/analytics_platform/backend/lazy_computation/`. Computes precomputed buckets on first read, caches them in a dedicated CH table per query family, and serves subsequent reads from the cache. Gated per-org by the `web-analytics-precompute-toggle` PostHog feature flag (evaluated against the team's organization).- **Owned by**: web analytics team, riding on the analytics_platform framework- **Population**: synchronous, on first read miss; subsequent reads hit the cache- **Coverage** (today): `web_overview_query` only — see `posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py`-- **Adoption** (as of 2026-05): freshly enabled, allowlist gates further rollout+- **Adoption** (as of 2026-05): freshly enabled, org feature flag gates further rollout## When to use which@@ -75,7 +75,7 @@ The pad value is the maximum realistic session duration. PostHog sessions cap at`can_use_lazy_precompute(runner)` in `web_overview_lazy_precompute.py`. Refuses when any of:-- Team is not in `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS`+- The `web-analytics-precompute-toggle` PostHog feature flag is off for the team's organization- Team timezone has a non-whole-hour UTC offset- `query.conversionGoal` is set- `query.sampling.enabled` is Truediff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 6ae5d55a0de3..04dce89ae2e3 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -2984,7 +2984,7 @@ export namespace Schemas {samplingFactor?: number | null;tags?: QueryLogTags | null;useSessionsTable?: boolean | null;- /** Opt this specific query into the web_overview_query precompute path. Requires the team to be in the `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` allowlist for the gate to pass. * */+ /** Opt this specific query into the web_overview_query precompute path. Requires the `web-analytics-precompute-toggle` PostHog feature flag to be on for the team's organization for the gate to pass. * */useWebAnalyticsPrecompute?: boolean | null;/** version of the node, used for schema migrations */version?: number | null;