: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(web-analytics): export web vitals tiles in the csv download (#108536)
Jordan Mryyan0c421e653b3a
diff --git a/frontend/src/queries/nodes/WebVitals/WebVitals.tsx b/frontend/src/queries/nodes/WebVitals/WebVitals.tsxindex b3bca5400a1d..f5ad66faacb8 100644--- a/frontend/src/queries/nodes/WebVitals/WebVitals.tsx+++ b/frontend/src/queries/nodes/WebVitals/WebVitals.tsx@@ -10,6 +10,7 @@ import { addProductIntentForCrossSell } from 'lib/utils/product-intents'import { urls } from 'scenes/urls'import { webAnalyticsLogic } from 'scenes/web-analytics/webAnalyticsLogic'+import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/insightVizKeys'import { Query } from '~/queries/Query/Query'import {AnyResponseType,@@ -32,8 +33,11 @@ export function WebVitals(props: {context: QueryContextattachTo?: LogicWrapper | BuiltLogic}): JSX.Element | null {- const { onData, loadPriority, dataNodeCollectionId } = props.context.insightProps ?? {}- const [key] = useState(() => `WebVitals.${uniqueNode++}`)+ const { insightProps } = props.context+ const { onData, loadPriority, dataNodeCollectionId } = insightProps ?? {}+ const [key] = useState(() =>+ insightProps?.dashboardItemId ? insightVizDataNodeKey(insightProps) : `WebVitals.${uniqueNode++}`+ )const logic = dataNodeLogic({query: props.query,diff --git a/frontend/src/queries/nodes/WebVitals/WebVitalsPathBreakdown.tsx b/frontend/src/queries/nodes/WebVitals/WebVitalsPathBreakdown.tsxindex 0232ba5ca464..8f352841e4e3 100644--- a/frontend/src/queries/nodes/WebVitals/WebVitalsPathBreakdown.tsx+++ b/frontend/src/queries/nodes/WebVitals/WebVitalsPathBreakdown.tsx@@ -8,6 +8,7 @@ import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'import { webAnalyticsLogic } from 'scenes/web-analytics/webAnalyticsLogic'+import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/insightVizKeys'import {AnyResponseType,WebAnalyticsPreComputeStrategy,@@ -34,8 +35,11 @@ export function WebVitalsPathBreakdown(props: {context: QueryContextattachTo?: LogicWrapper | BuiltLogic}): JSX.Element | null {- const { onData, loadPriority, dataNodeCollectionId } = props.context.insightProps ?? {}- const [key] = useState(() => `WebVitalsPathBreakdown.${uniqueNode++}`)+ const { insightProps } = props.context+ const { onData, loadPriority, dataNodeCollectionId } = insightProps ?? {}+ const [key] = useState(() =>+ insightProps?.dashboardItemId ? insightVizDataNodeKey(insightProps) : `WebVitalsPathBreakdown.${uniqueNode++}`+ )const logic = dataNodeLogic({query: props.query,diff --git a/frontend/src/scenes/web-analytics/WebAnalyticsExport.test.tsx b/frontend/src/scenes/web-analytics/WebAnalyticsExport.test.tsxindex bca8d6f65584..9f9b788b82d6 100644--- a/frontend/src/scenes/web-analytics/WebAnalyticsExport.test.tsx+++ b/frontend/src/scenes/web-analytics/WebAnalyticsExport.test.tsx@@ -1,6 +1,68 @@-import { DataTableNode, NodeKind, WebStatsBreakdown, WebStatsTableQueryResponse } from '~/queries/schema/schema-general'+import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'+import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/insightVizKeys'+import {+ DataTableNode,+ NodeKind,+ WebStatsBreakdown,+ WebStatsTableQueryResponse,+ WebVitalsPathBreakdownQuery,+ WebVitalsPathBreakdownQueryResponse,+ WebVitalsQuery,+ WebVitalsQueryResponse,+} from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'+import { InsightLogicProps, PropertyMathType } from '~/types'-import { WebAnalyticsTableAdapter, buildCsvFilenames } from './webAnalyticsExportUtils'+import { TileId, WebAnalyticsTile } from './common'+import {+ WebAnalyticsTableAdapter,+ buildCsvFilenames,+ collectAllTilesTableData,+ getExportAdapter,+} from './webAnalyticsExportUtils'++const webVitalsQuery: WebVitalsQuery = {+ kind: NodeKind.WebVitalsQuery,+ properties: [],+ source: {+ kind: NodeKind.TrendsQuery,+ series: [],+ },+}++const webVitalsResponse: WebVitalsQueryResponse = {+ results: [+ {+ action: { custom_name: 'LCP', math: PropertyMathType.P90 },+ days: ['2026-09-01', '2026-09-02'],+ data: [2400, 2600],+ },+ {+ action: { custom_name: 'CLS', math: PropertyMathType.P90 },+ days: ['2026-09-01', '2026-09-02'],+ data: [0.04, 0.05],+ },+ ],+}++const pathBreakdownQuery: WebVitalsPathBreakdownQuery = {+ kind: NodeKind.WebVitalsPathBreakdownQuery,+ dateRange: { date_from: '-7d' },+ properties: [],+ percentile: PropertyMathType.P90,+ metric: 'FCP',+ thresholds: [1800, 3000],+}++const pathBreakdownResponse: WebVitalsPathBreakdownQueryResponse = {+ results: [+ {+ good: [{ path: '/pricing', value: 900 }],+ needs_improvements: [],+ poor: [{ path: '/', value: 13320 }],+ },+ ],+}describe('WebAnalyticsExport adapters', () => {describe('buildCsvFilenames', () => {@@ -121,4 +183,80 @@ describe('WebAnalyticsExport adapters', () => {])})})++ describe('web vitals adapters', () => {+ it.each([+ {+ name: 'timeseries',+ response: webVitalsResponse,+ query: webVitalsQuery,+ expected: [+ ['Date', 'LCP p90 (ms)', 'CLS p90'],+ ['2026-09-01', '2400', '0.04'],+ ['2026-09-02', '2600', '0.05'],+ ],+ },+ {+ name: 'timeseries without results',+ response: { results: [] },+ query: webVitalsQuery,+ expected: [],+ },+ {+ name: 'path breakdown',+ response: pathBreakdownResponse,+ query: pathBreakdownQuery,+ expected: [+ ['Band', 'Path', 'FCP p90 (ms)'],+ ['Great', '/pricing', '900'],+ ['Poor', '/', '13320'],+ ],+ },+ {+ name: 'path breakdown without paths',+ response: { results: [{ good: [], needs_improvements: [], poor: [] }] },+ query: pathBreakdownQuery,+ expected: [],+ },+ ])('converts $name', ({ response, query, expected }) => {+ expect(getExportAdapter(response, query)?.toTableData()).toEqual(expected)+ })++ it('collects the web vitals tiles from their mounted data node logics', () => {+ initKeaTests()+ const webVitalsInsightProps: InsightLogicProps = { dashboardItemId: 'new-web-vitals-tile' }+ const pathBreakdownInsightProps: InsightLogicProps = { dashboardItemId: 'new-web-vitals-path-tile' }+ const tiles: WebAnalyticsTile[] = [+ {+ kind: 'query',+ tileId: TileId.WEB_VITALS,+ layout: {},+ query: webVitalsQuery,+ insightProps: webVitalsInsightProps,+ },+ {+ kind: 'query',+ tileId: TileId.WEB_VITALS_PATH_BREAKDOWN,+ layout: {},+ query: pathBreakdownQuery,+ insightProps: pathBreakdownInsightProps,+ },+ ]+ dataNodeLogic({+ key: insightVizDataNodeKey(webVitalsInsightProps),+ query: webVitalsQuery,+ cachedResults: webVitalsResponse,+ }).mount()+ dataNodeLogic({+ key: insightVizDataNodeKey(pathBreakdownInsightProps),+ query: pathBreakdownQuery,+ cachedResults: pathBreakdownResponse,+ }).mount()++ expect(collectAllTilesTableData(tiles).map(({ title, tableData }) => [title, tableData.length])).toEqual([+ ['Web vitals', 3],+ ['Web vitals path breakdown: FCP', 3],+ ])+ })+ })})diff --git a/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx b/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsxindex 91baf9ae128f..ae792381bb51 100644--- a/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx+++ b/frontend/src/scenes/web-analytics/WebAnalyticsMenu.tsx@@ -37,6 +37,7 @@ const ANALYTICS_TILES = [export const WebAnalyticsMenu = (): JSX.Element => {const {+ exportAllDisabledReason,hasSavedFocusMode,hiddenTiles,isFocusModeActive,@@ -63,6 +64,7 @@ export const WebAnalyticsMenu = (): JSX.Element => {<ButtonPrimitivemenuItemdata-attr="web-analytics-export-all-csv"+ disabledReasons={exportAllDisabledReason ? { [exportAllDisabledReason]: true } : undefined}onClick={() => {if (exportAllTilesAsCsvZip(tiles)) {exportTriggered()diff --git a/frontend/src/scenes/web-analytics/WebAnalyticsSceneMenuBar.tsx b/frontend/src/scenes/web-analytics/WebAnalyticsSceneMenuBar.tsxindex aa3232cb4ee7..2e4debda4618 100644--- a/frontend/src/scenes/web-analytics/WebAnalyticsSceneMenuBar.tsx+++ b/frontend/src/scenes/web-analytics/WebAnalyticsSceneMenuBar.tsx@@ -91,6 +91,7 @@ export function WebAnalyticsSceneMenuBar(): JSX.Element | null {function WebAnalyticsSceneMenuBarInner(): JSX.Element {const {+ exportAllDisabledReason,hasSavedFocusMode,hiddenTiles,isFocusModeActive,@@ -166,6 +167,8 @@ function WebAnalyticsSceneMenuBarInner(): JSX.Element {</SceneMenuBarItem>)}<SceneMenuBarItem+ disabled={!!exportAllDisabledReason}+ tooltip={exportAllDisabledReason ?? undefined}onClick={() => {if (exportAllTilesAsCsvZip(tiles)) {exportTriggered()diff --git a/frontend/src/scenes/web-analytics/webAnalyticsExportUtils.ts b/frontend/src/scenes/web-analytics/webAnalyticsExportUtils.tsindex 0adce15c8293..e153f54626da 100644--- a/frontend/src/scenes/web-analytics/webAnalyticsExportUtils.ts+++ b/frontend/src/scenes/web-analytics/webAnalyticsExportUtils.ts@@ -4,9 +4,16 @@ import { lemonToast } from '@posthog/lemon-ui'import { downloadFile } from 'lib/utils/dom'import { slugify } from 'lib/utils/strings'-import { insightDataLogic } from 'scenes/insights/insightDataLogic'-import { ExportAdapter, csvFromTableData, getInsightExportAdapter } from '~/queries/nodes/InsightViz/exportAdapters'+import { DataNodeLogicProps, dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'+import {+ ExportAdapter,+ TrendsAdapter,+ csvFromTableData,+ getInsightExportAdapter,+} from '~/queries/nodes/InsightViz/exportAdapters'+import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/insightVizKeys'+import { GRADE_PER_BAND } from '~/queries/nodes/WebVitals/definitions'import {DataTableNode,NodeKind,@@ -15,9 +22,26 @@ import {WebGoalsQuery,WebStatsTableQuery,WebStatsTableQueryResponse,+ WebVitalsMetric,+ WebVitalsMetricBand,+ WebVitalsPathBreakdownQuery,+ WebVitalsPathBreakdownQueryResponse,+ WebVitalsQueryResponse,} from '~/queries/schema/schema-general'-import { isWebExternalClicksQuery, isWebGoalsQuery, isWebStatsTableQuery } from '~/queries/utils'-import { TabsTileTab, TILE_LABELS, WebAnalyticsTile, getDisplayColumnName } from '~/scenes/web-analytics/common'+import {+ isWebExternalClicksQuery,+ isWebGoalsQuery,+ isWebStatsTableQuery,+ isWebVitalsPathBreakdownQuery,+ isWebVitalsQuery,+} from '~/queries/utils'+import {+ QueryTile,+ TabsTileTab,+ TILE_LABELS,+ WebAnalyticsTile,+ getDisplayColumnName,+} from '~/scenes/web-analytics/common'import { InsightLogicProps } from '~/types'export interface TileExportSection {@@ -75,6 +99,12 @@ export function getExportAdapter(insightDataRaw: unknown, query: QuerySchema | uif (!insightDataRaw || !query) {return null}+ if (isWebVitalsQuery(query)) {+ return webVitalsTrendsAdapter(insightDataRaw as WebVitalsQueryResponse)+ }+ if (isWebVitalsPathBreakdownQuery(query)) {+ return new WebVitalsPathBreakdownAdapter(insightDataRaw as WebVitalsPathBreakdownQueryResponse, query)+ }const webAdapter = new WebAnalyticsTableAdapter(insightDataRaw as WebStatsTableQueryResponse, query)if (webAdapter.canHandle()) {return webAdapter@@ -82,15 +112,18 @@ export function getExportAdapter(insightDataRaw: unknown, query: QuerySchema | ureturn getInsightExportAdapter(insightDataRaw, query)}+function findMountedTileDataNode(insightProps: InsightLogicProps): ReturnType<typeof dataNodeLogic.findMounted> {+ return dataNodeLogic.findMounted({ key: insightVizDataNodeKey(insightProps) } as DataNodeLogicProps)+}+function tileToTableData(query: QuerySchema, insightProps: InsightLogicProps): string[][] | null {- const insightDataRaw = insightDataLogic.findMounted(insightProps)?.values.insightDataRaw- const adapter = getExportAdapter(insightDataRaw, query)+ const adapter = getExportAdapter(findMountedTileDataNode(insightProps)?.values.response, query)const tableData = adapter?.toTableData() ?? []return tableData.length > 0 ? tableData : null}function isTileStillLoading(insightProps: InsightLogicProps): boolean {- return insightDataLogic.findMounted(insightProps)?.values.insightDataLoading === true+ return findMountedTileDataNode(insightProps)?.values.dataLoading === true}export function anyTileStillLoading(tiles: WebAnalyticsTile[]): boolean {@@ -135,13 +168,18 @@ export function exportAllTilesAsCsvZip(tiles: WebAnalyticsTile[], filename = 'wereturn true}+function queryTileTitle(tile: QueryTile): string {+ const base = tile.title ?? TILE_LABELS[tile.tileId] ?? tile.tileId+ return isWebVitalsPathBreakdownQuery(tile.query) ? `${base}: ${tile.query.metric}` : base+}+export function collectAllTilesTableData(tiles: WebAnalyticsTile[]): TileExportSection[] {const sections: TileExportSection[] = []for (const tile of tiles) {if (tile.kind === 'query') {const tableData = tileToTableData(tile.query, tile.insightProps)if (tableData) {- sections.push({ title: tile.title ?? TILE_LABELS[tile.tileId] ?? tile.tileId, tableData })+ sections.push({ title: queryTileTitle(tile), tableData })}} else if (tile.kind === 'tabs') {const activeTab = tile.tabs.find((tab) => tab.id === tile.activeTabId)@@ -241,3 +279,41 @@ export class WebAnalyticsTableAdapter implements ExportAdapter {return hasData && columns.length > 0}}++function webVitalsColumnName(metric: WebVitalsMetric, percentile: string): string {+ return metric === 'CLS' ? `${metric} ${percentile}` : `${metric} ${percentile} (ms)`+}++function webVitalsTrendsAdapter(response: WebVitalsQueryResponse): TrendsAdapter {+ return new TrendsAdapter({+ ...response,+ results: (response.results ?? []).map((item) => ({+ ...item,+ action: { ...item.action, custom_name: webVitalsColumnName(item.action.custom_name, item.action.math) },+ })),+ })+}++const WEB_VITALS_BANDS: WebVitalsMetricBand[] = ['good', 'needs_improvements', 'poor']++class WebVitalsPathBreakdownAdapter implements ExportAdapter {+ constructor(+ private response: WebVitalsPathBreakdownQueryResponse,+ private query: WebVitalsPathBreakdownQuery+ ) {}++ toTableData(): string[][] {+ const breakdown = this.response.results?.[0]+ const rows = WEB_VITALS_BANDS.flatMap((band) =>+ (breakdown?.[band] ?? []).map(({ path, value }) => [GRADE_PER_BAND[band], path, String(value)])+ )+ if (rows.length === 0) {+ return []+ }+ return [['Band', 'Path', webVitalsColumnName(this.query.metric, this.query.percentile)], ...rows]+ }++ canHandle(): boolean {+ return true+ }+}diff --git a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsxindex 404e605d33f1..dbe177574452 100644--- a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx+++ b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx@@ -196,6 +196,7 @@ export interface webAnalyticsLogicValues {currentFiltersConfig: WebAnalyticsFiltersConfigdateFilter: DateFilterStatedeviceTab: string+ exportAllDisabledReason: string | nullfilters: {compareFilter: CompareFilterconversionGoal: WebAnalyticsConversionGoal | null@@ -660,6 +661,7 @@ export interface webAnalyticsLogicMeta {shouldFilterTestAccounts: boolean) => InsightVizNode<TrendsQuery>showFocusMode: (featureFlags: FeatureFlagsSet, productTab: ProductTab) => boolean+ exportAllDisabledReason: (productTab: ProductTab) => string | nullhasSavedFocusMode: (focusModeConcerns: WebAnalyticsConcern[]) => booleanhasSeenFocusModeOnboarding: (user: UserType | null, currentTeam: TeamPublicType | TeamType | null) => booleanshouldAutoOpenFocusModeOnboarding: (@@ -1682,6 +1684,13 @@ export const webAnalyticsLogic: LogicWrapper<webAnalyticsLogicType> = kea<webAna(featureFlags: import('lib/logic/featureFlagLogic').FeatureFlagsSet, productTab: ProductTab): boolean =>featureFlags[FEATURE_FLAGS.WEB_ANALYTICS_FOCUS_MODE] === 'test' && productTab === ProductTab.ANALYTICS,],+ exportAllDisabledReason: [+ (s) => [s.productTab],+ (productTab: ProductTab): string | null =>+ productTab === ProductTab.ANALYTICS || productTab === ProductTab.WEB_VITALS+ ? null+ : 'Switch to the Web analytics or Web vitals tab to export as CSV',+ ],hasSavedFocusMode: [(s) => [s.focusModeConcerns],(focusModeConcerns: WebAnalyticsConcern[]): boolean => focusModeConcerns.length > 0,