: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): sync tool-quality categories to url and fix stale date label (#70132)
Radu Raiceaa55d1017c2b2
diff --git a/products/mcp_analytics/frontend/MCPAnalyticsToolQuality.tsx b/products/mcp_analytics/frontend/MCPAnalyticsToolQuality.tsxindex f483576c3b17..66f4b017ce06 100644--- a/products/mcp_analytics/frontend/MCPAnalyticsToolQuality.tsx+++ b/products/mcp_analytics/frontend/MCPAnalyticsToolQuality.tsx@@ -16,7 +16,8 @@ import { ToolQualityCharts } from './tool-quality/ToolQualityCharts'import { ToolQualityTable } from './tool-quality/ToolQualityTable'function FilterBar(): JSX.Element {- const { availableCategories, selectedCategories, scopeShare, dateFilter } = useValues(mcpAnalyticsToolQualityLogic)+ const { availableCategories, selectedCategories, scopeShare, dateFilter, dateRangeLabel } =+ useValues(mcpAnalyticsToolQualityLogic)const { setSelectedCategories, setDateFilter } = useActions(mcpAnalyticsToolQualityLogic)const hasScope = selectedCategories.length > 0@@ -42,10 +43,10 @@ function FilterBar(): JSX.Element {/>{hasScope && sharePct !== null ? (<Tooltip- title={`${scopeShare.inScope.toLocaleString()} of ${scopeShare.total.toLocaleString()} MCP tool calls in the last 7 days were in the selected categories`}+ title={`${scopeShare.inScope.toLocaleString()} of ${scopeShare.total.toLocaleString()} MCP tool calls were in the selected categories (${dateRangeLabel})`}><div className="text-sm text-muted">- <span className="font-semibold text-default">{sharePct}%</span> of MCP usage (last 7d)+ <span className="font-semibold text-default">{sharePct}%</span> of MCP usage ({dateRangeLabel})</div></Tooltip>) : null}diff --git a/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.test.ts b/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.test.tsindex 14f312bb1722..eee8cc53b015 100644--- a/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.test.ts+++ b/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.test.ts@@ -84,7 +84,7 @@ describe('mcpAnalyticsToolQualityLogic', () => {return mockApi.query.mock.calls.slice(callIndex).map((call) => call[0] as any)}- it('reloads the tool rows and daily stats with the new date range when the date filter changes', async () => {+ it('reloads the tool rows, daily stats and category counts with the new date range when the date filter changes', async () => {const logic = mcpAnalyticsToolQualityLogic()logic.mount()await expectLogic(logic).toFinishAllListeners()@@ -95,10 +95,12 @@ describe('mcpAnalyticsToolQualityLogic', () => {}).toFinishAllListeners()const newCalls = queryCallsSince(callsBefore)- expect(newCalls.length).toBe(2) // tool rows + daily stats+ expect(newCalls.length).toBe(3) // tool rows + daily stats + category counts+ // The scope-share headline must track the same window as the rest of the tab.expect(newCalls.map((call) => call.filters.dateRange)).toEqual([{ date_from: '-30d', date_to: null },{ date_from: '-30d', date_to: null },+ { date_from: '-30d', date_to: null },])})diff --git a/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.ts b/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.tsindex 98fcddd5b4ac..900a93c90635 100644--- a/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.ts+++ b/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.ts@@ -4,7 +4,7 @@ import { actionToUrl, router, urlToAction } from 'kea-router'import api from 'lib/api'import { dayjs } from 'lib/dayjs'-import { dateStringToDayJs } from 'lib/utils/dateFilters'+import { dateFilterToText, dateStringToDayJs } from 'lib/utils/dateFilters'import { teamLogic } from 'scenes/teamLogic'import { urls } from 'scenes/urls'@@ -30,14 +30,16 @@ WHERE event = '$mcp_tool_call'ORDER BY category`-// Per-category call counts over a fixed 7-day window powering the "share of MCP+// Per-category call counts over the selected window, powering the "share of MCP// usage" headline. Rows with an empty category count toward the total but not the-// in-scope numerator, so uncategorized traffic dilutes the share as expected.-const CATEGORY_COUNTS_QUERY = hogql`+// in-scope numerator, so uncategorized traffic dilutes the share as expected. The+// date range rides in via the {filters} placeholder so the headline tracks the+// same window as the rest of the tab.+const CATEGORY_COUNTS_QUERY = `SELECT toString(properties.$mcp_tool_category) AS category, count() AS callsFROM eventsWHERE event = '$mcp_tool_call'- AND timestamp >= now() - INTERVAL 7 DAY+ AND {filters}GROUP BY category`@@ -221,8 +223,17 @@ export const mcpAnalyticsToolQualityLogic = kea<mcpAnalyticsToolQualityLogicType[] as CategoryCount[],{loadCategoryCounts: async (): Promise<CategoryCount[]> => {- const response = await hogqlQuery(CATEGORY_COUNTS_QUERY)- return ((response?.results as unknown[][]) ?? []).map((r) => ({+ const response = (await api.query({+ kind: NodeKind.HogQLQuery,+ query: CATEGORY_COUNTS_QUERY,+ filters: {+ dateRange: {+ date_from: values.dateFilter.dateFrom,+ date_to: values.dateFilter.dateTo,+ },+ },+ })) as HogQLQueryResponse+ return ((response.results as unknown[][]) ?? []).map((r) => ({category: String(r[0] ?? ''),calls: Number(r[1] ?? 0),}))@@ -341,6 +352,13 @@ ORDER BY day]: [],],+ // Human-readable label for the active window (e.g. "Last 7 days"), used by+ // the scope-share headline so it never falls out of sync with the filter.+ dateRangeLabel: [+ (s) => [s.dateFilter],+ (dateFilter: DateFilter): string =>+ dateFilterToText(dateFilter.dateFrom, dateFilter.dateTo, 'the selected range') ?? 'the selected range',+ ],scopeShare: [(s) => [s.categoryCounts, s.selectedCategories],(categoryCounts: CategoryCount[], selectedCategories: string[]): ScopeShare => {@@ -374,9 +392,12 @@ ORDER BY day}),listeners(({ actions, values }) => ({- // Both scope filters refetch the table and the charts+ // Both scope filters refetch the table and the charts; a date change also+ // refreshes the category counts so the "share of MCP usage" headline tracks+ // the same window.setDateFilter: () => {actions.reloadAll()+ actions.loadCategoryCounts()},setSelectedCategories: () => {actions.reloadAll()@@ -406,6 +427,11 @@ ORDER BY day} else {delete searchParams.tool}+ if (values.selectedCategories.length > 0) {+ searchParams.categories = values.selectedCategories+ } else {+ delete searchParams.categories+ }if (values.dateFilter.dateFrom) {searchParams.date_from = values.dateFilter.dateFrom} else {@@ -421,6 +447,7 @@ ORDER BY dayreturn {setSelectedTool: syncUrl,setDateFilter: syncUrl,+ setSelectedCategories: syncUrl,}}),@@ -430,6 +457,14 @@ ORDER BY dayif (tool !== values.selectedTool) {actions.setSelectedTool(tool)}+ const categories = Array.isArray(searchParams.categories)+ ? searchParams.categories.map(String)+ : typeof searchParams.categories === 'string' && searchParams.categories+ ? [searchParams.categories]+ : []+ if (JSON.stringify(categories) !== JSON.stringify(values.selectedCategories)) {+ actions.setSelectedCategories(categories)+ }const dateFrom =typeof searchParams.date_from === 'string' ? searchParams.date_from : values.dateFilter.dateFromconst dateTo = typeof searchParams.date_to === 'string' ? searchParams.date_to : null