: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): fill metric card in exported/shared insights (#70780)
Sam Penningtonacb9729fec1d
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex cde51484a882..ff76237dbe4e 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -2256,6 +2256,10 @@ snapshots:hash: v1.k794b7964.9e1bd66437108d2ae7be66826ebffdc276aa0e5d774aec80cc21f412e64bd88c.MHWc22XT82IGcvrDumZrv_AGDtt8aFuowT9zTB7TCxkexporter-other--sql-insight-no-results--light:hash: v1.k794b7964.61d2bc9e86a0f575ed9653d7a8591219e402a06f6e53021eee8efcc7eb87d644._4TYA0cVTREdm7CEPknXXiutaQB4rZKngjK1MrAGE-Y+ exporter-trends--trends-metric-insight--dark:+ hash: v1.k794b7964.5888bc8efe3994a6f6de72dc564dd209797d995747398bbe0f8cf489f4d72df2.WJEQf5G2g5LwsaQVobnxkbS2SiYJxDbaadzeIyamrlY+ exporter-trends--trends-metric-insight--light:+ hash: v1.k794b7964.12f7c6fa526a6c940fa5138db6ba1066ece183e5feb87f52f8a48d1470c4cd57.VWm4tXCvu-q1PRelHAug4e69-DB9Tt5fpKG_kbwJuS0exporter-trends--trends-pie-insight--dark:hash: v1.k794b7964.e5e7c9e36ec62680c87794152b474fcd8ae0061bc7e8145416512ee745e05d21.uVeV9IUiset22yU0BtIEzyXgbkadZEoT1M-mMbalaVwexporter-trends--trends-pie-insight--light:diff --git a/frontend/src/exporter/ExportedInsight/ExportedInsight.scss b/frontend/src/exporter/ExportedInsight/ExportedInsight.scssindex c8708a0238e4..cab249130274 100644--- a/frontend/src/exporter/ExportedInsight/ExportedInsight.scss+++ b/frontend/src/exporter/ExportedInsight/ExportedInsight.scss@@ -32,6 +32,24 @@border-radius: 0 0 var(--radius) var(--radius);}+ // A metric card renders best as a square card — header plus chart, proportioned like a dashboard+ // tile on a 2x2 grid — rather than stretched to the full exporter height. The aspect-ratio derives+ // the card's height from the export width, and the content flex-fills the space under the header so+ // the filling sparkline hugs the bottom of the card.+ &--metric {+ flex: 0 0 auto;+ height: auto;+ aspect-ratio: 1 / 1;++ .ExportedInsight__content {+ flex: 1 1 0;++ // `min-height: 0` overrides the flex-item `auto` floor so the aspect-ratio governs the card's+ // height instead of the content's min-content height stretching it taller than a square.+ min-height: 0;+ }+ }+.LemonTable {--lemon-table-background-color: var(--color-bg-surface-primary);}diff --git a/frontend/src/exporter/ExportedInsight/ExportedInsight.tsx b/frontend/src/exporter/ExportedInsight/ExportedInsight.tsxindex c5f07b060cbc..1d1e9ccee657 100644--- a/frontend/src/exporter/ExportedInsight/ExportedInsight.tsx+++ b/frontend/src/exporter/ExportedInsight/ExportedInsight.tsx@@ -64,6 +64,7 @@ export function ExportedInsight({const trendsDisplay =isInsightVizNode(query) && isTrendsQuery(query.source) ? query.source.trendsFilter?.display : undefinedconst isBoxPlot = trendsDisplay === ChartDisplayType.BoxPlot+ const isMetric = trendsDisplay === ChartDisplayType.Metricconst showLegend =legend &&isInsightVizNode(query) &&@@ -79,7 +80,7 @@ export function ExportedInsight({return (<BindLogic logic={insightLogic} props={insightLogicProps}>- <div className="ExportedInsight">+ <div className={clsx('ExportedInsight', isMetric && 'ExportedInsight--metric')}>{!noHeader && (<div className="ExportedInsight__header"><div>diff --git a/frontend/src/exporter/Exporter.scss b/frontend/src/exporter/Exporter.scssindex 8cbc4d1e31cf..86d6f51ac5b3 100644--- a/frontend/src/exporter/Exporter.scss+++ b/frontend/src/exporter/Exporter.scss@@ -16,6 +16,12 @@ body.ExporterBody {max-height: 100vh;}+ // A metric export is a square card, not a full-page insight — drop the viewport-height floor so the+ // exporter wraps the card tightly instead of padding it out to 100vh.+ &--metric {+ min-height: 0;+ }+&--heatmap {padding: 0;diff --git a/frontend/src/exporter/Exporter.tsx b/frontend/src/exporter/Exporter.tsxindex 76806b21c1d7..1466f529fe99 100644--- a/frontend/src/exporter/Exporter.tsx+++ b/frontend/src/exporter/Exporter.tsx@@ -18,6 +18,8 @@ import { teamLogic } from 'scenes/teamLogic'import { ExporterLogin } from '~/exporter/ExporterLogin'import { ExportType, ExportedData } from '~/exporter/types'+import { isInsightVizNode, isTrendsQuery } from '~/queries/utils'+import { ChartDisplayType } from '~/types'import { exporterViewLogic } from './exporterViewLogic'@@ -66,6 +68,16 @@ export function Exporter(props: ExportedData): JSX.Element {const { whitelabel, showInspector = false } = exportOptionsconst forcedTheme = resolveForcedTheme(exportOptions.theme)+ // A metric insight sizes to a square card rather than filling the viewport, so drop the 100vh floor+ // that would otherwise leave empty space below it (see Exporter.scss and ExportedInsight.scss).+ const metric =+ insight &&+ isInsightVizNode(insight.query) &&+ isTrendsQuery(insight.query.source) &&+ insight.query.source.trendsFilter?.display === ChartDisplayType.Metric+ ? insight+ : undefined+const { currentTeam } = useValues(teamLogic)const { ref: elementRef, height, width } = useResizeObserver()@@ -109,6 +121,7 @@ export function Exporter(props: ExportedData): JSX.Element {<divclassName={clsx('Exporter', {'Exporter--insight': !!insight,+ 'Exporter--metric': !!metric,'Exporter--dashboard': !!dashboard,'Exporter--recording': !!recording,'Exporter--notebook': !!notebook,diff --git a/frontend/src/exporter/stories/ExporterTrends.stories.tsx b/frontend/src/exporter/stories/ExporterTrends.stories.tsxindex fd6ba41caf82..bfb5e27dfd37 100644--- a/frontend/src/exporter/stories/ExporterTrends.stories.tsx+++ b/frontend/src/exporter/stories/ExporterTrends.stories.tsx@@ -10,6 +10,7 @@ import __trendsBarBreakdown from '../../mocks/fixtures/api/projects/team_id/insiimport __trendsLine from '../../mocks/fixtures/api/projects/team_id/insights/trendsLine.json'import __trendsLineBreakdown from '../../mocks/fixtures/api/projects/team_id/insights/trendsLineBreakdown.json'import __trendsLineMulti from '../../mocks/fixtures/api/projects/team_id/insights/trendsLineMulti.json'+import __trendsMetric from '../../mocks/fixtures/api/projects/team_id/insights/trendsMetric.json'import __trendsNumber from '../../mocks/fixtures/api/projects/team_id/insights/trendsNumber.json'import __trendsPie from '../../mocks/fixtures/api/projects/team_id/insights/trendsPie.json'import __trendsTable from '../../mocks/fixtures/api/projects/team_id/insights/trendsTable.json'@@ -142,6 +143,19 @@ export const TrendsNumberInsight: Story = {tags: ['test-skip'], // doesn't produce a helpful reference image, as canvas can't be captured}+/** The Metric sparkline is SVG/canvas, so it captures cleanly and guards against the exported card+ * collapsing to a fixed-height sparkline that floats mid-card instead of filling and hugging the bottom. */+export const TrendsMetricInsight: Story = {+ args: { insight: __trendsMetric as any },+ parameters: {+ mockDate: '2022-04-01',+ testOptions: {+ snapshotBrowsers: ['chromium'],+ waitForSelector: '.Metric canvas',+ },+ },+}+export const TrendsTableInsight: Story = {args: { insight: __trendsTable as any },}diff --git a/frontend/src/scenes/trends/Trends.tsx b/frontend/src/scenes/trends/Trends.tsxindex 0ca82c69d17d..c64f412227e5 100644--- a/frontend/src/scenes/trends/Trends.tsx+++ b/frontend/src/scenes/trends/Trends.tsx@@ -87,7 +87,9 @@ export function TrendInsight({ view, context, embedded, inSharedMode, editMode }const commonProps = {showPersonsModal,context,- inCardView: embedded && !inSharedMode,+ // Fill the card in every embedded surface (dashboard tiles and shared/exported insights alike) so the+ // Metric sparkline stretches and hugs the bottom instead of collapsing to a fixed height mid-card.+ inCardView: embedded,inSharedMode,}diff --git a/products/exports/backend/tasks/image_exporter.py b/products/exports/backend/tasks/image_exporter.pyindex fee08e51887e..535ccc487a3c 100644--- a/products/exports/backend/tasks/image_exporter.py+++ b/products/exports/backend/tasks/image_exporter.py@@ -18,7 +18,7 @@ from playwright.sync_api import ()from prometheus_client import Counter, Histogram-from posthog.schema import FunnelLayout, NodeKind+from posthog.schema import ChartDisplayType, FunnelLayout, NodeKindfrom posthog.caching.calculate_results import calculate_for_query_based_insightfrom posthog.event_usage import AnalyticsProps, EventSource@@ -130,7 +130,7 @@ MEASURE_CONTENT_WIDTH_JS = f"""return null;"""-ScreenWidth = Literal[800, 1920, 1400, 4000]+ScreenWidth = Literal[600, 800, 1920, 1400, 4000]CSSSelector = Literal[".InsightCard", ".ExportedInsight", ".replayer-wrapper", ".heatmap-exporter"]@@ -192,10 +192,25 @@ def _export_to_png(funnels_filter = source.get("funnelsFilter") or {}funnel_layout = funnels_filter.get("layout")is_left_to_right_funnel = is_funnel and (funnel_layout is None or funnel_layout == FunnelLayout.VERTICAL)+ # A metric renders as a square card whose height derives from the viewport width, so a+ # narrower viewport keeps it at dashboard-tile size instead of an 800px-tall square.+ # Mirrors the frontend's check (InsightVizNode wrapper required), which gates the square+ # metric CSS — a bare TrendsQuery gets neither, so it must keep the default viewport.+ trends_filter = source.get("trendsFilter") or {}+ is_metric = (+ query.get("kind") == NodeKind.INSIGHT_VIZ_NODE+ and source.get("kind") == NodeKind.TRENDS_QUERY+ and trends_filter.get("display") == ChartDisplayType.METRIC+ )# Set initial window size large enough for wide content like left-to-right funnels with many steps# Small funnels will be constrained later.# The higher the number, the more RAM will be required by the Chromium driver.- screenshot_width = 4000 if is_left_to_right_funnel else 800+ if is_left_to_right_funnel:+ screenshot_width = 4000+ elif is_metric:+ screenshot_width = 600+ else:+ screenshot_width = 800elif exported_asset.dashboard is not None:cache_keys_param = _build_cache_keys_param(insight_cache_keys)url_to_render = absolute_uri(f"/exporter?token={access_token}{cache_keys_param}")