: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
feat(insights): show data checks on the insight page (#119275)
Sam Penningtonfef51ec203dd
diff --git a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsxindex a06f90598b8b..4d9d4920eafe 100644--- a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx+++ b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx@@ -48,6 +48,7 @@ import {} from '~/types'import { ChartAlternatives } from 'products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives'+import { InsightDataChecks } from 'products/product_analytics/frontend/insights/dataChecks/InsightDataChecks'import { Funnel } from 'products/product_analytics/frontend/insights/funnels/Funnel'import { FunnelCanvasLabel } from 'products/product_analytics/frontend/insights/funnels/FunnelCanvasLabel'import { FunnelCorrelation } from 'products/product_analytics/frontend/insights/funnels/FunnelCorrelation/FunnelCorrelation'@@ -553,6 +554,7 @@ export function InsightVizDisplay({}const showComputationMetadata = !disableLastComputation || !!samplingFactor+ const showsDataChecks = !!context?.showDataChecks && !inSharedMode// Web Analytics insights don't use themes, so allow them to render without waiting for theme to loadif (!theme && activeView !== InsightType.WEB_ANALYTICS) {@@ -599,6 +601,7 @@ export function InsightVizDisplay({<div className="flex items-center gap-2">{isPaths && <PathCanvasLabel />}{isFunnels && <FunnelCanvasLabel />}+ {showsDataChecks && <InsightDataChecks insightProps={insightProps} />}</div></div>)}diff --git a/frontend/src/queries/types.ts b/frontend/src/queries/types.tsindex 871b1c3bdb97..bba7e5b26435 100644--- a/frontend/src/queries/types.ts+++ b/frontend/src/queries/types.ts@@ -28,6 +28,8 @@ export interface QueryContext<Q extends QuerySchema = QuerySchema> {showQueryEditor?: boolean/* Adds help and examples to the query editor component */showQueryHelp?: boolean+ /** Shows the result of the data checks next to the chart. For a surface where the person can change the query. */+ showDataChecks?: booleaninsightProps?: InsightLogicProps<Q>emptyStateHeading?: stringemptyStateDetail?: string | JSX.Elementdiff --git a/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsxnew file mode 100644index 000000000000..d7fb23640174--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx@@ -0,0 +1,103 @@+import { useActions, useValues } from 'kea'+import { useId } from 'react'++import { Button, Dot, Popover, PopoverContent, PopoverTrigger, Text } from '@posthog/quill'++import { humanFriendlyNumber } from 'lib/utils/numbers'+import { pluralize } from 'lib/utils/strings'++import type { InsightLogicProps } from '~/types'++import type { InsightDataChecksCoverageApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { DATA_CHECK_NAMES } from './dataCheckText'+import { InsightDataCheckRow } from './InsightDataCheckRow'+import { insightDataChecksLogic } from './insightDataChecksLogic'+import type { InsightDataChecksStatus } from './insightDataChecksLogic'++function summary(status: InsightDataChecksStatus, coverage: InsightDataChecksCoverageApi | null | undefined): string {+ if (status === 'checking') {+ return 'Checking the events that this chart reads.'+ }+ if (status === 'failed') {+ return "The checks didn't finish. The chart's own numbers are not affected."+ }+ if (!coverage) {+ return 'No check could run on this chart.'+ }+ const days = pluralize(coverage.days_checked, 'day')+ if (coverage.events_checked === 0) {+ return `No events in the newest ${days} of this chart, so there is nothing to check.`+ }+ if (coverage.events_checked === null) {+ return `Checked the events from the newest ${days} of this chart.`+ }+ return `Checked ${humanFriendlyNumber(coverage.events_checked)} events from the newest ${days} of this chart.`+}++export interface DataChecksPopoverProps {+ insightProps: InsightLogicProps+ trigger: React.ReactElement+ children: React.ReactNode+}++export function DataChecksPopover({ insightProps, trigger, children }: DataChecksPopoverProps): JSX.Element {+ const logic = insightDataChecksLogic(insightProps)+ const { checks, checkedQuery, result, status } = useValues(logic)+ const { loadDataChecks, reportDataChecksViewed } = useActions(logic)+ const titleId = useId()++ return (+ <Popover+ onOpenChange={(open) => {+ if (open) {+ reportDataChecksViewed()+ }+ }}+ >+ <PopoverTrigger openOnHover delay={150} closeDelay={150} render={trigger}>+ {children}+ </PopoverTrigger>+ <PopoverContent+ align="end"+ className="w-80 gap-2"+ aria-labelledby={titleId}+ data-attr="insight-data-checks-list"+ >+ <Text id={titleId} size="sm" weight="medium">+ Checks we've run on this data+ </Text>+ <ul className="-mx-2 my-0 flex list-none flex-col p-0">+ {status === 'checking'+ ? Object.entries(DATA_CHECK_NAMES).map(([key, name]) => (+ <li key={key} className="flex items-start gap-2 px-2 py-1">+ <span className="flex h-5 w-4 shrink-0 items-center justify-center">+ <Dot variant="default" pulse />+ </span>+ <Text size={null} variant="muted" render={<span />}>+ {name}+ </Text>+ </li>+ ))+ : checks.map((check) => (+ <InsightDataCheckRow key={check.key} insightProps={insightProps} check={check} />+ ))}+ </ul>+ <Text size="xs" variant="muted">+ {summary(status, result?.coverage)}+ </Text>+ {status === 'failed' && (+ <Button+ variant="outline"+ size="sm"+ className="self-start"+ onClick={() => checkedQuery && loadDataChecks(checkedQuery)}+ data-attr="insight-data-checks-retry"+ >+ Try again+ </Button>+ )}+ </PopoverContent>+ </Popover>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsx b/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsxnew file mode 100644index 000000000000..9258eb063df0--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsx@@ -0,0 +1,130 @@+import { useActions, useValues } from 'kea'++import { IconCheck, IconWarning } from '@posthog/icons'+import { Button, Dot, Text, Tooltip, TooltipContent, TooltipTrigger } from '@posthog/quill'++import { Link } from 'lib/lemon-ui/Link'+import { urls } from 'scenes/urls'++import type { InsightLogicProps } from '~/types'++import type {+ InsightDataCheckApi,+ InsightDataCheckKeyEnumApi,+ InsightDataCheckLevelEnumApi,+} from 'products/product_analytics/frontend/generated/api.schemas'++import { dataCheckText } from './dataCheckText'+import { insightDataChecksLogic } from './insightDataChecksLogic'++const CHECK_DESCRIPTIONS: Record<InsightDataCheckKeyEnumApi, string> = {+ internal_users:+ "We count the events that your project's internal and test users filter would remove. Activity from your own team makes the numbers look higher than real usage.",+ local_development:+ 'We look for events sent from localhost. They come from people building and testing the app, not from real users.',+ bot_traffic:+ 'We read the browser details on each event and count the events from known bots, crawlers and AI agents.',+ duplicate_events:+ 'We count the events where one person sent the same event from the same page twice in one second. That usually points to a tracking bug.',+}++const LEVEL_LABELS: Record<InsightDataCheckLevelEnumApi, string> = {+ passed: 'Passed',+ warning: 'Warning',+ problem: 'Problem',+ skipped: 'Not checked',+}++function LevelIcon({ level }: { level: InsightDataCheckApi['level'] }): JSX.Element {+ if (level === 'passed') {+ return <IconCheck className="size-4 text-(--success-foreground)" />+ }+ if (level === 'warning' || level === 'problem') {+ return (+ <IconWarning+ className={+ level === 'problem'+ ? 'size-4 text-(--destructive-foreground)'+ : 'size-4 text-(--warning-foreground)'+ }+ />+ )+ }+ return <Dot variant="default" />+}++export function InsightDataCheckRow({+ insightProps,+ check,+}: {+ insightProps: InsightLogicProps+ check: InsightDataCheckApi+}): JSX.Element {+ const { filtersTestAccounts } = useValues(insightDataChecksLogic(insightProps))+ const { filterOutInternalUsers } = useActions(insightDataChecksLogic(insightProps))+ const text = dataCheckText(check, filtersTestAccounts)+ const isFinding = check.level === 'warning' || check.level === 'problem'++ return (+ <li data-attr="insight-data-check" data-check={check.key} data-level={check.level}>+ <Tooltip>+ <TooltipTrigger+ render={+ <button+ type="button"+ data-quill+ className="flex w-full cursor-default items-start gap-2 rounded-md border-0 bg-transparent px-2 py-1 text-start hover:bg-(--fill-hover) focus-visible:outline-2 focus-visible:outline-(--ring)"+ />+ }+ >+ <span className="flex h-5 w-4 shrink-0 items-center justify-center">+ <LevelIcon level={check.level} />+ </span>+ {/* No size of its own, so the row takes the text size that quill gives a popover. */}+ <Text+ size={null}+ weight={isFinding ? 'medium' : undefined}+ variant={check.level === 'skipped' ? 'muted' : 'default'}+ render={<span />}+ >+ {text.result}+ <span className="sr-only">{`: ${LEVEL_LABELS[check.level]}`}</span>+ </Text>+ </TooltipTrigger>+ <TooltipContent side="right">{CHECK_DESCRIPTIONS[check.key]}</TooltipContent>+ </Tooltip>+ {text.fix && (+ <div className="flex flex-col items-start gap-1.5 ps-8 pe-2 pt-0.5 pb-2">+ <Text size="xs" variant="muted">+ {text.fix}+ </Text>+ {check.action === 'filter_internal_users' && (+ <Button+ variant="link"+ size="sm"+ onClick={() => filterOutInternalUsers()}+ data-attr="insight-data-check-filter-internal-users"+ >+ Filter them out+ </Button>+ )}+ {check.action === 'open_filter_settings' && (+ <Button+ variant="link"+ size="sm"+ render={+ <Link+ to={urls.settings('environment-customization', 'internal-user-filtering')}+ target="_blank"+ />+ }+ data-attr="insight-data-check-filter-settings"+ >+ Open filter settings+ </Button>+ )}+ </div>+ )}+ </li>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/InsightDataChecks.tsx b/products/product_analytics/frontend/insights/dataChecks/InsightDataChecks.tsxnew file mode 100644index 000000000000..a50db1a87817--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/InsightDataChecks.tsx@@ -0,0 +1,94 @@+import { useValues } from 'kea'++import { IconCheck, IconWarning } from '@posthog/icons'+import { Dot, Spinner, cn } from '@posthog/quill'++import { pluralize } from 'lib/utils/strings'++import type { InsightLogicProps } from '~/types'++import { DataChecksPopover } from './DataChecksPopover'+import { insightDataChecksLogic } from './insightDataChecksLogic'+import type { InsightDataChecksStatus } from './insightDataChecksLogic'++const COLORS: Partial<Record<InsightDataChecksStatus, string>> = {+ passed: 'border-(--success-foreground) bg-(--success) text-(--success-foreground)',+ warning: 'border-(--warning-foreground) bg-(--warning) text-(--warning-foreground)',+ problem: 'border-(--destructive-foreground) bg-(--destructive) text-(--destructive-foreground)',+}++function statusLabel(status: InsightDataChecksStatus, findingCount: number): string {+ switch (status) {+ case 'checking':+ return 'Checking data'+ case 'failed':+ return "Data checks didn't finish"+ case 'no_events':+ return 'No data to check'+ case 'inconclusive':+ return 'Data not checked'+ case 'warning':+ return pluralize(findingCount, 'data warning')+ case 'problem':+ return pluralize(findingCount, 'data issue')+ default:+ return 'No data issues found'+ }+}++export function InsightDataChecks({ insightProps }: { insightProps: InsightLogicProps }): JSX.Element | null {+ const { findings, labelOpen, status } = useValues(insightDataChecksLogic(insightProps))++ if (status === 'unavailable' || status === 'unsupported') {+ return null+ }++ return (+ <DataChecksPopover+ insightProps={insightProps}+ trigger={+ <button+ type="button"+ data-quill+ className={cn(+ 'group/checks inline-flex cursor-pointer items-center rounded-full border-2 p-0',+ 'transition-colors duration-700 motion-reduce:duration-0',+ 'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--ring)',+ COLORS[status] ?? 'border-(--border) bg-(--muted) text-(--muted-foreground)'+ )}+ data-attr="insight-data-checks"+ data-status={status}+ />+ }+ >+ <span+ key={status}+ className="inline-flex size-5 shrink-0 items-center justify-center transition-opacity duration-500 starting:opacity-0"+ >+ {status === 'passed' ? (+ <IconCheck className="size-4" />+ ) : status === 'checking' ? (+ <Spinner className="size-3.5" />+ ) : status === 'no_events' || status === 'inconclusive' ? (+ <Dot variant="default" />+ ) : (+ <IconWarning className="size-4" />+ )}+ </span>+ <span+ className={cn(+ 'overflow-hidden text-xs font-medium whitespace-nowrap',+ 'transition-[max-width,opacity] duration-700 ease-out motion-reduce:duration-0',+ labelOpen ? 'max-w-40 opacity-100' : 'max-w-0 opacity-0',+ 'group-hover/checks:max-w-40 group-hover/checks:opacity-100',+ 'group-focus-visible/checks:max-w-40 group-focus-visible/checks:opacity-100',+ 'group-data-popup-open/checks:max-w-40 group-data-popup-open/checks:opacity-100'+ )}+ >+ <span key={status} className="pe-2.5 transition-opacity duration-500 starting:opacity-0">+ {statusLabel(status, findings.length)}+ </span>+ </span>+ </DataChecksPopover>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.ts b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.tsnew file mode 100644index 000000000000..37b42ce454f9--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.ts@@ -0,0 +1,66 @@+import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { dataCheckText } from './dataCheckText'++const passed: InsightDataCheckApi = {+ key: 'bot_traffic',+ level: 'passed',+ skip_reason: null,+ method: 'sampled',+ events: 0,+ share: 0,+ scaled: false,+ action: null,+ top_events: [],+ top_hosts: [],+ top_bots: [],+}++describe('dataCheckText', () => {+ it.each<[string, Partial<InsightDataCheckApi>, boolean, string, string | null]>([+ [+ 'a skipped check with its reason',+ { level: 'skipped', skip_reason: 'read_limit', events: null, share: null },+ false,+ 'Not checked, because it would read more data than it is allowed',+ null,+ ],+ [+ 'a passed check that found a few events, from a sample',+ { events: 1234, share: 0.001, scaled: true },+ false,+ 'Almost no bots or crawlers found (about 1,234 events)',+ null,+ ],+ [+ 'internal users on an insight that filters them out',+ { key: 'internal_users', method: 'counted' },+ true,+ 'Internal and test users are filtered out',+ null,+ ],+ [+ 'a finding from a sample, without the filter on',+ { level: 'problem', events: 4000, share: 0.4, scaled: true, action: 'open_filter_settings' },+ false,+ 'About 40% of these events came from bots and crawlers',+ 'In the internal and test users filter, add "Traffic type is not Bot or AI Agent", and turn that filter on for this insight.',+ ],+ [+ 'a finding too small for a share, with the filter on',+ { key: 'local_development', level: 'warning', events: 60, share: 0.002, action: 'open_filter_settings' },+ true,+ '60 of these events came from local development',+ 'Send local traffic to a separate project, or exclude localhost in the internal and test users filter.',+ ],+ [+ 'internal users that turning the filter on removes',+ { key: 'internal_users', level: 'warning', events: 80, share: 0.08, action: 'filter_internal_users' },+ false,+ '8% of these events came from internal and test users',+ 'Turn on the internal and test users filter for this insight.',+ ],+ ])('words %s', (_name, check, filtersTestAccounts, result, fix) => {+ expect(dataCheckText({ ...passed, ...check }, filtersTestAccounts)).toMatchObject({ result, fix })+ })+})diff --git a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.ts b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.tsnew file mode 100644index 000000000000..cffad566647a--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.ts@@ -0,0 +1,104 @@+import { humanFriendlyNumber } from 'lib/utils/numbers'+import { capitalizeFirstLetter } from 'lib/utils/strings'++import type {+ InsightDataCheckApi,+ InsightDataCheckKeyEnumApi,+ InsightDataCheckSkipReasonEnumApi,+} from 'products/product_analytics/frontend/generated/api.schemas'++export interface DataCheckText {+ name: string+ result: string+ /** What to change. Null when the check found nothing to fix. */+ fix: string | null+}++export const DATA_CHECK_NAMES: Record<InsightDataCheckKeyEnumApi, string> = {+ internal_users: 'Internal and test users',+ local_development: 'Local development events',+ bot_traffic: 'Bot and crawler traffic',+ duplicate_events: 'Events sent twice',+}++// What the check looks for, as it reads after "No".+const LOOKS_FOR: Record<InsightDataCheckKeyEnumApi, string> = {+ internal_users: 'internal or test users',+ local_development: 'local development events',+ bot_traffic: 'bots or crawlers',+ duplicate_events: 'events sent twice',+}++const FOUND: Record<InsightDataCheckKeyEnumApi, string> = {+ internal_users: 'of these events came from internal and test users',+ local_development: 'of these events came from local development',+ bot_traffic: 'of these events came from bots and crawlers',+ duplicate_events: 'of these events were sent twice within one second',+}++const SKIP_REASONS: Record<InsightDataCheckSkipReasonEnumApi, string> = {+ too_many_events: 'the days checked hold too many events to count',+ read_limit: 'it would read more data than it is allowed',+ time_limit: 'it took longer than it is allowed',+ memory_limit: 'it needed more memory than it is allowed',+ too_few_sampled_events: 'the sample has too few events',+ filtered_by_traffic_type: 'this insight already filters on traffic type',+ hidden_property: 'a property it reads is hidden from you',+ invalid_filter: "the project's internal and test users filter cannot run",+}++function eventCount(check: InsightDataCheckApi): string {+ const count = humanFriendlyNumber(check.events ?? 0)+ return check.scaled ? `about ${count}` : count+}++function inTheFilter(change: string, filtersTestAccounts: boolean): string {+ return filtersTestAccounts ? `${change}.` : `${change}, and turn that filter on for this insight.`+}++function fixOf(check: InsightDataCheckApi, filtersTestAccounts: boolean): string {+ switch (check.key) {+ case 'internal_users':+ if (check.action === 'filter_internal_users') {+ return 'Turn on the internal and test users filter for this insight.'+ }+ return inTheFilter(+ "Change the internal and test users filter so that it removes your team's emails",+ filtersTestAccounts+ )+ case 'local_development':+ return inTheFilter(+ 'Send local traffic to a separate project, or exclude localhost in the internal and test users filter',+ filtersTestAccounts+ )+ case 'bot_traffic':+ return inTheFilter(+ 'In the internal and test users filter, add "Traffic type is not Bot or AI Agent"',+ filtersTestAccounts+ )+ case 'duplicate_events':+ return 'Start PostHog once in your app, and send each event from one place only.'+ }+}++export function dataCheckText(check: InsightDataCheckApi, filtersTestAccounts: boolean): DataCheckText {+ const name = DATA_CHECK_NAMES[check.key]+ if (check.level === 'skipped') {+ const reason = check.skip_reason ? SKIP_REASONS[check.skip_reason] : null+ return { name, result: reason ? `Not checked, because ${reason}` : 'Not checked', fix: null }+ }+ if (check.level === 'passed') {+ if (check.key === 'internal_users' && filtersTestAccounts) {+ return { name, result: 'Internal and test users are filtered out', fix: null }+ }+ const result = check.events+ ? `Almost no ${LOOKS_FOR[check.key]} found (${eventCount(check)} events)`+ : `No ${LOOKS_FOR[check.key]} found`+ return { name, result, fix: null }+ }+ const amount =+ check.share !== null && check.share >= 0.01+ ? `${check.scaled ? 'About ' : ''}${Math.round(check.share * 100)}%`+ : capitalizeFirstLetter(eventCount(check))+ return { name, result: `${amount} ${FOUND[check.key]}`, fix: fixOf(check, filtersTestAccounts) }+}diff --git a/products/product_analytics/frontend/insights/insightScene/InsightAsScene.tsx b/products/product_analytics/frontend/insights/insightScene/InsightAsScene.tsxindex 81e165074376..721eb4aeeb47 100644--- a/products/product_analytics/frontend/insights/insightScene/InsightAsScene.tsx+++ b/products/product_analytics/frontend/insights/insightScene/InsightAsScene.tsx@@ -1,5 +1,5 @@import clsx from 'clsx'-import { BindLogic, BuiltLogic, Logic, LogicWrapper, useActions, useValues } from 'kea'+import { BindLogic, BuiltLogic, Logic, LogicWrapper, useActions, useMountedLogic, useValues } from 'kea'import { AccessDenied } from 'lib/components/AccessDenied'import { useFileSystemLogView } from 'lib/hooks/useFileSystemLogView'@@ -17,6 +17,7 @@ import { Node } from '~/queries/schema/schema-general'import { containsHogQLQuery, isBIVisualizationNode, isDataVisualizationNode, isInsightVizNode } from '~/queries/utils'import { InsightShortId, ItemMode } from '~/types'+import { insightDataChecksLogic } from 'products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic'import { InsightHomeGuide } from 'products/product_analytics/frontend/insights/home/InsightHomeGuide'import { InsightModals } from 'products/product_analytics/frontend/insights/insightScene/InsightModals'import { SubscriptionSummaries } from 'products/subscriptions/frontend/components/SubscriptionSummaries/SubscriptionSummaries'@@ -64,6 +65,8 @@ export function InsightAsScene({ insightId, attachTo }: InsightAsSceneProps): JS// other logicsuseAttachedLogic(logic, attachTo) // insightLogic(insightProps)useAttachedLogic(insightDataLogic(insightProps), attachTo)+ // Mounted here and not in the mark, so a check result survives while the chart reloads and hides the mark.+ useMountedLogic(insightDataChecksLogic(insightProps))const actuallyShowQueryEditor = insightMode === ItemMode.Edit && showQueryEditor@@ -123,6 +126,7 @@ export function InsightAsScene({ insightId, attachTo }: InsightAsSceneProps): JSshowOpenEditorButton: false,showQueryEditor: actuallyShowQueryEditor,showQueryHelp: insightMode === ItemMode.Edit && !containsHogQLQuery(query),+ showDataChecks: true,insightProps,// Flag-gated inside the charts' shared useDateRangeZoom hook.onDateRangeZoom: zoomDateRange,