: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
fix(mcp-analytics): count users by distinct_id to avoid person join oom (#121984)
Georgis Andonis9eb904c6c36b
diff --git a/products/mcp_analytics/backend/dashboard_templates.py b/products/mcp_analytics/backend/dashboard_templates.pyindex 238d7c99f016..bf7bdacd3d42 100644--- a/products/mcp_analytics/backend/dashboard_templates.py+++ b/products/mcp_analytics/backend/dashboard_templates.py@@ -59,7 +59,7 @@ def get_mcp_analytics_default_template() -> DashboardTemplate:"name": "$mcp_initialize","custom_name": "users","math": "hogql",- "math_hogql": "count(DISTINCT person_id)",+ "math_hogql": "count(DISTINCT distinct_id)",},{"kind": "EventsNode",diff --git a/products/mcp_analytics/backend/tests/test_dashboard_templates.py b/products/mcp_analytics/backend/tests/test_dashboard_templates.pyindex 3262e6812355..df25cda44900 100644--- a/products/mcp_analytics/backend/tests/test_dashboard_templates.py+++ b/products/mcp_analytics/backend/tests/test_dashboard_templates.py@@ -39,7 +39,7 @@ def test_mcp_analytics_default_template_includes_users_and_sessions() -> None:"name": "$mcp_initialize","custom_name": "users","math": "hogql",- "math_hogql": "count(DISTINCT person_id)",+ "math_hogql": "count(DISTINCT distinct_id)",},{"kind": "EventsNode",diff --git a/products/mcp_analytics/frontend/leaderboardHome/mcpLeaderboardHomeLogic.ts b/products/mcp_analytics/frontend/leaderboardHome/mcpLeaderboardHomeLogic.tsindex 16d2b278e0a4..5cb189ecd3da 100644--- a/products/mcp_analytics/frontend/leaderboardHome/mcpLeaderboardHomeLogic.ts+++ b/products/mcp_analytics/frontend/leaderboardHome/mcpLeaderboardHomeLogic.ts@@ -42,6 +42,10 @@ const TOOL_CALL_WHERE = `event = '$mcp_tool_call'const BUCKETED_ROW_LIMIT = 10000const WINDOW_ROW_LIMIT = 1000+// The users counts below use distinct_id, not person_id, for the same reason as USERS_QUERY in+// mcpDashboardOverviewLogic.ts: the person_id join over every person override runs out of memory+// on large projects.+const labelOf = (valueSql: string): string => `coalesce(nullIf(trim(${valueSql}), ''), 'Unknown')`const propertyLabel = (property: string): string => labelOf(`toString(properties.${property})`)@@ -71,7 +75,7 @@ const windowFacetQuery = (label: string, onlyErrors = false): string => `SELECT${label} AS label,count() AS calls,- uniq(person_id) AS users,+ uniq(distinct_id) AS users,countIf(toBool(properties.$mcp_is_error)) AS errorsFROM eventsWHERE ${TOOL_CALL_WHERE}@@ -84,14 +88,14 @@ LIMIT ${WINDOW_ROW_LIMIT}const MODEL_LABEL = propertyLabel('$mcp_llm_model')const labUsersQuery = `-SELECT ${labSqlExpression(MODEL_LABEL)} AS lab, uniq(person_id) AS users+SELECT ${labSqlExpression(MODEL_LABEL)} AS lab, uniq(distinct_id) AS usersFROM eventsWHERE ${TOOL_CALL_WHERE}GROUP BY lab`const namedModelUsersQuery = `-SELECT uniqIf(person_id, ${MODEL_LABEL} != 'Unknown') AS users+SELECT uniqIf(distinct_id, ${MODEL_LABEL} != 'Unknown') AS usersFROM eventsWHERE ${TOOL_CALL_WHERE}`diff --git a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.tsindex 56bb3d274bfa..d013c01d9d49 100644--- a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts+++ b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts@@ -63,13 +63,16 @@ ORDER BY bucket// Distinct MCP users for the "Users" tile — how many distinct people made tool calls.// Counted over the doubled window like the KPI query, then split into the selected period// and its equal-length predecessor with a single conditional uniq so the comparison is a-// true distinct-person count (summing per-bucket distinct users would over-count anyone+// true distinct-user count (summing per-bucket distinct users would over-count anyone// active on more than one day). `__CUR_START__` is the selected-period boundary, injected// as a timezone-aware toDateTime at call time.+// Users are counted by distinct_id, not person_id. HogQL resolves person_id through a join+// over every person override of the project, and on large projects that join runs out of+// memory. A person with several distinct_ids counts once per distinct_id.const USERS_QUERY = `SELECT- uniqIf(person_id, timestamp >= __CUR_START__) AS current_users,- uniqIf(person_id, timestamp < __CUR_START__) AS prior_users+ uniqIf(distinct_id, timestamp >= __CUR_START__) AS current_users,+ uniqIf(distinct_id, timestamp < __CUR_START__) AS prior_usersFROM eventsWHERE event = '$mcp_tool_call'AND properties.$mcp_tool_name IS NOT NULL