Skip to content

: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.

fix(insights): load the web analytics insight lazily (#112339)

Paul D'Ambra68249a343920
diff --git a/frontend/bin/check-eager-graph.mjs b/frontend/bin/check-eager-graph.mjs
index 68b34b9d6c2c..563082cdeda2 100644
--- a/frontend/bin/check-eager-graph.mjs
+++ b/frontend/bin/check-eager-graph.mjs
@@ -133,6 +133,9 @@ const ROOTS = [
forbidden: [
// Neither scene plays recordings. A hit means a static import pulled the playlist and player onto it.
'src/scenes/session-recordings/playlist/SessionRecordingsPlaylist.tsx',
+ // Only web analytics insights render this tile. A hit means a static import put the web
+ // analytics stack on every page that shows an insight.
+ 'src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx',
],
},
{
@@ -157,6 +160,9 @@ const ROOTS = [
forbidden: [
// Neither scene plays recordings. A hit means a static import pulled the playlist and player onto it.
'src/scenes/session-recordings/playlist/SessionRecordingsPlaylist.tsx',
+ // Only web analytics insights render this tile. A hit means a static import put the web
+ // analytics stack on every page that shows an insight.
+ 'src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx',
],
},
{
diff --git a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
index 2b6f265c3db0..f7b775d5f20d 100644
--- a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
+++ b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
@@ -1,11 +1,14 @@
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
+import { Suspense } from 'react'
-import { LemonButton } from '@posthog/lemon-ui'
+import { LemonButton, Spinner } from '@posthog/lemon-ui'
import { ExportButton } from 'lib/components/ExportButton/ExportButton'
import { InsightLegend } from 'lib/components/InsightLegend/InsightLegend'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
+import { lazyWithRetry } from 'lib/utils/retryImport'
+import { ChunkLoadErrorBoundary } from 'scenes/ChunkLoadErrorBoundary'
import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'
import {
BoxPlotMissingPropertyState,
@@ -30,7 +33,6 @@ import { insightNavLogic } from 'scenes/insights/InsightNav/insightNavLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
import { isBoxPlotMissingProperty } from 'scenes/insights/utils/queryUtils'
-import { WebAnalyticsInsight } from 'scenes/web-analytics/WebAnalyticsInsight'
import { SceneSection } from '~/layout/scenes/components/SceneSection'
import { InsightVizNode, TrendsQuery } from '~/queries/schema/schema-general'
@@ -66,6 +68,12 @@ import { InsightDisplayConfig } from './InsightDisplayConfig'
import { InsightResultMetadata } from './InsightResultMetadata'
import { ResultCustomizationsModal } from './ResultCustomizationsModal'
+// Query.tsx reaches this file through static imports, so a static import of web analytics puts its
+// tiles on every page that shows an insight.
+const WebAnalyticsInsight = lazyWithRetry(() =>
+ import('scenes/web-analytics/WebAnalyticsInsight').then((m) => ({ default: m.WebAnalyticsInsight }))
+)
+
/** When the dashboard is still streaming/refreshing tiles, prefer loading UX over "Chart data didn't load". */
function DashboardInsightRefreshHintOrLoading({
dashboardId,
@@ -219,21 +227,23 @@ export function InsightVizDisplay({
const isFlowViz = funnelVizType === FunnelVizType.Flow
const actionable = !embedded && editMode
+ const insightLoadingState = (
+ <InsightLoadingState
+ queryId={queryId}
+ key={queryId}
+ insightProps={insightProps}
+ renderEmptyStateAsSkeleton={context?.renderEmptyStateAsSkeleton}
+ suppressSlowQuerySuggestions={context?.suppressSlowQuerySuggestions}
+ />
+ )
+
// Empty states that completely replace the graph
const BlockingEmptyState = (() => {
if (insightDataLoading) {
if (hasRenderableResults) {
return null
}
- return (
- <InsightLoadingState
- queryId={queryId}
- key={queryId}
- insightProps={insightProps}
- renderEmptyStateAsSkeleton={context?.renderEmptyStateAsSkeleton}
- suppressSlowQuerySuggestions={context?.suppressSlowQuerySuggestions}
- />
- )
+ return insightLoadingState
}
// Insight specific empty states - note order is important here
@@ -430,7 +440,14 @@ export function InsightVizDisplay({
case InsightType.JOURNEYS:
return <Journeys showPersonsModal={!inSharedMode} />
case InsightType.WEB_ANALYTICS:
- return <WebAnalyticsInsight context={context} editMode={editMode} />
+ return (
+ <ChunkLoadErrorBoundary>
+ {/* The image exporter waits for every .Spinner to detach, so a Spinner fallback stops it capturing the page before this chunk loads. */}
+ <Suspense fallback={<Spinner className="text-3xl mx-auto my-8" />}>
+ <WebAnalyticsInsight context={context} editMode={editMode} />
+ </Suspense>
+ </ChunkLoadErrorBoundary>
+ )
default:
return null
}
@@ -534,15 +551,7 @@ export function InsightVizDisplay({
// Web Analytics insights don't use themes, so allow them to render without waiting for theme to load
if (!theme && activeView !== InsightType.WEB_ANALYTICS) {
- return (
- <InsightLoadingState
- queryId={queryId}
- key={queryId}
- insightProps={insightProps}
- renderEmptyStateAsSkeleton={context?.renderEmptyStateAsSkeleton}
- suppressSlowQuerySuggestions={context?.suppressSlowQuerySuggestions}
- />
- )
+ return insightLoadingState
}
return (