: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): use series custom name for aggregated bar axis labels (#60947)
Christiaan Hendriksen94ddf4ed87bf
diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsxindex 53c43fa828b0..adc4be37d9e7 100644--- a/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/TrendsBarChart.tsx@@ -16,7 +16,6 @@ import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormaimport { InsightEmptyState } from 'scenes/insights/EmptyStates'import { insightLogic } from 'scenes/insights/insightLogic'import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'-import { formatBreakdownLabel, getDisplayNameFromEntityFilter } from 'scenes/insights/utils'import { teamLogic } from 'scenes/teamLogic'import { openPersonsModal } from 'scenes/trends/persons-modal/PersonsModal'import { trendsDataLogic } from 'scenes/trends/trendsDataLogic'@@ -38,6 +37,7 @@ import { TrendsAlertOverlays } from '../shared/TrendsAlertOverlays'import { trendsFilterToYFormatterConfig } from '../shared/trendsAxisFormat'import { buildTrendsSeriesMeta, type TrendsSeriesMeta } from '../shared/trendsSeriesMeta'import { TrendsTooltip } from '../shared/TrendsTooltip'+import { getAggregatedDisplayLabel as getAggregatedDisplayLabelFn } from './getAggregatedDisplayLabel'import { handleTrendsBarAggregatedChartClick } from './handleTrendsBarAggregatedChartClick'import {buildTrendsBarAggregatedSeries,@@ -122,23 +122,13 @@ export function TrendsBarChart({ context, inSharedMode = false }: TrendsBarChartconst stackBreakdowns = !!querySource && !!getStackBreakdownValues(querySource)const getAggregatedDisplayLabel = useCallback(- (r: IndexedTrendResult): string => {- if (stackBreakdowns) {- // Breakdown values within the band are distinguished by color and the tooltip.- return getDisplayNameFromEntityFilter(r.action) ?? r.label ?? ''- }- if (r.breakdown_value != null) {- return formatBreakdownLabel(- r.breakdown_value,- breakdownFilter,- allCohorts?.results,- formatPropertyValueForDisplay,- undefined,- r.label- )- }- return r.label ?? ''- },+ (r: IndexedTrendResult): string =>+ getAggregatedDisplayLabelFn(r, {+ stackBreakdowns,+ breakdownFilter,+ cohorts: allCohorts?.results,+ formatPropertyValueForDisplay,+ }),[stackBreakdowns, breakdownFilter, allCohorts?.results, formatPropertyValueForDisplay])diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.test.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.test.tsnew file mode 100644index 000000000000..19e711752797--- /dev/null+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.test.ts@@ -0,0 +1,59 @@+import type { IndexedTrendResult } from 'scenes/trends/types'++import type { BreakdownFilter } from '~/queries/schema/schema-general'++import { type AggregatedDisplayLabelDeps, getAggregatedDisplayLabel } from './getAggregatedDisplayLabel'++const mkResult = (overrides: Partial<IndexedTrendResult>): IndexedTrendResult =>+ ({+ id: 0,+ label: '',+ count: 0,+ data: [],+ days: [],+ labels: [],+ aggregated_value: 0,+ ...overrides,+ }) as IndexedTrendResult++const deps = (overrides?: Partial<AggregatedDisplayLabelDeps>): AggregatedDisplayLabelDeps => ({+ stackBreakdowns: false,+ breakdownFilter: null,+ cohorts: undefined,+ formatPropertyValueForDisplay: undefined,+ ...overrides,+})++describe('getAggregatedDisplayLabel', () => {+ it('prefers the series custom name over the event name when there is no breakdown', () => {+ const r = mkResult({+ label: 'Job Created',+ action: { id: '0', type: 'events', name: 'Job Created', custom_name: 'Articles' },+ })+ expect(getAggregatedDisplayLabel(r, deps())).toBe('Articles')+ })++ it('falls back to the event name when no custom name is set', () => {+ const r = mkResult({ label: 'Job Created', action: { id: '0', type: 'events', name: 'Job Created' } })+ expect(getAggregatedDisplayLabel(r, deps())).toBe('Job Created')+ })++ it('uses the breakdown value, not the custom name, when a breakdown is present', () => {+ const breakdownFilter: BreakdownFilter = { breakdown: '$browser', breakdown_type: 'event' }+ const r = mkResult({+ label: 'Chrome',+ breakdown_value: 'Chrome',+ action: { id: '0', type: 'events', name: 'Job Created', custom_name: 'Job Created' },+ })+ expect(getAggregatedDisplayLabel(r, deps({ breakdownFilter }))).toBe('Chrome')+ })++ it('uses the entity name for the band in stacked-breakdown mode', () => {+ const r = mkResult({+ label: 'Chrome',+ breakdown_value: 'Chrome',+ action: { id: '0', type: 'events', name: 'Job Created', custom_name: 'Articles' },+ })+ expect(getAggregatedDisplayLabel(r, deps({ stackBreakdowns: true }))).toBe('Articles')+ })+})diff --git a/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.ts b/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.tsnew file mode 100644index 000000000000..ab9f136dca6a--- /dev/null+++ b/products/product_analytics/frontend/insights/trends/TrendsBarChart/getAggregatedDisplayLabel.ts@@ -0,0 +1,35 @@+import { formatBreakdownLabel, getDisplayNameFromEntityFilter } from 'scenes/insights/utils'+import type { IndexedTrendResult } from 'scenes/trends/types'++import { FormatPropertyValueForDisplayFunction } from '~/models/propertyDefinitionsModel'+import { BreakdownFilter } from '~/queries/schema/schema-general'+import { CohortType } from '~/types'++export interface AggregatedDisplayLabelDeps {+ stackBreakdowns: boolean+ breakdownFilter: BreakdownFilter | null | undefined+ cohorts: CohortType[] | undefined+ formatPropertyValueForDisplay: FormatPropertyValueForDisplayFunction | undefined+}++/** Category-axis label for a single band of the aggregated (Bar chart - Total value) chart. */+export function getAggregatedDisplayLabel(r: IndexedTrendResult, deps: AggregatedDisplayLabelDeps): string {+ if (deps.stackBreakdowns) {+ // Breakdown values within the band are distinguished by color and the tooltip.+ return getDisplayNameFromEntityFilter(r.action) ?? r.label ?? ''+ }+ if (r.breakdown_value != null) {+ return formatBreakdownLabel(+ r.breakdown_value,+ deps.breakdownFilter,+ deps.cohorts,+ deps.formatPropertyValueForDisplay,+ undefined,+ r.label+ )+ }+ // Custom name wins over the event name, matching the legacy LineGraph y-axis. Series sharing+ // an event are differentiated only by their custom name, so falling back to the event name+ // would collapse them all onto the same label.+ return getDisplayNameFromEntityFilter(r.action) ?? r.label ?? ''+}