: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: Show correct breakdown values on tooltips with formula mode (#61541)
Luke Belton1cf29b411c15
diff --git a/frontend/src/test/insight-testing/mocks.ts b/frontend/src/test/insight-testing/mocks.tsindex 1564f6fc6b94..64c8ee9380ec 100644--- a/frontend/src/test/insight-testing/mocks.ts+++ b/frontend/src/test/insight-testing/mocks.ts@@ -27,9 +27,19 @@ export interface QueryBody {breakdowns?: Array<{ property?: string }>breakdown?: string}+ trendsFilter?: {+ formula?: string+ formulas?: string[]+ formulaNodes?: unknown[]+ }[key: string]: unknown}+function hasFormula(query: QueryBody): boolean {+ const tf = query.trendsFilter+ return !!(tf?.formula || tf?.formulas?.length || tf?.formulaNodes?.length)+}+interface FunnelsQueryResponseLike {results: FunnelStepData[]}@@ -66,27 +76,35 @@ export function buildActorsResponse(} as ActorsQueryResponse}-function buildTrendsResponse(series: SeriesData[]): TrendsQueryResponse {+// `isFormula` only models the single-formula shape: the real runner combines series per+// formula (`action: null`, `order` = formula index). This mock doesn't combine series, so+// it stamps `order: 0` on every row, which works for a single formula. A future multi-formula+// test would need this to derive the formula index per row (0 for A, 1 for B, …).+function buildTrendsResponse(series: SeriesData[], opts: { isFormula?: boolean } = {}): TrendsQueryResponse {return {- results: series.map((s, i) => ({- action: {- id: `$${s.label.toLowerCase().replace(/\s+/g, '_')}`,- type: 'events',- name: s.label,- // Breakdown rows of a single series share the series' order (as the real query- // runner does); only distinct series get distinct orders.- order: s.compare || s.breakdown_value != null ? 0 : i,- },- label: s.label,- count: s.data.reduce((a, b) => a + b, 0),- aggregated_value: s.data.reduce((a, b) => a + b, 0),- data: s.data,- labels: s.labels ?? s.data.map((_, j) => `Day ${j + 1}`),- days: s.days ?? s.data.map((_, j) => `2024-01-0${j + 1}`),- breakdown_value: s.breakdown_value,- compare: s.compare,- compare_label: s.compare_label,- })),+ results: series.map((s, i) => {+ const seriesOrder = s.compare || s.breakdown_value != null ? 0 : i+ return {+ action: opts.isFormula+ ? null+ : {+ id: `$${s.label.toLowerCase().replace(/\s+/g, '_')}`,+ type: 'events',+ name: s.label,+ order: seriesOrder,+ },+ order: opts.isFormula ? 0 : seriesOrder,+ label: s.label,+ count: s.data.reduce((a, b) => a + b, 0),+ aggregated_value: s.data.reduce((a, b) => a + b, 0),+ data: s.data,+ labels: s.labels ?? s.data.map((_, j) => `Day ${j + 1}`),+ days: s.days ?? s.data.map((_, j) => `2024-01-0${j + 1}`),+ breakdown_value: s.breakdown_value,+ compare: s.compare,+ compare_label: s.compare_label,+ }+ }),} as TrendsQueryResponse}@@ -217,7 +235,7 @@ export function setupInsightMocks({const defaults: MockResponse[] = [{match: (query) => query.kind === NodeKind.TrendsQuery,- response: (query) => buildTrendsResponse(resolveSeriesData(query)),+ response: (query) => buildTrendsResponse(resolveSeriesData(query), { isFormula: hasFormula(query) }),},{match: (query) => query.kind === NodeKind.StickinessQuery,diff --git a/frontend/src/types.ts b/frontend/src/types.tsindex af97dfd5879a..02719242881d 100644--- a/frontend/src/types.ts+++ b/frontend/src/types.ts@@ -3260,6 +3260,10 @@ export interface TrendResult {persons_urls?: { url: string }[]persons?: Personfilter?: TrendsFilterType+ /** Series order set by the query runner: the action order for plain series, or the+ * formula index for formula results (uniform across a formula's breakdown rows).+ * Unlike `action.order`, this is always populated — formula results carry `action: null`. */+ order?: number}interface Person {diff --git a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsxindex 2ec12fdcfe68..4aad1a4879ee 100644--- a/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsLineChart/TrendsLineChart.test.tsx@@ -116,6 +116,24 @@ describe('TrendsLineChart', () => {expect(tooltip.row('Spike')).toContain('3')})+ it('shows every breakdown value when a formula is applied', async () => {+ renderInsight({+ query: buildTrendsQuery({+ series: [{ kind: NodeKind.EventsNode, event: 'Napped', name: 'Napped' }],+ breakdownFilter: { breakdown: 'hedgehog', breakdown_type: 'event' },+ trendsFilter: { formula: 'A' },+ }),+ featureFlags: HOG_CHARTS_FLAG,+ })++ await chart.clickAtIndex(2)++ const tooltip = createInsightTooltipAccessor(chart.getTooltip()!)+ expect(tooltip.row('Spike')).toContain('3')+ expect(tooltip.row('Bramble')).toContain('1')+ expect(tooltip.row('Prickles')).toContain('1')+ })+it('shows current and previous period rows in compare mode', async () => {renderInsight({query: buildTrendsQuery({diff --git a/products/product_analytics/frontend/insights/trends/shared/trendsSeriesMeta.ts b/products/product_analytics/frontend/insights/trends/shared/trendsSeriesMeta.tsindex 149bca2711bc..b45abdd1e6fd 100644--- a/products/product_analytics/frontend/insights/trends/shared/trendsSeriesMeta.ts+++ b/products/product_analytics/frontend/insights/trends/shared/trendsSeriesMeta.ts@@ -23,7 +23,7 @@ export const buildTrendsSeriesMeta = (r: IndexedTrendResult): TrendsSeriesMeta =breakdown_value: r.breakdown_value,compare_label: r.compare_label,days: r.days,- order: r.action?.order ?? r.id,+ order: r.order ?? r.action?.order ?? 0,filter: r.filter,})