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(usage-report): count all MCP analytics events per team (#79182)

Lucas Fariae0af87965082
diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
index d675781bbd37..c2637666a9d1 100644
--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr
+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
@@ -392,6 +392,25 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.25
'''
+ SELECT team_id,
+ event,
+ uniqExact(tuple(toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid))) AS count
+ FROM events PREWHERE timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ WHERE event IN ('$mcp_missing_capability',
+ '$mcp_initialize',
+ '$mcp_tools_list',
+ '$mcp_resource_read',
+ '$mcp_resources_list',
+ '$mcp_prompt_get',
+ '$mcp_prompts_list')
+ GROUP BY team_id,
+ event
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.26
+ '''
+
SELECT JSONExtractInt(log_comment, 'team_id') team_id,
count(1) cnt,
sum(read_bytes) read_bytes
@@ -405,7 +424,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.26
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.27
'''
SELECT team_id,
@@ -418,7 +437,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.27
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.28
'''
SELECT team_id,
@@ -435,7 +454,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.28
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.29
'''
SELECT team_id,
@@ -451,20 +470,6 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.29
- '''
-
- SELECT team_id,
- multiIf(replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'web', 'web', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'js', 'web_lite', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python', 'unknown') AS library,
- count(1) as total
- FROM events
- WHERE event = '$exception'
- AND timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 23:59:59'
- GROUP BY team_id,
- library
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.3
'''
@@ -503,6 +508,20 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.30
'''
+ SELECT team_id,
+ multiIf(replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'web', 'web', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'js', 'web_lite', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'node', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python', 'unknown') AS library,
+ count(1) as total
+ FROM events
+ WHERE event = '$exception'
+ AND timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ GROUP BY team_id,
+ library
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.31
+ '''
+
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
@@ -512,7 +531,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.31
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.32
'''
SELECT team_id,
@@ -524,7 +543,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.32
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.33
'''
SELECT team_id,
@@ -536,7 +555,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.33
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.34
'''
SELECT team_id,
@@ -548,7 +567,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.34
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.35
'''
SELECT team_id,
@@ -560,7 +579,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.35
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.36
'''
SELECT team_id,
@@ -572,7 +591,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.36
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.37
'''
SELECT team_id,
@@ -584,7 +603,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.37
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.38
'''
SELECT team_id,
@@ -596,7 +615,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.38
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.39
'''
SELECT team_id,
@@ -608,18 +627,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.39
- '''
-
- SELECT team_id,
- count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
- FROM events
- WHERE timestamp >= '2022-01-10 18:00:00'
- AND timestamp < '2022-01-10 20:00:00'
- AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.4
'''
@@ -661,8 +668,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 20:00:00'
- AND timestamp < '2022-01-10 22:00:00'
+ WHERE timestamp >= '2022-01-10 18:00:00'
+ AND timestamp < '2022-01-10 20:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -673,8 +680,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 22:00:00'
- AND timestamp < '2022-01-10 23:59:59'
+ WHERE timestamp >= '2022-01-10 20:00:00'
+ AND timestamp < '2022-01-10 22:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
GROUP BY team_id
'''
@@ -685,11 +692,9 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 02:00:00'
+ WHERE timestamp >= '2022-01-10 22:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
- AND person_mode IN ('full',
- 'force_upgrade')
GROUP BY team_id
'''
# ---
@@ -699,8 +704,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 02:00:00'
- AND timestamp < '2022-01-10 04:00:00'
+ WHERE timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 02:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -713,8 +718,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 04:00:00'
- AND timestamp < '2022-01-10 06:00:00'
+ WHERE timestamp >= '2022-01-10 02:00:00'
+ AND timestamp < '2022-01-10 04:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -727,8 +732,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 06:00:00'
- AND timestamp < '2022-01-10 08:00:00'
+ WHERE timestamp >= '2022-01-10 04:00:00'
+ AND timestamp < '2022-01-10 06:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -741,8 +746,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 08:00:00'
- AND timestamp < '2022-01-10 10:00:00'
+ WHERE timestamp >= '2022-01-10 06:00:00'
+ AND timestamp < '2022-01-10 08:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -755,8 +760,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 10:00:00'
- AND timestamp < '2022-01-10 12:00:00'
+ WHERE timestamp >= '2022-01-10 08:00:00'
+ AND timestamp < '2022-01-10 10:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -769,8 +774,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 12:00:00'
- AND timestamp < '2022-01-10 14:00:00'
+ WHERE timestamp >= '2022-01-10 10:00:00'
+ AND timestamp < '2022-01-10 12:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -783,8 +788,8 @@
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
- WHERE timestamp >= '2022-01-10 14:00:00'
- AND timestamp < '2022-01-10 16:00:00'
+ WHERE timestamp >= '2022-01-10 12:00:00'
+ AND timestamp < '2022-01-10 14:00:00'
AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
AND person_mode IN ('full',
'force_upgrade')
@@ -829,6 +834,20 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.50
'''
+ SELECT team_id,
+ count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
+ FROM events
+ WHERE timestamp >= '2022-01-10 14:00:00'
+ AND timestamp < '2022-01-10 16:00:00'
+ AND event NOT IN ['$feature_flag_called', '$experiment_exposure', 'survey sent', 'survey shown', 'survey dismissed', '$exception', '$ai_generation', '$ai_embedding', '$ai_span', '$ai_trace', '$ai_metric', '$ai_feedback', '$ai_evaluation', '$ai_tag', '$ai_trace_summary', '$ai_generation_summary', '$ai_trace_clusters', '$ai_generation_clusters', '$conversations_loaded', '$conversations_widget_loaded', '$conversations_message_sent', '$conversations_user_identified', '$conversations_restore_link_requested', '$conversations_widget_state_changed', '$conversations_back_to_tickets']
+ AND person_mode IN ('full',
+ 'force_upgrade')
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.51
+ '''
+
SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
@@ -840,7 +859,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.51
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.52
'''
SELECT team_id,
@@ -854,7 +873,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.52
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.53
'''
SELECT team_id,
@@ -868,7 +887,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.53
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.54
'''
SELECT team_id,
@@ -882,7 +901,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.54
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.55
'''
SELECT team_id,
@@ -898,7 +917,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.55
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.56
'''
SELECT team_id,
@@ -909,7 +928,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.56
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.57
'''
SELECT team_id,
@@ -932,7 +951,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.57
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.58
'''
SELECT team_id,
@@ -955,7 +974,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.58
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.59
'''
SELECT team_id,
@@ -979,29 +998,6 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.59
- '''
-
- SELECT team_id,
- count(distinct session_id) as count
- FROM
- (SELECT any(team_id) as team_id,
- session_id
- FROM session_replay_events
- WHERE min_first_timestamp >= '2022-01-10 00:00:00'
- AND min_first_timestamp < '2022-01-10 23:59:59'
- GROUP BY session_id
- HAVING ifNull(argMinMerge(snapshot_source), 'web') == 'mobile'
- AND max(is_deleted) = 0)
- WHERE session_id NOT IN
- (SELECT DISTINCT session_id
- FROM session_replay_events
- WHERE min_first_timestamp >= '2022-01-09 00:00:00'
- AND min_first_timestamp < '2022-01-10 00:00:00'
- GROUP BY session_id)
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.6
'''
@@ -1040,6 +1036,29 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.60
'''
+ SELECT team_id,
+ count(distinct session_id) as count
+ FROM
+ (SELECT any(team_id) as team_id,
+ session_id
+ FROM session_replay_events
+ WHERE min_first_timestamp >= '2022-01-10 00:00:00'
+ AND min_first_timestamp < '2022-01-10 23:59:59'
+ GROUP BY session_id
+ HAVING ifNull(argMinMerge(snapshot_source), 'web') == 'mobile'
+ AND max(is_deleted) = 0)
+ WHERE session_id NOT IN
+ (SELECT DISTINCT session_id
+ FROM session_replay_events
+ WHERE min_first_timestamp >= '2022-01-09 00:00:00'
+ AND min_first_timestamp < '2022-01-10 00:00:00'
+ GROUP BY session_id)
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.61
+ '''
+
SELECT team_id,
sum(total_size) as bytes
FROM
@@ -1061,7 +1080,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.61
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.62
'''
SELECT team_id,
@@ -1088,7 +1107,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.62
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.63
'''
SELECT distinct_id as team,
@@ -1102,7 +1121,7 @@
GROUP BY team
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.63
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.64
'''
SELECT distinct_id as team,
@@ -1116,7 +1135,7 @@
GROUP BY team
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.64
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.65
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1135,7 +1154,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.65
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.66
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1154,7 +1173,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.66
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.67
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1173,32 +1192,13 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.67
- '''
- WITH JSONExtractInt(log_comment, 'team_id') as team_id,
- JSONExtractString(log_comment, 'query_type') as query_type,
- JSONExtractString(log_comment, 'access_method') as access_method
- SELECT team_id,
- sum(read_bytes) as count
- FROM clusterAllReplicas(posthog, system.query_log)
- WHERE (type = 'QueryFinish'
- OR type = 'ExceptionWhileProcessing')
- AND is_initial_query = 1
- AND event_time >= 'today 00:00:00'
- AND event_time < '2022-01-11 05:59:59'
- AND query_start_time >= '2022-01-10 00:00:00'
- AND query_start_time < '2022-01-10 23:59:59'
- AND access_method = 'personal_api_key'
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.68
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(read_rows) as count
+ sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1217,7 +1217,7 @@
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
- sum(query_duration_ms) as count
+ sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
@@ -1266,6 +1266,25 @@
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.70
+ '''
+ WITH JSONExtractInt(log_comment, 'team_id') as team_id,
+ JSONExtractString(log_comment, 'query_type') as query_type,
+ JSONExtractString(log_comment, 'access_method') as access_method
+ SELECT team_id,
+ sum(query_duration_ms) as count
+ FROM clusterAllReplicas(posthog, system.query_log)
+ WHERE (type = 'QueryFinish'
+ OR type = 'ExceptionWhileProcessing')
+ AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
+ AND query_start_time >= '2022-01-10 00:00:00'
+ AND query_start_time < '2022-01-10 23:59:59'
+ AND access_method = 'personal_api_key'
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.71
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1285,7 +1304,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.71
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.72
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1305,7 +1324,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.72
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.73
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1325,7 +1344,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.73
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1345,7 +1364,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.74
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1365,7 +1384,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.75
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
@@ -1385,7 +1404,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.76
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
'''
SELECT team_id,
@@ -1409,7 +1428,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.77
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
'''
SELECT team_id,
@@ -1424,7 +1443,7 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.78
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.79
'''
SELECT team_id,
@@ -1438,19 +1457,6 @@
metric_name
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.79
- '''
-
- SELECT team_id,
- SUM(count) as count
- FROM app_metrics2
- WHERE app_source='hog_function'
- AND metric_name IN ('billable_invocation')
- AND timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 23:59:59'
- GROUP BY team_id
- '''
-# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.8
'''
@@ -1489,6 +1495,19 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.80
'''
+ SELECT team_id,
+ SUM(count) as count
+ FROM app_metrics2
+ WHERE app_source='hog_function'
+ AND metric_name IN ('billable_invocation')
+ AND timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ GROUP BY team_id
+ '''
+# ---
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.81
+ '''
+
SELECT team_id,
COUNT() - uniqExactIf(request_id, verified
AND NOT relay
@@ -1508,7 +1527,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.81
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.82
'''
SELECT team_id,
@@ -1522,7 +1541,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.82
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.83
'''
SELECT team_id,
@@ -1536,7 +1555,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.83
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.84
'''
SELECT team_id,
@@ -1550,7 +1569,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.84
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.85
'''
SELECT team_id,
@@ -1564,7 +1583,7 @@
GROUP BY team_id
'''
# ---
-# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.85
+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.86
'''
SELECT team_id,
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index c9413171b013..d89618caa6ce 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -50,6 +50,7 @@ from posthog.models.scoping import team_scope
from posthog.models.sharing_configuration import SharingConfiguration
from posthog.session_recordings.queries.test.session_replay_sql import produce_replay_summary
from posthog.tasks.usage_report import (
+ MCP_ANALYTICS_EVENT_METRICS,
OrgReport,
UsageReportCounters,
_add_team_report_to_org_reports,
@@ -58,6 +59,7 @@ from posthog.tasks.usage_report import (
_get_all_usage_data_as_team_rows,
_get_full_org_usage_report,
_get_full_org_usage_report_as_dict,
+ _get_mcp_analytics_event_metric_counts,
_get_team_report,
_get_teams_for_usage_reports,
capture_event,
@@ -667,6 +669,13 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"web_lite_events_count_in_period": 1,
"node_events_count_in_period": 1,
"mcp_tool_call_events_count_in_period": 0,
+ "mcp_missing_capability_events_count_in_period": 0,
+ "mcp_initialize_events_count_in_period": 0,
+ "mcp_tools_list_events_count_in_period": 0,
+ "mcp_resource_read_events_count_in_period": 0,
+ "mcp_resources_list_events_count_in_period": 0,
+ "mcp_prompt_get_events_count_in_period": 0,
+ "mcp_prompts_list_events_count_in_period": 0,
"openclaw_events_count_in_period": 1,
"posthog_pi_events_count_in_period": 1,
"posthog_ai_events_count_in_period": 1,
@@ -748,6 +757,13 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"web_lite_events_count_in_period": 1,
"node_events_count_in_period": 1,
"mcp_tool_call_events_count_in_period": 0,
+ "mcp_missing_capability_events_count_in_period": 0,
+ "mcp_initialize_events_count_in_period": 0,
+ "mcp_tools_list_events_count_in_period": 0,
+ "mcp_resource_read_events_count_in_period": 0,
+ "mcp_resources_list_events_count_in_period": 0,
+ "mcp_prompt_get_events_count_in_period": 0,
+ "mcp_prompts_list_events_count_in_period": 0,
"openclaw_events_count_in_period": 1,
"posthog_pi_events_count_in_period": 1,
"posthog_ai_events_count_in_period": 1,
@@ -823,6 +839,13 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"web_lite_events_count_in_period": 0,
"node_events_count_in_period": 0,
"mcp_tool_call_events_count_in_period": 0,
+ "mcp_missing_capability_events_count_in_period": 0,
+ "mcp_initialize_events_count_in_period": 0,
+ "mcp_tools_list_events_count_in_period": 0,
+ "mcp_resource_read_events_count_in_period": 0,
+ "mcp_resources_list_events_count_in_period": 0,
+ "mcp_prompt_get_events_count_in_period": 0,
+ "mcp_prompts_list_events_count_in_period": 0,
"openclaw_events_count_in_period": 0,
"posthog_pi_events_count_in_period": 0,
"posthog_ai_events_count_in_period": 0,
@@ -921,6 +944,13 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"web_lite_events_count_in_period": 0,
"node_events_count_in_period": 0,
"mcp_tool_call_events_count_in_period": 0,
+ "mcp_missing_capability_events_count_in_period": 0,
+ "mcp_initialize_events_count_in_period": 0,
+ "mcp_tools_list_events_count_in_period": 0,
+ "mcp_resource_read_events_count_in_period": 0,
+ "mcp_resources_list_events_count_in_period": 0,
+ "mcp_prompt_get_events_count_in_period": 0,
+ "mcp_prompts_list_events_count_in_period": 0,
"openclaw_events_count_in_period": 0,
"posthog_pi_events_count_in_period": 0,
"posthog_ai_events_count_in_period": 0,
@@ -1002,6 +1032,13 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
"web_lite_events_count_in_period": 0,
"node_events_count_in_period": 0,
"mcp_tool_call_events_count_in_period": 0,
+ "mcp_missing_capability_events_count_in_period": 0,
+ "mcp_initialize_events_count_in_period": 0,
+ "mcp_tools_list_events_count_in_period": 0,
+ "mcp_resource_read_events_count_in_period": 0,
+ "mcp_resources_list_events_count_in_period": 0,
+ "mcp_prompt_get_events_count_in_period": 0,
+ "mcp_prompts_list_events_count_in_period": 0,
"openclaw_events_count_in_period": 0,
"posthog_pi_events_count_in_period": 0,
"posthog_ai_events_count_in_period": 0,
@@ -1477,7 +1514,7 @@ class TestQueryUsageReportSQL:
@patch("posthog.tasks.usage_report.use_new_events_schema", return_value=False)
@patch("posthog.tasks.usage_report._execute_split_query")
- @patch("posthog.tasks.usage_report.sync_execute", return_value=[(1, 4)])
+ @patch("posthog.tasks.usage_report.sync_execute")
@patch("posthog.tasks.usage_report.get_property_string_expr")
def test_get_all_event_metrics_splits_ai_breakdown_out_of_main_scan(
self,
@@ -1493,6 +1530,13 @@ class TestQueryUsageReportSQL:
{"node_events": [(1, 10)], "openclaw_events": [], "posthog_pi_events": [], "posthog_ai_events": []},
[(1, "posthog-ai", 2), (1, "posthog-openclaw", 3)],
]
+ # Both MCP scans are calendar-aligned, so they reach sync_execute directly: 1st is
+ # `$mcp_tool_call`, 2nd is the grouped (team_id, event, count) scan over the other 7 events.
+ # A single day spans one calendar-aligned split, hence one call each.
+ mock_sync_execute.side_effect = [
+ [(1, 4)],
+ [(1, "$mcp_missing_capability", 6), (1, "$mcp_initialize", 7)],
+ ]
begin = datetime(2026, 6, 15, tzinfo=tzutc())
end = begin + timedelta(days=1)
@@ -1518,16 +1562,34 @@ class TestQueryUsageReportSQL:
assert "ai_lib_expr IN (" in ai_query
assert "'posthog-ai'" in ai_query
- mcp_query = mock_sync_execute.call_args.args[0]
+ dedup_expression = "uniqExact(tuple(toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)))"
+
+ mcp_query = mock_sync_execute.call_args_list[0].args[0]
assert "event = '$mcp_tool_call'" in mcp_query
- assert "uniqExact(tuple(toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)))" in mcp_query
+ assert dedup_expression in mcp_query
assert result["mcp_tool_call_events"] == [(1, 4)]
+ # The other 7 MCP Analytics events share one grouped query, and reuse `$mcp_tool_call`'s
+ # dedup expression and table so every MCP metric is counted the same way.
+ mcp_analytics_query = mock_sync_execute.call_args_list[1].args[0]
+ assert dedup_expression in mcp_analytics_query
+ assert "GROUP BY team_id, event" in mcp_analytics_query
+ assert "FROM events" in mcp_analytics_query
+ # `$mcp_tool_call` keeps its own untouched query; `$mcp_custom` is never emitted verbatim.
+ assert "'$mcp_tool_call'" not in mcp_analytics_query
+ assert "'$mcp_custom'" not in mcp_analytics_query
+ # One grouped query for the 7, not one query each.
+ assert mock_sync_execute.call_count == 2
+
# AI counts are folded back in and subtracted from node_events (10 - 2 - 3 = 5).
assert result["posthog_ai_events"] == [(1, 2)]
assert result["openclaw_events"] == [(1, 3)]
assert result["node_events"] == [(1, 5)]
+ # New MCP Analytics metrics are merged straight into the returned dict.
+ assert result["mcp_missing_capability_events"] == [(1, 6)]
+ assert result["mcp_initialize_events"] == [(1, 7)]
+
@patch("posthog.tasks.usage_report.events_read_table", return_value="events")
@patch("posthog.tasks.usage_report.get_property_string_expr", return_value=("property_expr", {}))
@patch("posthog.tasks.usage_report.use_new_events_schema", return_value=False)
@@ -5482,6 +5544,27 @@ class TestCalendarAlignedQuerySplitting(SimpleTestCase):
],
)
+ @patch("posthog.tasks.usage_report.sync_execute")
+ def test_mcp_analytics_counts_sum_per_metric_across_splits_and_teams(self, mock_sync_execute: MagicMock) -> None:
+ # A period spanning 3 days splits 3 ways, so a team's count for one event arrives in
+ # pieces that must be added, not overwritten, and kept separate per team and per event.
+ mock_sync_execute.side_effect = [
+ [(1, "$mcp_initialize", 2), (2, "$mcp_initialize", 5)],
+ [(1, "$mcp_initialize", 3), (1, "$mcp_tools_list", 7)],
+ [(1, "$mcp_initialize", 4), ("unexpected", "$mcp_not_a_real_event", 9)],
+ ]
+
+ result = _get_mcp_analytics_event_metric_counts(
+ begin=datetime(2023, 1, 1, 12, 0), end=datetime(2023, 1, 3, 12, 0)
+ )
+
+ self.assertEqual(sorted(result["mcp_initialize_events"]), [(1, 9), (2, 5)])
+ self.assertEqual(result["mcp_tools_list_events"], [(1, 7)])
+ # Every metric key is present even when no row mentioned it, so `_get_all_usage_data`
+ # never KeyErrors on a quiet event.
+ self.assertEqual(result["mcp_prompts_list_events"], [])
+ self.assertEqual(set(result), set(MCP_ANALYTICS_EVENT_METRICS.values()))
+
class TestQuerySplitting(ClickhouseDestroyTablesMixin, ClickhouseTestMixin, TestCase):
def setUp(self) -> None:
@@ -5816,6 +5899,33 @@ class TestQuerySplitting(ClickhouseDestroyTablesMixin, ClickhouseTestMixin, Test
self.assertEqual(dict(event_metrics["mcp_tool_call_events"]).get(self.team.id), 2)
self.assertEqual(dict(event_metrics["python_events"]).get(self.team.id), 1)
+ @parameterized.expand(
+ [
+ ("$mcp_missing_capability", "mcp_missing_capability_events"),
+ ("$mcp_initialize", "mcp_initialize_events"),
+ ("$mcp_tools_list", "mcp_tools_list_events"),
+ ("$mcp_resource_read", "mcp_resource_read_events"),
+ ("$mcp_resources_list", "mcp_resources_list_events"),
+ ("$mcp_prompt_get", "mcp_prompt_get_events"),
+ ("$mcp_prompts_list", "mcp_prompts_list_events"),
+ ]
+ )
+ def test_mcp_analytics_events_are_counted_per_team(self, event_name: str, metric_name: str) -> None:
+ _create_event(
+ event=event_name,
+ team=self.team,
+ distinct_id="mcp_analytics_user",
+ timestamp=self.begin + relativedelta(hours=1),
+ properties={"$lib": "posthog-node-mcp"},
+ )
+ flush_persons_and_events()
+
+ event_metrics = get_all_event_metrics_in_period(self.begin, self.end)
+
+ self.assertEqual(dict(event_metrics[metric_name]).get(self.team.id), 1)
+ # `$mcp_tool_call` stays untouched by every other MCP Analytics event.
+ self.assertIsNone(dict(event_metrics["mcp_tool_call_events"]).get(self.team.id))
+
def test_get_teams_with_billable_enhanced_persons_event_count_in_period(
self,
) -> None:
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index 3f0ed700de9c..4321297554fb 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -286,6 +286,13 @@ class UsageReportCounters:
# MCP usage overlaps billable event and per-SDK totals.
mcp_tool_call_events_count_in_period: int
+ mcp_missing_capability_events_count_in_period: int
+ mcp_initialize_events_count_in_period: int
+ mcp_tools_list_events_count_in_period: int
+ mcp_resource_read_events_count_in_period: int
+ mcp_resources_list_events_count_in_period: int
+ mcp_prompt_get_events_count_in_period: int
+ mcp_prompts_list_events_count_in_period: int
# SDK usage (continued)
openclaw_events_count_in_period: int
@@ -604,7 +611,15 @@ def _execute_calendar_aligned_split_query(
query_template: str,
params: dict,
num_splits: int,
-) -> list[tuple[int, int]]:
+ combine_results_func: Optional[Callable[[list], Any]] = None,
+) -> Any:
+ """Like `_execute_split_query`, but every split boundary lands on midnight.
+
+ A dedup key containing `toDate(timestamp)` only sums correctly across splits when no split
+ cuts a day in half: a duplicate row pair straddling the boundary lands one row in each split
+ and so is deduped by neither. `combine_results_func` mirrors `_execute_split_query`'s — leave
+ it unset for flat `(team_id, count)` rows, pass one when grouping by a second dimension.
+ """
total_days = (end.date() - begin.date()).days
split_boundaries = {begin, end}
@@ -628,7 +643,9 @@ def _execute_calendar_aligned_split_query(
)
)
- return _combine_team_count_results(all_results)
+ if combine_results_func is None:
+ return _combine_team_count_results(all_results)
+ return combine_results_func(all_results)
def _combine_team_count_results(results_list: list) -> list[tuple[int, int]]:
@@ -825,6 +842,71 @@ def _get_ai_sub_sdk_event_metric_counts(
}, node_subtractions
+# MCP Analytics events emitted verbatim by the @posthog/mcp SDK, beyond `$mcp_tool_call` (which
+# has its own dedicated metric/query below and must not be folded in here — see
+# `mcp_tool_call_events` in `get_all_event_metrics_in_period`). Excludes `$exception`/`$identify`
+# (shared with every SDK, would double count) and `$mcp_custom` (a registered constant that's
+# never emitted verbatim, so it would always be zero).
+MCP_ANALYTICS_EVENT_METRICS: dict[str, str] = {
+ "$mcp_missing_capability": "mcp_missing_capability_events",
+ "$mcp_initialize": "mcp_initialize_events",
+ "$mcp_tools_list": "mcp_tools_list_events",
+ "$mcp_resource_read": "mcp_resource_read_events",
+ "$mcp_resources_list": "mcp_resources_list_events",
+ "$mcp_prompt_get": "mcp_prompt_get_events",
+ "$mcp_prompts_list": "mcp_prompts_list_events",
+}
+
+
+def _get_mcp_analytics_event_metric_counts(begin: datetime, end: datetime) -> dict[str, list[tuple[int, int]]]:
+ """One grouped query for all `MCP_ANALYTICS_EVENT_METRICS`, fanned out in Python.
+
+ Mirrors `_get_ai_sub_sdk_event_metric_counts`'s "one query, group by two dims, fan out"
+ shape, but groups by `event` (not `$ai_lib`).
+
+ Reads `events` directly, matching `mcp_tool_call_events` rather than the
+ `events_read_table(use_new)` the rest of this module uses. The two must agree: every MCP
+ metric shares one dedup expression so the counts stay comparable, and that expression is the
+ legacy `events` ORDER BY key. The JSON table sorts by full-precision `timestamp` and raw
+ `distinct_id`/`uuid` instead, so the same expression does not dedup it equivalently. Moving
+ these metrics onto that table means changing how they dedup, for the whole module at once.
+ """
+ quoted_events = ", ".join(f"'{event}'" for event in MCP_ANALYTICS_EVENT_METRICS)
+ # nosemgrep: clickhouse-fstring-param-audit - event names are internal constants, not user input
+ query_template = f"""
+ SELECT
+ team_id,
+ event,
+ uniqExact(tuple(toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid))) AS count
+ FROM events
+ PREWHERE timestamp >= %(begin)s AND timestamp < %(end)s
+ WHERE event IN ({quoted_events})
+ GROUP BY team_id, event
+ """
+
+ def combine_mcp_analytics_results(results_list: list) -> dict[str, list[tuple[int, int]]]:
+ counts_by_metric: dict[str, dict[int, int]] = {
+ metric_name: {} for metric_name in MCP_ANALYTICS_EVENT_METRICS.values()
+ }
+ for results in results_list:
+ for team_id, event, count in results:
+ metric_name = MCP_ANALYTICS_EVENT_METRICS.get(event)
+ if metric_name is None:
+ continue
+ team_counts = counts_by_metric[metric_name]
+ team_counts[team_id] = team_counts.get(team_id, 0) + count
+ return {metric_name: list(team_counts.items()) for metric_name, team_counts in counts_by_metric.items()}
+
+ return _execute_calendar_aligned_split_query(
+ begin=begin,
+ end=end,
+ query_template=query_template,
+ params={},
+ num_splits=12,
+ combine_results_func=combine_mcp_analytics_results,
+ )
+
+
@timed_log()
@retry(tries=QUERY_RETRIES, delay=QUERY_RETRY_DELAY, backoff=QUERY_RETRY_BACKOFF)
def get_all_event_metrics_in_period(begin: datetime, end: datetime) -> dict[str, list[tuple[int, int]]]:
@@ -978,6 +1060,7 @@ def get_all_event_metrics_in_period(begin: datetime, end: datetime) -> dict[str,
ai_lib_expression=ai_lib_expression,
use_new_events_schema=use_new,
)
+ mcp_analytics_counts_by_metric = _get_mcp_analytics_event_metric_counts(begin=begin, end=end)
# Fold the AI sub-counts in and remove them from node_events (the main scan counts every
# posthog-node event as node_events). max(0, count) guards against tiny cross-query ingestion jitter.
@@ -985,6 +1068,7 @@ def get_all_event_metrics_in_period(begin: datetime, end: datetime) -> dict[str,
(team_id, max(0, count - node_subtractions.get(team_id, 0))) for team_id, count in metrics["node_events"]
]
metrics.update(ai_counts_by_metric)
+ metrics.update(mcp_analytics_counts_by_metric)
return metrics
@@ -2763,6 +2847,13 @@ def _get_all_usage_data(period_start: datetime, period_end: datetime) -> dict[st
"teams_with_web_lite_events_count_in_period": all_metrics["web_lite_events"],
"teams_with_node_events_count_in_period": all_metrics["node_events"],
"teams_with_mcp_tool_call_events_count_in_period": all_metrics["mcp_tool_call_events"],
+ "teams_with_mcp_missing_capability_events_count_in_period": all_metrics["mcp_missing_capability_events"],
+ "teams_with_mcp_initialize_events_count_in_period": all_metrics["mcp_initialize_events"],
+ "teams_with_mcp_tools_list_events_count_in_period": all_metrics["mcp_tools_list_events"],
+ "teams_with_mcp_resource_read_events_count_in_period": all_metrics["mcp_resource_read_events"],
+ "teams_with_mcp_resources_list_events_count_in_period": all_metrics["mcp_resources_list_events"],
+ "teams_with_mcp_prompt_get_events_count_in_period": all_metrics["mcp_prompt_get_events"],
+ "teams_with_mcp_prompts_list_events_count_in_period": all_metrics["mcp_prompts_list_events"],
"teams_with_openclaw_events_count_in_period": all_metrics["openclaw_events"],
"teams_with_posthog_pi_events_count_in_period": all_metrics["posthog_pi_events"],
"teams_with_posthog_ai_events_count_in_period": all_metrics["posthog_ai_events"],
@@ -3155,6 +3246,27 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounter
mcp_tool_call_events_count_in_period=all_data["teams_with_mcp_tool_call_events_count_in_period"].get(
team.id, 0
),
+ mcp_missing_capability_events_count_in_period=all_data[
+ "teams_with_mcp_missing_capability_events_count_in_period"
+ ].get(team.id, 0),
+ mcp_initialize_events_count_in_period=all_data["teams_with_mcp_initialize_events_count_in_period"].get(
+ team.id, 0
+ ),
+ mcp_tools_list_events_count_in_period=all_data["teams_with_mcp_tools_list_events_count_in_period"].get(
+ team.id, 0
+ ),
+ mcp_resource_read_events_count_in_period=all_data["teams_with_mcp_resource_read_events_count_in_period"].get(
+ team.id, 0
+ ),
+ mcp_resources_list_events_count_in_period=all_data["teams_with_mcp_resources_list_events_count_in_period"].get(
+ team.id, 0
+ ),
+ mcp_prompt_get_events_count_in_period=all_data["teams_with_mcp_prompt_get_events_count_in_period"].get(
+ team.id, 0
+ ),
+ mcp_prompts_list_events_count_in_period=all_data["teams_with_mcp_prompts_list_events_count_in_period"].get(
+ team.id, 0
+ ),
openclaw_events_count_in_period=all_data["teams_with_openclaw_events_count_in_period"].get(team.id, 0),
posthog_pi_events_count_in_period=all_data["teams_with_posthog_pi_events_count_in_period"].get(team.id, 0),
posthog_ai_events_count_in_period=all_data["teams_with_posthog_ai_events_count_in_period"].get(team.id, 0),
diff --git a/posthog/temporal/usage_report/queries.py b/posthog/temporal/usage_report/queries.py
index 5b9dabfaea78..bcc1efa5bbb2 100644
--- a/posthog/temporal/usage_report/queries.py
+++ b/posthog/temporal/usage_report/queries.py
@@ -279,6 +279,13 @@ QUERIES: list[QuerySpec] = [
"web_lite_events": "teams_with_web_lite_events_count_in_period",
"node_events": "teams_with_node_events_count_in_period",
"mcp_tool_call_events": "teams_with_mcp_tool_call_events_count_in_period",
+ "mcp_missing_capability_events": "teams_with_mcp_missing_capability_events_count_in_period",
+ "mcp_initialize_events": "teams_with_mcp_initialize_events_count_in_period",
+ "mcp_tools_list_events": "teams_with_mcp_tools_list_events_count_in_period",
+ "mcp_resource_read_events": "teams_with_mcp_resource_read_events_count_in_period",
+ "mcp_resources_list_events": "teams_with_mcp_resources_list_events_count_in_period",
+ "mcp_prompt_get_events": "teams_with_mcp_prompt_get_events_count_in_period",
+ "mcp_prompts_list_events": "teams_with_mcp_prompts_list_events_count_in_period",
"openclaw_events": "teams_with_openclaw_events_count_in_period",
"posthog_pi_events": "teams_with_posthog_pi_events_count_in_period",
"posthog_ai_events": "teams_with_posthog_ai_events_count_in_period",