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(web-analytics): include unknown regions and cities in breakdown totals (#59673)

Lucas Ricoyba6529a150cd
diff --git a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx
index 35b9b2b700cb..a8241545f5a6 100644
--- a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx
+++ b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx
@@ -328,6 +328,10 @@ const BreakdownValueTitle: QueryContextColumnTitleComponent = (props) => {
}
}
+const NotSetBreakdownLabel = (): JSX.Element => <span className="text-secondary italic">(not set)</span>
+
+const DirectBreakdownLabel = (): JSX.Element => <span className="text-secondary italic">(direct)</span>
+
const BreakdownValueCell: QueryContextColumnComponent = (props) => {
const { value, query } = props
const { source } = query
@@ -383,14 +387,15 @@ const BreakdownValueCell: QueryContextColumnComponent = (props) => {
</>
)
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.Region:
if (Array.isArray(value)) {
const [countryCode, regionCode, regionName] = value
+ const regionLabel = regionName || regionCode
return (
<>
{countryCodeToFlag(countryCode)} {COUNTRY_CODE_TO_LONG_NAME[countryCode] || countryCode} -{' '}
- {regionName || regionCode}
+ {regionLabel ? regionLabel : <NotSetBreakdownLabel />}
</>
)
}
@@ -401,7 +406,7 @@ const BreakdownValueCell: QueryContextColumnComponent = (props) => {
return (
<>
{countryCodeToFlag(countryCode)} {COUNTRY_CODE_TO_LONG_NAME[countryCode] || countryCode} -{' '}
- {cityName}
+ {cityName ? cityName : <NotSetBreakdownLabel />}
</>
)
}
@@ -410,7 +415,7 @@ const BreakdownValueCell: QueryContextColumnComponent = (props) => {
if (typeof value === 'number') {
return <>{toUtcOffsetFormat(value)}</>
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.Language:
if (typeof value === 'string') {
const [languageCode, countryCode] = value.split('-')
@@ -425,23 +430,28 @@ const BreakdownValueCell: QueryContextColumnComponent = (props) => {
</>
)
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.DeviceType:
if (typeof value === 'string') {
return <PropertyIcon.WithLabel property="$device_type" value={value} />
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.Browser:
if (typeof value === 'string') {
return <PropertyIcon.WithLabel property="$browser" value={value} />
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.OS:
if (typeof value === 'string') {
return <PropertyIcon.WithLabel property="$os" value={value} />
}
- break
+ return <NotSetBreakdownLabel />
case WebStatsBreakdown.InitialReferringDomain:
+ // NULL referrer is canonically "Direct" in PostHog (matches the channel-type
+ // bucketing in sessions_v2). Keep that wording to stay consistent across tiles.
+ if (value == null) {
+ return <DirectBreakdownLabel />
+ }
if (featureFlags[FEATURE_FLAGS.SHOW_REFERRER_FAVICON]) {
if (typeof value === 'string') {
return (
@@ -464,6 +474,12 @@ const BreakdownValueCell: QueryContextColumnComponent = (props) => {
return <>{value.replace(BREAKDOWN_REFERRER_PREFIX, '')}</>
}
break
+ case WebStatsBreakdown.InitialUTMSource:
+ case WebStatsBreakdown.InitialUTMMedium:
+ case WebStatsBreakdown.InitialUTMCampaign:
+ case WebStatsBreakdown.InitialUTMTerm:
+ case WebStatsBreakdown.InitialUTMContent:
+ return typeof value === 'string' ? <>{value}</> : <NotSetBreakdownLabel />
}
if (typeof value === 'string') {
@@ -1094,12 +1110,14 @@ const getBreakdownValue = (record: unknown, breakdownBy: WebStatsBreakdown): str
break
case WebStatsBreakdown.Region:
if (Array.isArray(breakdownValue)) {
- return breakdownValue[1]
+ // Element 1 may be NULL when GeoIP can't resolve a subdivision; skip the
+ // click handler in that case since filtering by NULL has no useful behavior.
+ return breakdownValue[1] ?? undefined
}
break
case WebStatsBreakdown.City:
if (Array.isArray(breakdownValue)) {
- return breakdownValue[1]
+ return breakdownValue[1] ?? undefined
}
break
case WebStatsBreakdown.Viewport:
diff --git a/posthog/models/web_preaggregated/__snapshots__/test_web_pre_aggregated_timezones.ambr b/posthog/models/web_preaggregated/__snapshots__/test_web_pre_aggregated_timezones.ambr
index d8ab38ec83d7..3388e18ead93 100644
--- a/posthog/models/web_preaggregated/__snapshots__/test_web_pre_aggregated_timezones.ambr
+++ b/posthog/models/web_preaggregated/__snapshots__/test_web_pre_aggregated_timezones.ambr
@@ -55,7 +55,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -128,7 +127,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -201,7 +199,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -274,7 +271,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -347,7 +343,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -420,7 +415,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -493,7 +487,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -566,7 +559,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -639,7 +631,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -712,7 +703,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -785,7 +775,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -858,7 +847,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -931,7 +919,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -1004,7 +991,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -1077,7 +1063,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
diff --git a/products/web_analytics/backend/hogql_queries/stats_table.py b/products/web_analytics/backend/hogql_queries/stats_table.py
index 126ded3dc7bb..363465f4dc0d 100644
--- a/products/web_analytics/backend/hogql_queries/stats_table.py
+++ b/products/web_analytics/backend/hogql_queries/stats_table.py
@@ -598,20 +598,34 @@ class WebStatsTableQueryRunner(WebAnalyticsQueryRunner[WebStatsTableQueryRespons
def outer_where_breakdown(self) -> ast.Expr | None:
match self.query.breakdownBy:
case WebStatsBreakdown.REGION | WebStatsBreakdown.CITY:
- return parse_expr("tupleElement(`context.columns.breakdown_value`, 2) IS NOT NULL")
+ # Country (element 1) must be present; region/city (element 2) may be NULL when
+ # GeoIP can't resolve the subdivision/city — those rows surface in the UI as
+ # "(not set)" so totals stay consistent with the parent Country view.
+ return parse_expr("tupleElement(`context.columns.breakdown_value`, 1) IS NOT NULL")
case WebStatsBreakdown.VIEWPORT:
return parse_expr(
"tupleElement(`context.columns.breakdown_value`, 1) IS NOT NULL AND tupleElement(`context.columns.breakdown_value`, 2) IS NOT NULL AND "
"tupleElement(`context.columns.breakdown_value`, 1) != 0 AND tupleElement(`context.columns.breakdown_value`, 2) != 0"
)
case (
- WebStatsBreakdown.INITIAL_UTM_SOURCE
+ # Breakdowns where missing data is real and worth surfacing as "(not set)"
+ # rather than silently dropped — keeps totals consistent with the overview tile
+ # and parent breakdowns. Path-like breakdowns (PAGE/INITIAL_PAGE/EXIT_*/...) still
+ # drop NULLs via the default branch since a pageview without a path is junk.
+ WebStatsBreakdown.COUNTRY
+ | WebStatsBreakdown.BROWSER
+ | WebStatsBreakdown.OS
+ | WebStatsBreakdown.DEVICE_TYPE
+ | WebStatsBreakdown.LANGUAGE
+ | WebStatsBreakdown.TIMEZONE
+ | WebStatsBreakdown.INITIAL_REFERRING_DOMAIN
+ | WebStatsBreakdown.INITIAL_UTM_SOURCE
| WebStatsBreakdown.INITIAL_UTM_CAMPAIGN
| WebStatsBreakdown.INITIAL_UTM_MEDIUM
| WebStatsBreakdown.INITIAL_UTM_TERM
| WebStatsBreakdown.INITIAL_UTM_CONTENT
):
- return None # actually show null values
+ return None
case WebStatsBreakdown.INITIAL_CHANNEL_TYPE:
return parse_expr(
"`context.columns.breakdown_value` IS NOT NULL AND `context.columns.breakdown_value` != ''"
diff --git a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_sample_web_analytics_queries.hogql.ambr b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_sample_web_analytics_queries.hogql.ambr
index 7adfbcff881e..261b35679a2c 100644
--- a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_sample_web_analytics_queries.hogql.ambr
+++ b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_sample_web_analytics_queries.hogql.ambr
@@ -449,7 +449,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -475,7 +474,7 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(tupleElement(`context.columns.breakdown_value`, 2), NULL)
+ HAVING notEquals(tupleElement(`context.columns.breakdown_value`, 1), NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -501,7 +500,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -527,7 +525,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -685,7 +682,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -863,7 +859,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -889,7 +884,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -967,7 +961,7 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(tupleElement(`context.columns.breakdown_value`, 2), NULL)
+ HAVING notEquals(tupleElement(`context.columns.breakdown_value`, 1), NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -1019,7 +1013,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -1352,7 +1345,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING notEquals(`context.columns.breakdown_value`, NULL)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
diff --git a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_pre_aggregated.ambr b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_pre_aggregated.ambr
index 8645ddec661e..3a8b757d7069 100644
--- a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_pre_aggregated.ambr
+++ b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_pre_aggregated.ambr
@@ -55,7 +55,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -128,7 +127,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -201,7 +199,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
diff --git a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_table.ambr b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_table.ambr
index 0dc038be377d..ca8c7383228f 100644
--- a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_table.ambr
+++ b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_web_stats_table.ambr
@@ -2661,7 +2661,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -4691,7 +4690,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -4752,7 +4750,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -4813,7 +4810,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
@@ -4874,7 +4870,6 @@
GROUP BY session_id,
breakdown_value)
GROUP BY `context.columns.breakdown_value`
- HAVING isNotNull(`context.columns.breakdown_value`)
ORDER BY `context.columns.visitors` DESC,
`context.columns.views` DESC,
`context.columns.breakdown_value` ASC
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_stats_table.py b/products/web_analytics/backend/hogql_queries/test/test_web_stats_table.py
index cd040281d3c5..3b9c25a2aa98 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_stats_table.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_stats_table.py
@@ -1358,7 +1358,13 @@ class TestWebStatsTableQueryRunner(ClickhouseTestMixin, APIBaseTest, FloatAwareT
breakdown_by=WebStatsBreakdown.TIMEZONE,
).results
- assert results == []
+ # Invalid timezone offsets collapse to a single NULL row instead of being dropped,
+ # so totals stay consistent with the overview tile (the frontend renders this as
+ # "(not set)").
+ assert len(results) == 1
+ assert results[0][0] is None
+ assert results[0][1][0] == idx + 1 # visitors == number of distinct persons seen so far
+ assert results[0][2][0] == sum(range(idx + 2)) # views == cumulative pageview count
def test_conversion_goal_no_conversions(self):
s1 = str(uuid7("2023-12-01"))