: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 { BuiltLogic, LogicWrapper, useActions, useValues } from 'kea'
import { useMemo, useState } from 'react'
import { Link } from '@posthog/lemon-ui'
import { IconOpenInNew } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
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,
ProductIntentContext,
ProductKey,
WebVitalsQuery,
WebVitalsQueryResponse,
} from '~/queries/schema/schema-general'
import { QueryContext } from '~/queries/types'
import { dataNodeLogic } from '../DataNode/dataNodeLogic'
import { getMetric } from './definitions'
import { WebVitalsContent } from './WebVitalsContent'
import { WebVitalsTab } from './WebVitalsTab'
let uniqueNode = 0
export function WebVitals(props: {
query: WebVitalsQuery
cachedResults?: AnyResponseType
context: QueryContext
attachTo?: LogicWrapper | BuiltLogic
}): JSX.Element | null {
const { insightProps } = props.context
const { onData, loadPriority, dataNodeCollectionId } = insightProps ?? {}
const [key] = useState(() =>
insightProps?.dashboardItemId ? insightVizDataNodeKey(insightProps) : `WebVitals.${uniqueNode++}`
)
const logic = dataNodeLogic({
query: props.query,
key,
cachedResults: props.cachedResults,
loadPriority,
onData,
dataNodeCollectionId: dataNodeCollectionId ?? key,
})
useAttachedLogic(logic, props.attachTo)
const { webVitalsPercentile, webVitalsTab, webVitalsMetricQuery } = useValues(webAnalyticsLogic)
const { setWebVitalsTab } = useActions(webAnalyticsLogic)
const { response, responseLoading } = useValues(logic)
// Manually handle loading state when loading to avoid showing stale data while refreshing
const webVitalsQueryResponse = responseLoading ? undefined : (response as WebVitalsQueryResponse | undefined)
const INP = useMemo(
() => getMetric(webVitalsQueryResponse?.results, props.query.source, 'INP', webVitalsPercentile),
[webVitalsQueryResponse, props.query.source, webVitalsPercentile]
)
const LCP = useMemo(
() => getMetric(webVitalsQueryResponse?.results, props.query.source, 'LCP', webVitalsPercentile),
[webVitalsQueryResponse, props.query.source, webVitalsPercentile]
)
const CLS = useMemo(
() => getMetric(webVitalsQueryResponse?.results, props.query.source, 'CLS', webVitalsPercentile),
[webVitalsQueryResponse, props.query.source, webVitalsPercentile]
)
const FCP = useMemo(
() => getMetric(webVitalsQueryResponse?.results, props.query.source, 'FCP', webVitalsPercentile),
[webVitalsQueryResponse, props.query.source, webVitalsPercentile]
)
return (
<div className="flex flex-col flex-1 gap-4">
<div className="flex flex-col gap-1">
<div className="grid gap-2 grid-cols-2 xl:grid-cols-4">
<WebVitalsTab
metric="INP"
value={INP}
isActive={webVitalsTab === 'INP'}
setTab={() => setWebVitalsTab('INP')}
isLoading={responseLoading}
/>
<WebVitalsTab
metric="LCP"
value={LCP}
isActive={webVitalsTab === 'LCP'}
setTab={() => setWebVitalsTab('LCP')}
isLoading={responseLoading}
/>
<WebVitalsTab
metric="FCP"
value={FCP}
isActive={webVitalsTab === 'FCP'}
setTab={() => setWebVitalsTab('FCP')}
isLoading={responseLoading}
/>
<WebVitalsTab
metric="CLS"
value={CLS}
isActive={webVitalsTab === 'CLS'}
setTab={() => setWebVitalsTab('CLS')}
isLoading={responseLoading}
/>
</div>
<span className="text-xs text-text-tertiary self-center sm:self-end">
Metrics above are from the last day in the selected time range.{' '}
<Link to="https://posthog.com/docs/web-analytics/web-vitals#web-vitals-dashboard" target="_blank">
Learn more in the Docs.
</Link>
</span>
</div>
<div className="flex flex-col sm:flex-row gap-2">
<WebVitalsContent
webVitalsQueryResponse={webVitalsQueryResponse}
querySource={props.query.source}
isLoading={responseLoading}
/>
<div className="flex flex-col flex-1 bg-surface-primary rounded border p-4">
<Query
key={webVitalsTab}
query={webVitalsMetricQuery}
readOnly
embedded
context={{ renderEmptyStateAsSkeleton: true }}
/>
<div className="flex w-full justify-end">
<LemonButton
to={urls.insightNew({ query: webVitalsMetricQuery })}
icon={<IconOpenInNew />}
size="small"
type="secondary"
onClick={() => {
void addProductIntentForCrossSell({
from: ProductKey.WEB_ANALYTICS,
to: ProductKey.PRODUCT_ANALYTICS,
intent_context: ProductIntentContext.WEB_VITALS_INSIGHT,
})
}}
>
Open as new Insight
</LemonButton>
</div>
</div>
</div>
</div>
)
}