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(mcp-analytics): stop shifting dashboard buckets by the project offset (#74244)

Georgis Andonis55632db30b4c
diff --git a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.test.ts b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.test.ts
index 8618bfa9ed9d..60de24c1f14a 100644
--- a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.test.ts
+++ b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.test.ts
@@ -453,6 +453,22 @@ describe('mcpDashboardOverviewLogic', () => {
})
})
+ // A bare dateTrunc returns a typed DateTime that the query API stamps with the project's UTC
+ // offset, which the client reads back as an instant and converts, shifting the bucket away
+ // from the wall-clock keys it joins and compares against (an empty activity chart and a
+ // skewed KPI split on any non-UTC project). Pins the toString on all three bucketed queries.
+ it('renders every bucketed query with a stringified dateTrunc', async () => {
+ const logic = mcpDashboardOverviewLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ const bucketed = mockApi.query.mock.calls
+ .map((call) => (call[0] as any).query)
+ .filter((query: string | undefined): query is string => !!query?.includes('dateTrunc('))
+ expect(bucketed).toHaveLength(3)
+ expect(bucketed.filter((query) => !query.includes('toString(dateTrunc('))).toEqual([])
+ })
+
it('reloads every tile when the date filter changes', async () => {
const logic = mcpDashboardOverviewLogic()
logic.mount()
diff --git a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts
index 625d4e5de072..45e6c0e4a201 100644
--- a/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts
+++ b/products/mcp_analytics/frontend/mcpDashboardOverviewLogic.ts
@@ -141,6 +141,14 @@ ORDER BY day
LIMIT 10000
`
+// The `__BUCKET__` expression every bucketed query above is built with. toString() is load-bearing:
+// a bare dateTrunc returns a typed DateTime that the query API serializes with the project's UTC
+// offset attached (2026-07-21T00:00:00-07:00), which the client then reads back as an instant and
+// converts, shifting every bucket away from the wall-clock keys it joins and compares against.
+// Rendering it server-side leaves a plain string with nothing left to reinterpret, matching the
+// backend runners (tool_quality_tables.py, tool_tables.py).
+const bucketExpr = (interval: IntervalType): string => `toString(dateTrunc('${interval}', timestamp))`
+
export interface BucketRow {
bucket: string
sessions: number
@@ -690,7 +698,7 @@ export const mcpDashboardOverviewLogic = kea<mcpDashboardOverviewLogicType>([
const kpiWindow = buildKpiWindow(values.dateFilter, values.timezone, interval)
const response = (await api.query({
kind: NodeKind.HogQLQuery,
- query: KPI_QUERY.replace('__BUCKET__', `dateTrunc('${interval}', timestamp)`),
+ query: KPI_QUERY.replace('__BUCKET__', bucketExpr(interval)),
filters: kpiWindowFilters(values.queryFilters, kpiWindow),
})) as HogQLQueryResponse
breakpoint()
@@ -806,7 +814,7 @@ export const mcpDashboardOverviewLogic = kea<mcpDashboardOverviewLogicType>([
loadActivityRows: async (_: void, breakpoint): Promise<ActivityRow[]> => {
const response = (await api.query({
kind: NodeKind.HogQLQuery,
- query: ACTIVITY_QUERY.replace('__BUCKET__', `dateTrunc('${values.interval}', timestamp)`),
+ query: ACTIVITY_QUERY.replace('__BUCKET__', bucketExpr(values.interval)),
filters: values.queryFilters,
})) as HogQLQueryResponse
breakpoint()
@@ -825,7 +833,7 @@ export const mcpDashboardOverviewLogic = kea<mcpDashboardOverviewLogicType>([
loadToolDailyRows: async (_: void, breakpoint): Promise<ToolDailyRow[]> => {
const response = (await api.query({
kind: NodeKind.HogQLQuery,
- query: TOOL_DAILY_QUERY.replace('__BUCKET__', `dateTrunc('${values.interval}', timestamp)`),
+ query: TOOL_DAILY_QUERY.replace('__BUCKET__', bucketExpr(values.interval)),
filters: values.queryFilters,
})) as HogQLQueryResponse
breakpoint()
diff --git a/products/mcp_analytics/frontend/timeBuckets.test.ts b/products/mcp_analytics/frontend/timeBuckets.test.ts
index 92a96a44051a..09995684ce36 100644
--- a/products/mcp_analytics/frontend/timeBuckets.test.ts
+++ b/products/mcp_analytics/frontend/timeBuckets.test.ts
@@ -4,14 +4,15 @@ import { buildBucketKeys, formatBucketLabel, normalizeBucket } from './timeBucke
describe('timeBuckets', () => {
describe('normalizeBucket', () => {
- // Guards the flat-zero-sparkline bug: whether the query serializes the bucket as a naive
- // datetime or a Z-stamped ISO, its wall-clock digits must survive verbatim — even when the
- // browser sits in a different timezone than the project — or it matches no key.
+ // Guards the flat-zero-sparkline bug: however the query serializes the bucket, its
+ // wall-clock digits must survive verbatim, even when the browser sits in a different
+ // timezone than the project, or it matches no key.
it.each([
['2026-06-18 00:00:00', '2026-06-18 00:00:00'], // naive (toString(dateTrunc))
['2026-06-19T00:00:00Z', '2026-06-19 00:00:00'], // Z-stamped ISO (raw DateTime column)
['2026-06-19T00:00:00+00:00', '2026-06-19 00:00:00'],
['2026-06-19T11:30:00Z', '2026-06-19 11:30:00'],
+ ['2026-06-19T00:00:00-07:00', '2026-06-19 00:00:00'], // offset-stamped (typed DateTime, non-UTC project)
])('keeps %s as %s under a non-UTC browser tz', (raw, expected) => {
dayjs.tz.setDefault('Europe/Athens')
try {
diff --git a/products/mcp_analytics/frontend/timeBuckets.ts b/products/mcp_analytics/frontend/timeBuckets.ts
index 052ae91ff61b..2ef50387bbb4 100644
--- a/products/mcp_analytics/frontend/timeBuckets.ts
+++ b/products/mcp_analytics/frontend/timeBuckets.ts
@@ -69,14 +69,16 @@ export function buildBucketKeys(
}
// Normalize a raw bucket string from a query (a date or datetime) to BUCKET_FORMAT so it joins the
-// generated keys regardless of how ClickHouse rendered it. The value already carries the project-tz
-// wall clock — either a naive datetime (toString(dateTrunc)) or a Z-stamped ISO (a raw DateTime
-// column) — so read it in UTC to keep those digits verbatim. dayjs.tz(s, tz) would treat a Z-stamped
-// value as an instant and convert it by the project offset, shifting buckets off the axis so nothing
-// matches (flat charts for non-UTC projects). buildBucketKeys formats keys as the same wall clock.
+// generated keys regardless of how ClickHouse rendered it. The value always carries the project-tz
+// wall clock, in one of three shapes: naive (toString(dateTrunc)), Z-stamped (a raw DateTime
+// column), or offset-stamped like 2026-07-21T00:00:00-07:00 (a typed DateTime column in a non-UTC
+// project). Strip the zone designator before parsing so those digits survive verbatim: any parse
+// that honors the offset re-converts a wall clock that was never an instant, shifting every bucket
+// off the axis so nothing matches, which renders as a flat or empty chart. buildBucketKeys formats
+// keys as the same wall clock.
export function normalizeBucket(raw: unknown): string {
const s = String(raw ?? '')
- return s ? dayjs.utc(s).format(BUCKET_FORMAT) : ''
+ return s ? dayjs.utc(s.replace(/(?:Z|[+-]\d{2}:?\d{2})$/, '')).format(BUCKET_FORMAT) : ''
}
// Human-readable axis/hover label for a bucket, showing the time only when the interval is sub-day.