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): support initial referring url in lazy precompute (#61506)

Lucas Ricoydf30b3cefa64
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
index 0344b926f2bb..0218b54594a1 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
@@ -37,6 +37,7 @@ PARITY_BREAKDOWNS = [
("region", WebStatsBreakdown.REGION),
("channel_type", WebStatsBreakdown.INITIAL_CHANNEL_TYPE),
("initial_referring_domain", WebStatsBreakdown.INITIAL_REFERRING_DOMAIN),
+ ("initial_referring_url", WebStatsBreakdown.INITIAL_REFERRING_URL),
]
@@ -302,13 +303,12 @@ class TestWebStatsLazyPrecompute(ClickhouseTestMixin, APIBaseTest):
("page", WebStatsBreakdown.PAGE),
("initial_page", WebStatsBreakdown.INITIAL_PAGE),
("exit_click", WebStatsBreakdown.EXIT_CLICK),
- ("initial_referring_url", WebStatsBreakdown.INITIAL_REFERRING_URL),
]
)
@freeze_time("2024-01-15T12:00:00Z")
def test_high_cardinality_breakdown_falls_through(self, _name: str, breakdown_by: WebStatsBreakdown):
- # Page/path/referring-URL breakdowns are intentionally excluded — too
- # high cardinality for the in-Python read. They fall through to raw.
+ # Page/path breakdowns are intentionally excluded — handled by the
+ # dedicated paths lazy precompute. They fall through to raw here.
self._seed()
with self._enable_lazy():
self._run(self._build_query(breakdown_by=breakdown_by))
diff --git a/products/web_analytics/backend/hogql_queries/web_stats_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/web_stats_lazy_precompute.py
index fd2f0a682257..cb66c5e813a7 100644
--- a/products/web_analytics/backend/hogql_queries/web_stats_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/web_stats_lazy_precompute.py
@@ -57,21 +57,26 @@ WEB_STATS_LAZY_FAILED = Counter(
# Breakdowns served by `SimpleBreakdownStrategy` / `ChannelTypeStrategy` that we
# expect to behave well under precompute. UTM/browser/OS/region/city values are
# user-controlled at ingestion so cardinality is not strictly bounded, but in
-# practice they sit several orders of magnitude below the per-URL breakdowns we
-# deliberately route to the raw path (page/path/referring-URL, FRUSTRATION_METRICS,
-# LANGUAGE). The failure mode for a pathological team is a slow precompute INSERT,
-# not an incorrect result — sort, pagination and HAVING all run in SQL so the
-# read returns exactly the page the user asked for regardless of total distinct
+# practice they sit several orders of magnitude below the per-page breakdowns we
+# deliberately route to the raw path (page/path, FRUSTRATION_METRICS, LANGUAGE).
+# The failure mode for a pathological team is a slow precompute INSERT, not an
+# incorrect result — sort, pagination and HAVING all run in SQL so the read
+# returns exactly the page the user asked for regardless of total distinct
# values. If a team's INSERT becomes a hotspot we can carve them out via the
# rollout gate; we are not adding a defensive SQL-side cardinality cap because
# it would silently truncate results vs. the raw path.
#
+# INITIAL_REFERRING_URL is the one high-cardinality URL breakdown we precompute:
+# the lazy INSERT runs per daily bucket, which stays tractable where the 28-day
+# raw query times out, so precompute is strictly better than the raw fallback.
+#
# Tuple/float breakdowns (REGION, CITY, VIEWPORT, TIMEZONE) are supported: the
# breakdown value is JSON-encoded into the `breakdown_value` String column and
# decoded back to its native shape on read.
SUPPORTED_BREAKDOWNS: set[WebStatsBreakdown] = {
WebStatsBreakdown.INITIAL_CHANNEL_TYPE,
WebStatsBreakdown.INITIAL_REFERRING_DOMAIN,
+ WebStatsBreakdown.INITIAL_REFERRING_URL,
WebStatsBreakdown.INITIAL_UTM_SOURCE,
WebStatsBreakdown.INITIAL_UTM_CAMPAIGN,
WebStatsBreakdown.INITIAL_UTM_MEDIUM,