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(insights): rank and gate chart alternatives by breakdown (#106493)

Sam Penningtonb85269304ca4
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.test.ts b/frontend/src/scenes/insights/insightVizDataLogic.test.ts
index 72a26b5817d2..e39ac4b83252 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.test.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.test.ts
@@ -984,6 +984,20 @@ describe('insightVizDataLogic', () => {
}).toMatchValues({ isSingleSeriesOutput: true })
})
+ it.each([
+ ['a single breakdown', { breakdown: '$browser', breakdown_type: 'event' }, undefined],
+ ['multiple breakdowns', { breakdowns: [{ property: '$browser', type: 'event' }] }, undefined],
+ ['a breakdown and one formula', { breakdowns: [{ property: '$browser', type: 'event' }] }, 'A * 2'],
+ ])('returns false for a single series with %s', (_, breakdownFilter, formula) => {
+ expectLogic(builtInsightVizDataLogic, () => {
+ builtInsightVizDataLogic.actions.updateQuerySource({
+ series: [{ kind: NodeKind.EventsNode, name: '$pageview', event: '$pageview' }],
+ breakdownFilter,
+ trendsFilter: formula ? { formula } : undefined,
+ } as Partial<TrendsQuery>)
+ }).toMatchValues({ isSingleSeriesOutput: false })
+ })
+
it('returns false for multiple series without formula', () => {
expectLogic(builtInsightVizDataLogic, () => {
builtInsightVizDataLogic.actions.updateQuerySource({
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.ts b/frontend/src/scenes/insights/insightVizDataLogic.ts
index fe058842d447..9946c725e62c 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.ts
@@ -108,6 +108,7 @@ import {
nodeKindToFilterProperty,
supportsBarValueStacking,
supportsPercentStackView,
+ hasBreakdownFilter,
} from '~/queries/utils'
import {
BaseMathType,
@@ -2250,7 +2251,10 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
(formula && !formulas) ||
(formulas && formulas.length === 1) ||
(formulaNodes && formulaNodes.length === 1)
- return (isTrends && hasSingleFormula) || ((series || []).length <= 1 && !breakdownFilter?.breakdown)
+ return (
+ !hasBreakdownFilter(breakdownFilter) &&
+ ((isTrends && hasSingleFormula) || (series || []).length <= 1)
+ )
},
],
isBreakdownSeries: [
diff --git a/playwright/e2e/product-analytics/insights/trends.spec.ts b/playwright/e2e/product-analytics/insights/trends.spec.ts
index 9317aed80ee7..5587c4a9754f 100644
--- a/playwright/e2e/product-analytics/insights/trends.spec.ts
+++ b/playwright/e2e/product-analytics/insights/trends.spec.ts
@@ -237,7 +237,9 @@ test.describe('Trends insights', () => {
await expect(firefoxRow).toContainText(customEventsWithBreakdown.expected.firefoxAmountSum)
})
- await test.step('switch to Number chart and verify bold number shows net sum of 35', async () => {
+ await test.step('remove the breakdown, switch to Number chart and verify bold number shows net sum of 35', async () => {
+ await insight.trends.removeBreakdown()
+ await insight.trends.waitForChart()
await insight.trends.selectChartType(/^Number/)
await expect(insight.trends.boldNumber).toContainText(customEventsWithBreakdown.expected.amountSum)
})
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
index bf94e9c5c062..85cbbb0cbf47 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
+++ b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
@@ -90,12 +90,7 @@ describe('ChartAlternatives', () => {
}
it('applies the display rewrite when a chart is selected and closes the gallery', () => {
- setQuery(
- makeTrendsQuery({
- breakdownFilter: { breakdown: 'browser', breakdown_type: 'event' },
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, formula: 'A / B' },
- })
- )
+ setQuery(makeTrendsQuery({ trendsFilter: { display: ChartDisplayType.ActionsLineGraph, formula: 'A / B' } }))
const logic = alternativesLogic()
logic.actions.openGallery()
expect(logic.values.galleryOpen).toBe(true)
@@ -107,7 +102,6 @@ describe('ChartAlternatives', () => {
formula: undefined,
formulaNodes: [],
})
- expect(currentTrendsQuery().breakdownFilter).toBeUndefined()
expect(logic.values.galleryOpen).toBe(false)
})
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.test.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.test.ts
index 7345c58aff06..d841d5a99073 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.test.ts
+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.test.ts
@@ -33,6 +33,18 @@ describe('getChartDisplayOptions', () => {
ChartDisplayType.BoldNumber,
'This type currently only supports insights with one series, and this insight has multiple series.',
],
+ [
+ 'a breakdown',
+ { ...RENDERS_EVERYTHING, breakdowns: [{ property: '$browser' }] },
+ ChartDisplayType.BoldNumber,
+ "This type doesn't support breakdowns.",
+ ],
+ [
+ 'a breakdown on the box plot',
+ { ...RENDERS_EVERYTHING, breakdown: '$browser' },
+ ChartDisplayType.BoxPlot,
+ "This type doesn't support breakdowns.",
+ ],
[
'box plot without a numeric property',
{ ...RENDERS_EVERYTHING, boxPlotMissingProperty: true },
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.ts
index 2ef73e1de449..28b64ac67c7a 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.ts
+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartDisplayOptions.ts
@@ -101,8 +101,10 @@ export function getChartDisplayOptions({
const singleSeriesOnlyDisabledReason = !hasSingleSeriesOutput
? 'This type currently only supports insights with one series, and this insight has multiple series.'
: undefined
+ const breakdownDisabledReason = breakdownProps.length > 0 ? "This type doesn't support breakdowns." : undefined
const boxPlotDisabledReason =
trendsOnlyDisabledReason ||
+ breakdownDisabledReason ||
(boxPlotMissingProperty ? 'Select a numeric property to use a box plot.' : undefined)
return [
@@ -169,7 +171,8 @@ export function getChartDisplayOptions({
icon: 'number',
label: 'Number',
description: 'A big number showing the total value.',
- disabledReason: trendsOnlyDisabledReason || singleSeriesOnlyDisabledReason,
+ disabledReason:
+ trendsOnlyDisabledReason || breakdownDisabledReason || singleSeriesOnlyDisabledReason,
},
...(hasMetricInsight
? [
@@ -178,7 +181,8 @@ export function getChartDisplayOptions({
icon: 'metric' as const,
label: 'Metric',
description: 'A headline value with a sparkline and period-over-period change.',
- disabledReason: trendsOnlyDisabledReason || singleSeriesOnlyDisabledReason,
+ disabledReason:
+ trendsOnlyDisabledReason || breakdownDisabledReason || singleSeriesOnlyDisabledReason,
},
]
: []),
@@ -233,7 +237,8 @@ export function getChartDisplayOptions({
icon: 'calendarHeatmap',
label: 'Calendar heatmap',
description: 'Values per day and hour.',
- disabledReason: trendsOnlyDisabledReason || singleSeriesOnlyDisabledReason,
+ disabledReason:
+ trendsOnlyDisabledReason || breakdownDisabledReason || singleSeriesOnlyDisabledReason,
},
],
},
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.test.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.test.ts
index fa65c13bbb20..9140d288a452 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.test.ts
+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.test.ts
@@ -42,11 +42,24 @@ describe('getChartAlternatives', () => {
],
},
{
- name: 'puts the world map first for a country breakdown, skips breakdown-dropping types and demotes vertical bars',
+ name: 'puts the world map first for a country breakdown, then the parts-of-a-whole charts',
query: makeTrendsQuery({
breakdownFilter: { breakdowns: [{ property: '$geoip_country_code', type: 'event' }] },
}),
- expected: [ChartDisplayType.WorldMap, ChartDisplayType.ActionsAreaGraph, ChartDisplayType.ActionsBarValue],
+ expected: [ChartDisplayType.WorldMap, ChartDisplayType.ActionsBar, ChartDisplayType.ActionsPie],
+ },
+ {
+ name: 'prefers stacked bars and proportions for a breakdown, skipping types that drop it',
+ query: makeTrendsQuery({ breakdownFilter: { breakdowns: [{ property: '$browser', type: 'event' }] } }),
+ expected: [ChartDisplayType.ActionsBar, ChartDisplayType.ActionsPie, ChartDisplayType.ActionsDonut],
+ },
+ {
+ name: 'demotes lines and side-by-side bars for a breakdown even from a stacked bar chart',
+ query: makeTrendsQuery({
+ trendsFilter: { display: ChartDisplayType.ActionsBar },
+ breakdownFilter: { breakdowns: [{ property: '$browser', type: 'event' }] },
+ }),
+ expected: [ChartDisplayType.ActionsPie, ChartDisplayType.ActionsDonut, ChartDisplayType.ActionsBarValue],
},
{
name: 'puts the world map first for a country filter',
@@ -84,10 +97,44 @@ describe('getChartAlternatives', () => {
expected: [ChartDisplayType.BoxPlot, ChartDisplayType.Metric, ChartDisplayType.ActionsUnstackedBar],
},
{
- name: 'prefers other total value charts when viewing a pie chart',
- query: makeTrendsQuery({ trendsFilter: { display: ChartDisplayType.ActionsPie } }),
+ name: 'prefers other total value charts when viewing a pie chart of several series',
+ query: makeTrendsQuery({
+ series: [
+ { kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.TotalCount },
+ { kind: NodeKind.EventsNode, event: '$autocapture', math: BaseMathType.TotalCount },
+ ],
+ trendsFilter: { display: ChartDisplayType.ActionsPie },
+ }),
expected: [ChartDisplayType.ActionsDonut, ChartDisplayType.ActionsBarValue, ChartDisplayType.Metric],
},
+ {
+ name: 'does not suggest proportion charts for a pie chart of one series',
+ query: makeTrendsQuery({ trendsFilter: { display: ChartDisplayType.ActionsPie } }),
+ expected: [
+ ChartDisplayType.Metric,
+ ChartDisplayType.ActionsUnstackedBar,
+ ChartDisplayType.ActionsLineGraph,
+ ],
+ },
+ {
+ name: 'does not suggest proportion charts for a pie chart of one rendered formula',
+ query: makeTrendsQuery({
+ series: [
+ { kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.TotalCount },
+ { kind: NodeKind.EventsNode, event: '$autocapture', math: BaseMathType.TotalCount },
+ ],
+ trendsFilter: {
+ display: ChartDisplayType.ActionsPie,
+ formulas: ['A', 'B'],
+ formulaNodes: [{ formula: 'A / B' }],
+ },
+ }),
+ expected: [
+ ChartDisplayType.Metric,
+ ChartDisplayType.ActionsUnstackedBar,
+ ChartDisplayType.ActionsLineGraph,
+ ],
+ },
])('$name', ({ query, expected }) => {
expect(getChartAlternatives(compatibleOptions, query).map((option) => option.display)).toEqual(expected)
})
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.ts
index be402c8bc9f8..ac886d731b0d 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.ts
+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartRecommendations.ts
@@ -7,7 +7,7 @@ import { hasBreakdownFilter } from '~/queries/utils'
import { ChartDisplayCategory, ChartDisplayType, PropertyMathType } from '~/types'
import type { AnyPropertyFilter } from '~/types'
-import { breakdownProperties, isCountryProperty } from './chartDisplayOptions'
+import { breakdownProperties, hasTrendsFormula, isCountryProperty } from './chartDisplayOptions'
import type { ChartDisplayOption, ChartDisplayOptionGroup } from './chartDisplayOptions'
const DEFAULT_RECOMMENDATION_ORDER = [
@@ -25,8 +25,16 @@ const DEFAULT_RECOMMENDATION_ORDER = [
ChartDisplayType.WorldMap,
]
-// Vertical bars get crowded once every bucket holds one bar per breakdown value.
-const BREAKDOWN_DEMOTED_DISPLAYS = [ChartDisplayType.ActionsUnstackedBar, ChartDisplayType.ActionsBar]
+const BREAKDOWN_BOOSTED_DISPLAYS = [
+ ChartDisplayType.ActionsBar,
+ ChartDisplayType.ActionsPie,
+ ChartDisplayType.ActionsDonut,
+]
+const BREAKDOWN_DEMOTED_DISPLAYS = [
+ ChartDisplayType.ActionsLineGraph,
+ ChartDisplayType.ActionsAreaGraph,
+ ChartDisplayType.ActionsUnstackedBar,
+]
function isSingleCountryBreakdown(breakdownFilter?: BreakdownFilter | null): boolean {
const properties = breakdownProperties(breakdownFilter)
@@ -51,9 +59,21 @@ function isStatistical(query: TrendsQuery): boolean {
)
}
+function hasMultipleParts(query: TrendsQuery): boolean {
+ if (hasBreakdownFilter(query.breakdownFilter)) {
+ return true
+ }
+ const trendsFilter = query.trendsFilter
+ if (hasTrendsFormula(trendsFilter)) {
+ return (trendsFilter?.formulaNodes?.length || trendsFilter?.formulas?.length || 1) > 1
+ }
+ return (query.series?.length ?? 0) > 1
+}
+
function rankRecommendations(query: TrendsQuery | null, currentDisplay: ChartDisplayType): ChartDisplayType[] {
const boosted: ChartDisplayType[] = []
- const demoted = query && hasBreakdownFilter(query.breakdownFilter) ? BREAKDOWN_DEMOTED_DISPLAYS : []
+ const hasBreakdown = !!query && hasBreakdownFilter(query.breakdownFilter)
+ const demoted = hasBreakdown ? BREAKDOWN_DEMOTED_DISPLAYS : []
if (query) {
if (hasCountryContext(query)) {
boosted.push(ChartDisplayType.WorldMap)
@@ -61,9 +81,15 @@ function rankRecommendations(query: TrendsQuery | null, currentDisplay: ChartDis
if (isStatistical(query)) {
boosted.push(ChartDisplayType.BoxPlot)
}
- }
- if (DISPLAY_TYPES_TO_CATEGORIES[currentDisplay] === ChartDisplayCategory.TotalValue) {
- boosted.push(...PIE_DISPLAY_TYPES, ChartDisplayType.ActionsBarValue)
+ if (hasBreakdown) {
+ boosted.push(...BREAKDOWN_BOOSTED_DISPLAYS)
+ }
+ if (
+ DISPLAY_TYPES_TO_CATEGORIES[currentDisplay] === ChartDisplayCategory.TotalValue &&
+ hasMultipleParts(query)
+ ) {
+ boosted.push(...PIE_DISPLAY_TYPES, ChartDisplayType.ActionsBarValue)
+ }
}
const ranked = [...new Set([...boosted, ...DEFAULT_RECOMMENDATION_ORDER])]
return [...ranked.filter((display) => !demoted.includes(display)), ...demoted]