: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
import { type ReactElement, useMemo } from 'react'
import { emptyStateIllustration } from '@posthog/mcp-ui'
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia } from '@posthog/quill'
import { SankeyChart } from '@posthog/quill-charts'
import type { SankeyChartConfig } from '@posthog/quill-charts'
import { parsePathNodeKey } from 'products/product_analytics/frontend/insights/paths/pathsChartTransforms'
import { ChartHeader } from './ChartHeader'
import { useMcpChartTheme } from './charts/theme'
import { buildPathsView, MAX_DRAWN_STEPS } from './pathsView'
import type { PathsResultItem, PathsVisualizerProps } from './types'
import { formatDuration, formatNumber } from './utils'
const TITLE = 'Paths'
const CHART_CONFIG: SankeyChartConfig = {
linkOpacity: 0.35,
valueFormatter: formatNumber,
}
/** Which steps a transition joins, so a pair of pages that repeats at two stages reads apart. */
function stepRange(edge: PathsResultItem): string {
return `step ${parsePathNodeKey(edge.source).step} to ${parsePathNodeKey(edge.target).step}`
}
export function PathsVisualizer({ results }: PathsVisualizerProps): ReactElement {
const theme = useMcpChartTheme()
const {
allEdges,
edges,
truncated,
stepsClipped,
graph,
columnLabels,
nodePadding,
stepCount,
pathStarts,
chartWidth,
} = useMemo(() => buildPathsView(results), [results])
// A node's value counts only its drawn ribbons, so a truncated view would label partial totals.
const config = useMemo<SankeyChartConfig>(
() => ({ ...CHART_CONFIG, columnLabels, nodePadding, showNodeValues: !truncated }),
[columnLabels, nodePadding, truncated]
)
const labelOf = useMemo(() => new Map(graph.nodes.map((node) => [node.id, node.label])), [graph.nodes])
if (allEdges.length === 0) {
return (
<div>
<ChartHeader title={TITLE} />
<Empty>
<EmptyHeader>
<EmptyMedia>{emptyStateIllustration('generic')}</EmptyMedia>
<EmptyDescription>No path data available</EmptyDescription>
</EmptyHeader>
</Empty>
</div>
)
}
return (
<div data-attr="paths-sankey" className="w-full">
<ChartHeader title={TITLE} />
<div className="h-80 w-full overflow-x-auto">
{/* The chart sizes to its parent, so a long path needs a wider parent to scroll. */}
<div className="flex flex-col h-full" style={{ width: chartWidth }}>
<SankeyChart<string, PathsResultItem>
nodes={graph.nodes}
links={graph.links}
theme={theme}
config={config}
/>
</div>
</div>
{/* The canvas has no per-ribbon semantics, so screen readers get the transitions as text. */}
<ul className="sr-only">
{edges.map((edge) => (
<li key={`${edge.source}→${edge.target}`}>
{labelOf.get(edge.source)} to {labelOf.get(edge.target)} ({stepRange(edge)}):{' '}
{formatNumber(edge.value ?? 0)} paths
{edge.average_conversion_time != null
? `, ${formatDuration(edge.average_conversion_time)} on average`
: ''}
</li>
))}
</ul>
<div className="mt-4 rounded-md bg-muted/50 p-3 text-sm text-muted-foreground">
{truncated ? (
<>
Showing the <strong className="text-foreground">{formatNumber(edges.length)}</strong> busiest of{' '}
<strong className="text-foreground">{formatNumber(allEdges.length)}</strong> path transitions
</>
) : (
<>
<strong className="text-foreground">{formatNumber(edges.length)}</strong> path transition
{edges.length === 1 ? '' : 's'}
</>
)}{' '}
across <strong className="text-foreground">{formatNumber(stepCount)}</strong> step
{stepCount === 1 ? '' : 's'}
{pathStarts > 0 && (
<>
{' '}
· <strong className="text-foreground">{formatNumber(pathStarts)}</strong> path
{pathStarts === 1 ? ' in this result begins' : 's in this result begin'} at step 1
</>
)}
{stepsClipped && <> · The chart shows steps 1 to {MAX_DRAWN_STEPS}</>}
</div>
</div>
)
}