: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
feat(insights): preview the slope graph under a breakdown (#106597)
Sam Pennington5a66b929e229
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.test.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.test.tsindex 6e9dff9cd055..f6b49067e349 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.test.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.test.ts@@ -199,15 +199,20 @@ describe('deriveChartPreview', () => {expect(results(preview)[0]).toMatchObject({ aggregated_value: 10 })})- it.each([- ['smoothed buckets', query(ChartDisplayType.ActionsLineGraph, { trendsFilter: { smoothingIntervals: 2 } })],- [- 'breakdown buckets',- query(ChartDisplayType.ActionsLineGraph, {- breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },- }),- ],- ])('does not derive a slope from %s', (_, source) => {- expect(deriveChartPreview(ChartDisplayType.SlopeGraph, source, response([series({})]))).toBeNull()+ it('derives one slope per value from a breakdown', () => {+ const source = query(ChartDisplayType.ActionsLineGraph, {+ breakdownFilter: { breakdowns: [{ property: '$browser', type: 'event' }] },+ })+ const loaded = response([+ series({ breakdown_value: 'Chrome' }),+ series({ breakdown_value: 'Safari', data: [5, 6, 7, 8] }),+ series({ breakdown_value: 'Edge', data: [0, 7, 7, 0] }),+ ])++ expect(results(deriveChartPreview(ChartDisplayType.SlopeGraph, source, loaded))).toMatchObject([+ { breakdown_value: 'Chrome', data: [1, 4] },+ { breakdown_value: 'Safari', data: [5, 8] },+ { breakdown_value: 'Edge', data: [0, 0] },+ ])})})diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.tsindex 4a877620a664..18831be8967e 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts@@ -145,8 +145,6 @@ interface PreviewRecipe {const noBreakdown = (source: TrendsQuery): boolean => !hasBreakdown(source)const summable = (source: TrendsQuery, rows: PreviewRows): boolean => canSumBuckets(source, rows.results)-const canSlope = (source: TrendsQuery): boolean =>- (source.trendsFilter?.smoothingIntervals ?? 1) <= 1 && !hasBreakdown(source)const completeCountries = (source: TrendsQuery, rows: PreviewRows): boolean =>hasCountryCodeBreakdown(source) && isCompleteBreakdown(rows.response)@@ -159,7 +157,7 @@ const RECIPES: Partial<Record<ChartDisplayType, PreviewRecipe>> = {[ChartDisplayType.ActionsStackedBar]: { needs: 'buckets' },[ChartDisplayType.Metric]: { needs: 'buckets', when: noBreakdown },[ChartDisplayType.ActionsLineGraphCumulative]: { needs: 'buckets', when: summable, transform: toCumulative },- [ChartDisplayType.SlopeGraph]: { needs: 'buckets', when: canSlope, transform: toSlope },+ [ChartDisplayType.SlopeGraph]: { needs: 'buckets', transform: toSlope },[ChartDisplayType.BoldNumber]: { needs: 'totals', when: noBreakdown },[ChartDisplayType.ActionsPie]: { needs: 'totals' },[ChartDisplayType.ActionsDonut]: { needs: 'totals' },diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.tsindex 10be4b5577c4..5e8ee74ca529 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts@@ -88,10 +88,11 @@ describe('chartPreviewsLogic', () => {it('orders tiles as suggested, previewed, blank, then disabled', () => {insightVizDataLogic(insightProps).actions.updateQuerySource({...trendsQuery(ChartDisplayType.ActionsLineGraph),+ series: [{ kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.UniqueUsers }],breakdownFilter: { breakdowns: [{ property: '$browser', type: 'event' }] },})insightDataLogic(insightProps).actions.setInsightData({- results: [timeSeriesRow],+ results: [{ ...timeSeriesRow, action: { ...timeSeriesRow.action, math: BaseMathType.UniqueUsers } }],hasMore: true,last_refresh: FIRST_REFRESH,})diff --git a/products/product_analytics/frontend/insights/trends/TrendsSlopeChart/TrendsSlopeChart.tsx b/products/product_analytics/frontend/insights/trends/TrendsSlopeChart/TrendsSlopeChart.tsxindex ef5f5914153f..5acca889aa0f 100644--- a/products/product_analytics/frontend/insights/trends/TrendsSlopeChart/TrendsSlopeChart.tsx+++ b/products/product_analytics/frontend/insights/trends/TrendsSlopeChart/TrendsSlopeChart.tsx@@ -24,6 +24,7 @@ interface TrendsSlopeChartProps {}const handleChartError = makeChartErrorHandler('trends-slope-chart')+const PREVIEW_MARGINS = { left: 8, right: 8 }export function TrendsSlopeChart({ context }: TrendsSlopeChartProps): JSX.Element | null {const theme = useChartTheme()@@ -61,6 +62,9 @@ export function TrendsSlopeChart({ context }: TrendsSlopeChartProps): JSX.ElemenshowSeriesLabels: false,legend: { show: !!showLegend },hideXAxis: context?.hideAxes,+ showStartLabels: !context?.hideAxes,+ showEndLabels: !context?.hideAxes,+ margins: context?.hideAxes ? PREVIEW_MARGINS : undefined,xTickFormatter: createXAxisTickCallback({interval: interval ?? 'day',allDays: currentPeriodResult?.days ?? [],