: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): humanize stickiness legend labels and center wrapped legend rows (#65799)
Sam Penningtondd1d1e208f85
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 06d6f2bbf01a..23bcf628a9bc 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -665,13 +665,13 @@ snapshots:components-hogcharts-legend--legend-hidden--light:hash: v1.k794b7964.c421a16355bae41c9fd5c1d1c6aa071f73152e460043018fc387b9b2a8a69435.YAeJXhL3L0kCKi5t7tzYOyNsfoE6H7iASEcvGIO7cXocomponents-hogcharts-legend--long-labels-truncate--dark:- hash: v1.k794b7964.17a87c4fa402ff5f0871d0e40adbc47f90e065c0fdcf16b6b610adb487c9be7c.IvI7NT4lT_XmM9qSg-OHGFwijNSFXgJjQO3sZA87flM+ hash: v1.k794b7964.337807c527ffaf43c0745cf7cc54c25634872a8a3deb43bdfcfa0f4326c12f53.YeeAhgM36nub3P78anneQw4BaiEGPGKmXRp1kl5usXkcomponents-hogcharts-legend--long-labels-truncate--light:- hash: v1.k794b7964.331a83b798c14594c31fbd0bb8ab30df6bfbb994460814ae66c1e2f25206c1ff.z5hnkMvwuTTNeqC4HkunO95MlM5pJd_lkGXJAkDtzis+ hash: v1.k794b7964.fcfc698e1a6fe7d69be13adc990934a48228b4039111e1fd33a5722cb5424c3f.qgDTdpri93KQ7GYjh0m2sZEcMckOC8G-Gb0IxXVwK6Ecomponents-hogcharts-legend--many-items-wraps--dark:- hash: v1.k794b7964.1b95335bf283551cdef86841be2fd53b6c652fa6f263b3da7203e1e6599d1c18.Lk6e0w_i4AQAMZulWHRcwh6J5PzTBlqcrIKEvFVFhas+ hash: v1.k794b7964.42c64bebe60554b4ae0b7f58359bda9558ac4c7b785fdf38592ac98fdaa5498e.fFCahsPVltf0Lm8eUgmzTZxXQhlm48ydNBQQMeLmkZocomponents-hogcharts-legend--many-items-wraps--light:- hash: v1.k794b7964.828b81c17c44ea01c54c37cbed719c119adba9b7ea7635aa12074b3d798ea25c.t0VXF8I2msfb7-4L1KpcZgD2WPODhEKK1sJlj9MwhCE+ hash: v1.k794b7964.07036b81af46bb3361404bd927b8bc6301902e240f58eb8da3587b8fd3864cb7.ZoNw_5-XEZ-YyDybJW05o-rPp3zQenbUlPjGvyxovuAcomponents-hogcharts-legend--vertical--dark:hash: v1.k794b7964.ccfb11e836bb82f76eef037168a9475264909305f12b175514f32e79cde6570a.redbR1FEBZ4p8hDDGgFwqT9B4f8VHOZv3RLf9gTOOIgcomponents-hogcharts-legend--vertical--light:diff --git a/packages/quill/packages/charts/src/components/Legend/ChartLegendLayout.tsx b/packages/quill/packages/charts/src/components/Legend/ChartLegendLayout.tsxindex b8747584e322..dc710e5944da 100644--- a/packages/quill/packages/charts/src/components/Legend/ChartLegendLayout.tsx+++ b/packages/quill/packages/charts/src/components/Legend/ChartLegendLayout.tsx@@ -33,7 +33,7 @@ export function ChartLegendLayout({// yet scrollable (plain center would push the top rows past the scroll origin).const legendClass = isRow? 'flex flex-col self-stretch max-w-[45%] overflow-y-auto justify-center-safe'- : 'max-h-[40%] overflow-y-auto'+ : 'self-stretch max-h-[40%] overflow-y-auto'const legendSlot = legend ? (<div className={`flex-none shrink-0 min-h-0 min-w-0 ${legendClass}`}>{legend}</div>diff --git a/packages/quill/packages/charts/src/components/Legend/Legend.tsx b/packages/quill/packages/charts/src/components/Legend/Legend.tsxindex 543cabf7c4ce..a98868e93e72 100644--- a/packages/quill/packages/charts/src/components/Legend/Legend.tsx+++ b/packages/quill/packages/charts/src/components/Legend/Legend.tsx@@ -22,12 +22,13 @@ export interface LegendProps {renderItem?: (defaultNode: React.ReactNode, item: LegendItem) => React.ReactNode}-// Position a horizontal legend via auto-margins on a fit-content box, not `justify-content`: rows keep a-// shared left edge (so a wrapped legend is a clean grid, not ragged) while the block still honors `align`.-const BLOCK_ALIGN_CLASS = {- start: 'mr-auto',- center: 'mx-auto',- end: 'ml-auto',+// Align a horizontal legend with `justify-content` so wrapped rows stay centered (or start/end) within the+// full-width slot. A fit-content + auto-margin box can't center once it wraps — `width: fit-content` on a+// wrapping flex container collapses to the slot width, leaving the rows pinned to the start edge.+const JUSTIFY_CLASS = {+ start: 'justify-start',+ center: 'justify-center',+ end: 'justify-end',} as constexport function Legend({@@ -44,11 +45,11 @@ export function Legend({return null}const hidden = hiddenKeys?.length ? new Set(hiddenKeys) : null- // Stack from the start edge (justify-start) so the legend scrolls cleanly when it overflows its slot;- // centering would push the leading rows past the scroll origin. Horizontal adds the fit-content box.+ // A vertical legend stacks from the start edge (justify-start) so it scrolls cleanly when it overflows+ // its slot — vertical packing is `align-content`, untouched here. Horizontal aligns via `justify-content`.const layout =orientation === 'horizontal'- ? `flex-row flex-wrap gap-x-3 gap-y-1 justify-start w-fit max-w-full ${BLOCK_ALIGN_CLASS[align]}`+ ? `flex-row flex-wrap gap-x-3 gap-y-1 ${JUSTIFY_CLASS[align]}`: 'flex-col gap-1 justify-start'return (<div className={`flex ${layout} ${className ?? ''}`} data-attr={dataAttr}>diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.test.tsx b/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.test.tsxindex 3e85217f3ec7..456f6dfb071e 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.test.tsx+++ b/products/product_analytics/frontend/insights/stickiness/StickinessBarChart/StickinessBarChart.test.tsx@@ -4,6 +4,8 @@ import { cleanup, configure, screen, waitFor } from '@testing-library/react'import { setupJsdom, setupSyncRaf } from '@posthog/quill-charts/testing'+import { FEATURE_FLAGS } from 'lib/constants'+import { NodeKind } from '~/queries/schema/schema-general'import { buildStickinessQuery, chart, getHogChart, personsModal, renderInsight } from '~/test/insight-testing'import { ChartDisplayType } from '~/types'@@ -136,4 +138,31 @@ describe('StickinessBarChart', () => {expect(seriesArg.day).toBe(3)expect(personsModal.get()).not.toBeInTheDocument()})++ describe('quill in-chart legend (PRODUCT_ANALYTICS_QUILL_LEGEND on)', () => {+ const quillLegendFlag = { [FEATURE_FLAGS.PRODUCT_ANALYTICS_QUILL_LEGEND]: true }+ const twoSeriesBar = stickinessBar({+ series: [+ { kind: NodeKind.EventsNode, event: '$pageview', name: '$pageview' },+ { kind: NodeKind.EventsNode, event: 'Napped', name: 'Napped' },+ ],+ stickinessFilter: { display: ChartDisplayType.ActionsBar, showLegend: true },+ })++ const getInChartLegend = (container: HTMLElement): HTMLElement =>+ container.querySelector<HTMLElement>('[data-attr="hog-chart-timeseries-bar-legend"]')!++ it('humanizes core event names in the legend, leaving custom events as-is', async () => {+ const { container } = renderInsight({ query: twoSeriesBar, featureFlags: quillLegendFlag })++ await waitFor(() => {+ expect(screen.getByRole('img', { name: /chart with 2 data series/i })).toBeInTheDocument()+ })++ const legendEl = getInChartLegend(container)+ expect(legendEl.textContent).toContain('Pageview')+ expect(legendEl.textContent).not.toContain('$pageview')+ expect(legendEl.textContent).toContain('Napped')+ })+ })})diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.test.tsx b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.test.tsxindex dedbeb0012fb..71587152fac7 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.test.tsx+++ b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/StickinessLineChart.test.tsx@@ -4,6 +4,8 @@ import { cleanup, configure, screen, waitFor } from '@testing-library/react'import { setupJsdom, setupSyncRaf } from '@posthog/quill-charts/testing'+import { FEATURE_FLAGS } from 'lib/constants'+import { NodeKind } from '~/queries/schema/schema-general'import { buildStickinessQuery, chart, getHogChart, personsModal, renderInsight } from '~/test/insight-testing'@@ -118,4 +120,31 @@ describe('StickinessLineChart', () => {expect(personsModal.get()).not.toBeInTheDocument()})})++ describe('quill in-chart legend (PRODUCT_ANALYTICS_QUILL_LEGEND on)', () => {+ const quillLegendFlag = { [FEATURE_FLAGS.PRODUCT_ANALYTICS_QUILL_LEGEND]: true }+ const twoSeriesLine = buildStickinessQuery({+ series: [+ { kind: NodeKind.EventsNode, event: '$pageview', name: '$pageview' },+ { kind: NodeKind.EventsNode, event: 'Napped', name: 'Napped' },+ ],+ stickinessFilter: { showLegend: true },+ })++ const getInChartLegend = (container: HTMLElement): HTMLElement =>+ container.querySelector<HTMLElement>('[data-attr="hog-chart-timeseries-line-legend"]')!++ it('humanizes core event names in the legend, leaving custom events as-is', async () => {+ const { container } = renderInsight({ query: twoSeriesLine, featureFlags: quillLegendFlag })++ await waitFor(() => {+ expect(screen.getByRole('img', { name: /chart with 2 data series/i })).toBeInTheDocument()+ })++ const legendEl = getInChartLegend(container)+ expect(legendEl.textContent).toContain('Pageview')+ expect(legendEl.textContent).not.toContain('$pageview')+ expect(legendEl.textContent).toContain('Napped')+ })+ })})diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.test.ts b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.test.tsindex 11bf95d01264..e2671f4592ce 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.test.ts+++ b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.test.ts@@ -51,7 +51,7 @@ describe('stickinessChartTransforms', () => {expect(series).toMatchObject({key: '0',- label: '$pageview',+ label: 'Pageview',data: [50, 30, 15, 5],color: RED,yAxisId: DEFAULT_Y_AXIS_ID,@@ -61,6 +61,13 @@ describe('stickinessChartTransforms', () => {expect(series.visibility).toBeUndefined()})+ it('humanizes built-in event labels, leaving custom events untouched', () => {+ const core = buildStickinessMainSeries(makeResult({ label: '$pageview' }), 0, { getColor: () => RED })+ const custom = buildStickinessMainSeries(makeResult({ label: 'Napped' }), 0, { getColor: () => RED })+ expect(core.label).toBe('Pageview')+ expect(custom.label).toBe('Napped')+ })+it.each([{ compare_label: undefined, expectedColor: RED },{ compare_label: 'current' as const, expectedColor: RED },diff --git a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.ts b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.tsindex f7a30ff5da70..5cfda0009e2e 100644--- a/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.ts+++ b/products/product_analytics/frontend/insights/stickiness/StickinessLineChart/stickinessChartTransforms.ts@@ -7,6 +7,7 @@ import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUimport { ChartDisplayType } from '~/types'import { COMPARE_PREVIOUS_DIM_OPACITY, dimHexColor } from '../../trends/shared/compareDimming'+import { humanizeSeriesLabel } from '../../trends/shared/humanizeSeriesLabel'// Shape both IndexedTrendResult (kea) and StickinessResultItem (MCP) satisfy.export interface StickinessResultLike {@@ -56,7 +57,7 @@ export function buildStickinessMainSeries<R extends StickinessResultLike, M = unconst color = r.compare_label === 'previous' ? dimHexColor(baseColor, COMPARE_PREVIOUS_DIM_OPACITY) : baseColorreturn {key: String(r.id),- label: r.label ?? '',+ label: humanizeSeriesLabel(r.label),data: toPercentData(r.data, r.count),color,yAxisId,