: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(insights): show breakdown values in multi-series tooltip (#65411)
Sam Penningtonf6a8d61d53dc
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex f4fd2a42da65..4c4f4cb5640d 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -1057,9 +1057,9 @@ snapshots:components-insighttooltip--long-names--light:hash: v1.k794b7964.a3cbff5e9d93637e541ee21344030f75fc32adb6edd2a363f7553ffb8d8fc407.VzgJ4zJ-yb9ZtDGxqQQZGPlH8D7diG9ufmZiyFOKwP4components-insighttooltip--math-tags--dark:- hash: v1.k794b7964.b87b215e58ae9082942943f8c2bed491f7bfdcf5df2d275a710de44883f3a199.o-tywVFQU6WUpyZNTXjAUntyMC0I_JuzmnKAfaoGQoc+ hash: v1.k794b7964.3c78a732db4eac6e6ea9afd535245b0538dcc4564ac9c81dfedae6255fbf92ea.c5MmyDD0IP4c3rahv19WpupyN4TFbxRKEbLSX63WHSIcomponents-insighttooltip--math-tags--light:- hash: v1.k794b7964.d80c1aacf5b19ce4014ba8b4bebdaaf66482114898a40e5ca2e2ae0da1be5639.KsOoqiHIM-ow9rXvH6B62y01_DTuzzeqzojkiW0DuIA+ hash: v1.k794b7964.d1e89fa880800f96ac8e71fe9c9b543b8fb8d9218e8067526df447a9406d5aa5.6hbIqsXuDoBw7ufqPDVvTHkR95kBj6ybJeVg-2Hkf0scomponents-insighttooltip--math-tags-as-rows--dark:hash: v1.k794b7964.504670d1745189ec8c8e7ace729965b33d9a1944942f4698927f0ab28bd3bd02.HBJCzAuy852iKeKJQwoz3IXMLuhgirZN__OmGb1M5uocomponents-insighttooltip--math-tags-as-rows--light:diff --git a/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.test.tsx b/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.test.tsxnew file mode 100644index 000000000000..4bea7b270100--- /dev/null+++ b/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.test.tsx@@ -0,0 +1,89 @@+import '@testing-library/jest-dom'++import { cleanup, render } from '@testing-library/react'++import { BreakdownFilter } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'++import { InsightTooltip } from './InsightTooltip'+import { SeriesDatum } from './insightTooltipUtils'++const EN_DASH = '–'++const eventBreakdown: BreakdownFilter = { breakdown: '$event', breakdown_type: 'event_metadata' }++function renderTooltip(seriesData: SeriesDatum[], breakdownFilter: BreakdownFilter): HTMLElement {+ const { container } = render(+ <InsightTooltip+ date="2024-05-29"+ timezone="UTC"+ seriesData={seriesData}+ breakdownFilter={breakdownFilter}+ renderCount={(value) => `${value}`}+ renderSeries={(value) => value}+ />+ )+ return container+}++describe('InsightTooltip column layout', () => {+ beforeEach(() => {+ initKeaTests()+ })++ afterEach(() => {+ cleanup()+ })++ // Regression: each breakdown row holds one series with its own `order`; the single value column+ // used to be keyed off one row's `order`, blanking every other row to an en-dash.+ it('shows each breakdown value when every row holds a single distinct series', () => {+ const seriesData: SeriesDatum[] = [+ {+ id: 0,+ dataIndex: 0,+ datasetIndex: 0,+ order: 0,+ breakdown_value: '$pageview',+ label: '$pageview',+ color: '#1d4aff',+ count: 94,+ },+ {+ id: 1,+ dataIndex: 0,+ datasetIndex: 1,+ order: 2,+ breakdown_value: '$rageclick',+ label: '$rageclick',+ color: '#621da6',+ count: 5,+ },+ ]++ const container = renderTooltip(seriesData, eventBreakdown)++ expect(container.textContent).toContain('94')+ expect(container.textContent).toContain('5')+ expect(container.textContent).not.toContain(EN_DASH)+ })++ // Regression: columns were seeded from the longest row only, so an `order` absent from it+ // (here order 2, present only in the Safari row) was dropped to an en-dash.+ it('renders a column for every series order across breakdown rows', () => {+ const seriesData: SeriesDatum[] = [+ { id: 0, dataIndex: 0, datasetIndex: 0, order: 0, breakdown_value: 'Chrome', label: 'A', count: 10 },+ { id: 1, dataIndex: 0, datasetIndex: 1, order: 1, breakdown_value: 'Chrome', label: 'B', count: 20 },+ { id: 2, dataIndex: 0, datasetIndex: 2, order: 1, breakdown_value: 'Safari', label: 'B', count: 5 },+ { id: 3, dataIndex: 0, datasetIndex: 3, order: 2, breakdown_value: 'Safari', label: 'C', count: 7 },+ ]++ const container = renderTooltip(seriesData, eventBreakdown)++ // The previously-dropped value (Safari / order 2) is now rendered.+ expect(container.textContent).toContain('7')+ expect(container.textContent).toContain('10')+ expect(container.textContent).toContain('20')+ expect(container.textContent).toContain('5')+ })+})diff --git a/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.tsx b/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.tsxindex 136a98bd6b53..509ee89c9e30 100644--- a/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.tsx+++ b/frontend/src/scenes/insights/InsightTooltip/InsightTooltip.tsx@@ -183,10 +183,38 @@ export function InsightTooltip({]const numDataPoints = Math.max(...dataSource.map((ds) => ds?.seriesData?.length ?? 0))- if (numDataPoints > 0) {- const indexOfLongestSeries = dataSource.findIndex((ds) => ds?.seriesData?.length === numDataPoints)- const longestSeriesData = dataSource?.[indexOfLongestSeries !== -1 ? indexOfLongestSeries : 0].seriesData- const truncatedCols = colCutoff !== undefined ? longestSeriesData.slice(0, colCutoff) : longestSeriesData+ if (numDataPoints === 1) {+ // Each row holds one series; breaking multiple series down by event name gives them+ // distinct `order`s, so an order-keyed column would blank every non-matching row.+ columns.push({+ key: 'value',+ className: 'datum-counts-column',+ align: 'right',+ title: <span className="whitespace-nowrap">{rightTitle ?? undefined}</span>,+ render: function renderSingleSeriesValue(_, datum) {+ const seriesColumnData = datum.seriesData[0]+ return renderDatumToTableCell(+ seriesColumnData?.action?.math_property,+ seriesColumnData?.count,+ formatPropertyValueForDisplay,+ renderCount,+ seriesColumnData?.color+ )+ },+ })+ } else if (numDataPoints > 1) {+ // Key columns off the union of orders across all rows, not just the longest one, so a+ // series present in only some rows still gets a column instead of a dash.+ const seriesByOrder = new Map<number, SeriesDatum>()+ for (const ds of dataSource) {+ for (const s of ds?.seriesData ?? []) {+ if (!seriesByOrder.has(s.order)) {+ seriesByOrder.set(s.order, s)+ }+ }+ }+ const sortedColumnSeries = [...seriesByOrder.values()].sort((a, b) => a.order - b.order)+ const truncatedCols = colCutoff !== undefined ? sortedColumnSeries.slice(0, colCutoff) : sortedColumnSeriesconst dataColumns: LemonTableColumn<InvertedSeriesDatum, keyof InvertedSeriesDatum | undefined>[] = []truncatedCols.forEach((seriesColumn) => {const colIdx = seriesColumn.order@@ -197,7 +225,6 @@ export function InsightTooltip({title:(colIdx === 0 ? rightTitle : undefined) ||(!concreteTooltipTitle &&- numDataPoints > 1 &&renderSeries(<InsightLabelaction={seriesColumn.action}@@ -221,7 +248,7 @@ export function InsightTooltip({renderCount,seriesColumnData?.color)- if (onRowClick && seriesColumnData && numDataPoints > 1) {+ if (onRowClick && seriesColumnData) {return (<divclassName="cursor-pointer hover:bg-accent-highlight-secondary -mx-2 px-2 -my-1 py-1"@@ -235,12 +262,6 @@ export function InsightTooltip({},})})- dataColumns.sort((a, b) => {- const itemA = truncatedCols?.find((s) => s.order === parseInt(a.key as string))- const itemB = truncatedCols?.find((s) => s.order === parseInt(b.key as string))-- return (itemA?.order || 0) - (itemB?.order || 0)- })columns.push(...dataColumns)}