: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): let people act on insight data check findings (#120061)
Sam Penningtond348a8364593
diff --git a/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsxindex d7fb23640174..627aadb2f113 100644--- a/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx+++ b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx@@ -1,7 +1,7 @@import { useActions, useValues } from 'kea'import { useId } from 'react'-import { Button, Dot, Popover, PopoverContent, PopoverTrigger, Text } from '@posthog/quill'+import { Accordion, Button, Dot, Popover, PopoverContent, PopoverTrigger, Text } from '@posthog/quill'import { humanFriendlyNumber } from 'lib/utils/numbers'import { pluralize } from 'lib/utils/strings'@@ -43,7 +43,7 @@ export interface DataChecksPopoverProps {export function DataChecksPopover({ insightProps, trigger, children }: DataChecksPopoverProps): JSX.Element {const logic = insightDataChecksLogic(insightProps)- const { checks, checkedQuery, result, status } = useValues(logic)+ const { visibleChecks, checkedQuery, result, status } = useValues(logic)const { loadDataChecks, reportDataChecksViewed } = useActions(logic)const titleId = useId()@@ -60,29 +60,35 @@ export function DataChecksPopover({ insightProps, trigger, children }: DataCheck</PopoverTrigger><PopoverContentalign="end"- className="w-80 gap-2"+ className="w-96 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>+ {visibleChecks.length > 0 ? (+ <Accordion>+ {visibleChecks.map((check) => (+ <InsightDataCheckRow key={check.key} insightProps={insightProps} check={check} />+ ))}+ </Accordion>+ ) : (+ status === 'checking' && (+ <ul className="-mx-2 my-0 flex list-none flex-col p-0">+ {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>+ ))}+ </ul>+ )+ )}<Text size="xs" variant="muted">{summary(status, result?.coverage)}</Text>diff --git a/products/product_analytics/frontend/insights/dataChecks/DuplicateEventsDetails.tsx b/products/product_analytics/frontend/insights/dataChecks/DuplicateEventsDetails.tsxnew file mode 100644index 000000000000..559ca674e0cb--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/DuplicateEventsDetails.tsx@@ -0,0 +1,61 @@+import { useValues } from 'kea'++import { Button, Text } from '@posthog/quill'++import { LinkPrimitive } from 'lib/lemon-ui/Link'+import { urls } from 'scenes/urls'++import type { InsightLogicProps } from '~/types'++import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { repeatedEventsTextOf } from './dataCheckText'+import { FixWithPostHogAIButton } from './FixWithPostHogAIButton'+import { insightDataChecksLogic } from './insightDataChecksLogic'+import { repeatedEventsQueryOf } from './repeatedEventsQuery'++export function DuplicateEventsDetails({+ insightProps,+ check,+}: {+ insightProps: InsightLogicProps+ check: InsightDataCheckApi+}): JSX.Element {+ const { lastResult } = useValues(insightDataChecksLogic(insightProps))+ const coverage = lastResult?.coverage+ const text = repeatedEventsTextOf(check)+ const repeatedEventsUrl =+ coverage &&+ urls.sqlEditor({+ query: repeatedEventsQueryOf({+ events: check.top_events,+ dateFrom: coverage.checked_date_from,+ dateTo: coverage.checked_date_to,+ sampleRate: coverage.sample?.rate ?? 1,+ }),+ })+ return (+ <div className="flex flex-col items-start gap-1.5">+ <Text size="xs" variant="muted">+ {text.found}+ </Text>+ <Text size="xs" variant="muted">+ {text.causes}+ </Text>+ <div className="flex flex-wrap gap-2">+ {repeatedEventsUrl && (+ <Button+ variant="outline"+ size="sm"+ className="no-underline"+ render={<LinkPrimitive to={repeatedEventsUrl} target="_blank" />}+ data-attr="insight-data-check-see-repeated-events"+ >+ See these events+ </Button>+ )}+ <FixWithPostHogAIButton insightProps={insightProps} check={check} />+ </div>+ </div>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/ExcludeFromFilterButton.tsx b/products/product_analytics/frontend/insights/dataChecks/ExcludeFromFilterButton.tsxnew file mode 100644index 000000000000..85e205bdabb6--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/ExcludeFromFilterButton.tsx@@ -0,0 +1,108 @@+import { useActions, useValues } from 'kea'++import {+ AlertDialog,+ AlertDialogClose,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+ AlertDialogTrigger,+ Button,+} from '@posthog/quill'++import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'+import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'+import { TeamMembershipLevel } from 'lib/constants'+import { LinkPrimitive } from 'lib/lemon-ui/Link'+import { urls } from 'scenes/urls'++import { getFilterLabel } from '~/taxonomy/helpers'+import type { InsightLogicProps } from '~/types'++import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { insightDataChecksLogic } from './insightDataChecksLogic'++export function ExcludeFromFilterButton({+ insightProps,+ check,+ label,+}: {+ insightProps: InsightLogicProps+ check: InsightDataCheckApi+ label: string+}): JSX.Element {+ const logic = insightDataChecksLogic(insightProps)+ const { currentTeamLoading } = useValues(logic)+ const { addFilterRule } = useActions(logic)+ const restrictedReason = useRestrictedArea({+ scope: RestrictionScope.Project,+ minimumAccessLevel: TeamMembershipLevel.Admin,+ })+ const rule = check.filter_to_add++ if (!rule) {+ return (+ <Button+ variant="outline"+ size="sm"+ className="no-underline"+ render={+ <LinkPrimitive+ to={urls.settings('environment-customization', 'internal-user-filtering')}+ target="_blank"+ />+ }+ data-attr="insight-data-check-filter-settings"+ >+ Edit filter+ </Button>+ )+ }++ const property = getFilterLabel(+ rule.key,+ rule.type === 'person' ? TaxonomicFilterGroupType.PersonProperties : TaxonomicFilterGroupType.EventProperties+ )+ return (+ <AlertDialog>+ <AlertDialogTrigger+ render={+ <Button+ variant="outline"+ size="sm"+ loading={currentTeamLoading}+ disabledReason={restrictedReason}+ data-attr="insight-data-check-add-filter-rule"+ />+ }+ >+ {label}+ </AlertDialogTrigger>+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>Add a rule to your internal and test users filter?</AlertDialogTitle>+ <AlertDialogDescription>+ {`Every insight that filters out internal and test users will also leave out events where ${property} is one of these:`}+ </AlertDialogDescription>+ </AlertDialogHeader>+ <ul className="m-0 flex max-h-48 flex-col gap-1 overflow-y-auto ps-4 text-xs">+ {rule.value.map((value) => (+ <li key={value}>{value}</li>+ ))}+ </ul>+ <AlertDialogFooter>+ <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>+ <AlertDialogClose+ render={<Button variant="primary" onClick={() => addFilterRule(check)} />}+ data-attr="insight-data-check-add-filter-rule-confirm"+ >+ Add rule+ </AlertDialogClose>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/FilterCheckDetails.tsx b/products/product_analytics/frontend/insights/dataChecks/FilterCheckDetails.tsxnew file mode 100644index 000000000000..11f35339be92--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/FilterCheckDetails.tsx@@ -0,0 +1,67 @@+import { useActions, useValues } from 'kea'++import { Text } from '@posthog/quill'++import { TestAccountFilterSwitch } from 'lib/components/TestAccountFiltersSwitch'++import type { InsightLogicProps } from '~/types'++import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { filterCheckParts } from './dataCheckText'+import { ExcludeFromFilterButton } from './ExcludeFromFilterButton'+import { FixWithPostHogAIButton } from './FixWithPostHogAIButton'+import { insightDataChecksLogic } from './insightDataChecksLogic'+import { NotCaughtByFilter } from './NotCaughtByFilter'++export function FilterCheckDetails({+ insightProps,+ check,+}: {+ insightProps: InsightLogicProps+ check: InsightDataCheckApi+}): JSX.Element {+ const logic = insightDataChecksLogic(insightProps)+ const { filtersTestAccounts, filterRemovesBots, queryFiltersTestAccounts } = useValues(logic)+ const { setFiltersTestAccounts } = useActions(logic)+ const { caught, missed } = filterCheckParts(check, filtersTestAccounts, filterRemovesBots)++ return (+ <div className="flex w-full flex-col gap-3">+ <div className="flex flex-col items-start gap-1.5">+ {caught && (+ <Text size="xs" variant="muted">+ {caught}+ </Text>+ )}+ <TestAccountFilterSwitch+ // The insight editor renders this switch too, so a shared id would point its label at the wrong one.+ id="insight-data-check-test-account-filter"+ size="small"+ checked={queryFiltersTestAccounts}+ onChange={(checked) => setFiltersTestAccounts(checked, check.key)}+ settingsLinkIcon="new-tab"+ data-attr="insight-data-check-filter-internal-users"+ />+ </div>+ {missed && (+ <div className="flex flex-col items-start gap-1.5">+ <NotCaughtByFilter+ count={missed.count}+ noun={missed.noun}+ examples={missed.examples}+ fallback={missed.fallback}+ />+ <div className="flex flex-wrap gap-2">+ <ExcludeFromFilterButton+ insightProps={insightProps}+ check={check}+ label={missed.excludeLabel}+ />+ <FixWithPostHogAIButton insightProps={insightProps} check={check} />+ </div>+ </div>+ )}+ </div>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/FixWithPostHogAIButton.tsx b/products/product_analytics/frontend/insights/dataChecks/FixWithPostHogAIButton.tsxnew file mode 100644index 000000000000..b44e679313c7--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/FixWithPostHogAIButton.tsx@@ -0,0 +1,34 @@+import { useActions, useValues } from 'kea'++import { IconSparkles } from '@posthog/icons'+import { Button } from '@posthog/quill'++import type { InsightLogicProps } from '~/types'++import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'++import { insightDataChecksLogic } from './insightDataChecksLogic'++export function FixWithPostHogAIButton({+ insightProps,+ check,+}: {+ insightProps: InsightLogicProps+ check: InsightDataCheckApi+}): JSX.Element {+ const logic = insightDataChecksLogic(insightProps)+ const { postHogAIOpen } = useValues(logic)+ const { fixWithPostHogAI } = useActions(logic)+ return (+ <Button+ variant="outline"+ size="sm"+ disabledReason={postHogAIOpen ? 'PostHog AI is already open' : undefined}+ onClick={() => fixWithPostHogAI(check)}+ data-attr="insight-data-check-fix-with-ai"+ >+ <IconSparkles />+ {check.key === 'duplicate_events' ? 'Investigate with PostHog AI' : 'Fix with PostHog AI'}+ </Button>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsx b/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsxindex 9258eb063df0..63892357b85e 100644--- a/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsx+++ b/products/product_analytics/frontend/insights/dataChecks/InsightDataCheckRow.tsx@@ -1,33 +1,20 @@-import { useActions, useValues } from 'kea'+import { 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 { AccordionContent, AccordionItem, AccordionTrigger, Dot, Text } from '@posthog/quill'import type { InsightLogicProps } from '~/types'import type {InsightDataCheckApi,- InsightDataCheckKeyEnumApi,InsightDataCheckLevelEnumApi,} from 'products/product_analytics/frontend/generated/api.schemas'-import { dataCheckText } from './dataCheckText'+import { FILTER_CHECK_KEYS, dataCheckText, isFinding } from './dataCheckText'+import { DuplicateEventsDetails } from './DuplicateEventsDetails'+import { FilterCheckDetails } from './FilterCheckDetails'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',@@ -35,15 +22,15 @@ const LEVEL_LABELS: Record<InsightDataCheckLevelEnumApi, string> = {skipped: 'Not checked',}-function LevelIcon({ level }: { level: InsightDataCheckApi['level'] }): JSX.Element {- if (level === 'passed') {+function LevelIcon({ check }: { check: InsightDataCheckApi }): JSX.Element {+ if (check.level === 'passed') {return <IconCheck className="size-4 text-(--success-foreground)" />}- if (level === 'warning' || level === 'problem') {+ if (isFinding(check)) {return (<IconWarningclassName={- level === 'problem'+ check.level === 'problem'? 'size-4 text-(--destructive-foreground)': 'size-4 text-(--warning-foreground)'}@@ -60,71 +47,55 @@ export function InsightDataCheckRow({insightProps: InsightLogicPropscheck: InsightDataCheckApi}): JSX.Element {- const { filtersTestAccounts } = useValues(insightDataChecksLogic(insightProps))- const { filterOutInternalUsers } = useActions(insightDataChecksLogic(insightProps))+ const { filtersTestAccounts, status } = useValues(insightDataChecksLogic(insightProps))const text = dataCheckText(check, filtersTestAccounts)- const isFinding = check.level === 'warning' || check.level === 'problem'+ const dependsOnFilter = FILTER_CHECK_KEYS.includes(check.key)+ const foundRepeats = check.key === 'duplicate_events' && isFinding(check)+ const hasDetails = dependsOnFilter || foundRepeats- 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)"- />- }+ const header = (+ <span className="flex items-start gap-2">+ <span className="flex h-5 w-4 shrink-0 items-center justify-center">+ <LevelIcon check={check} />+ </span>+ <span className="flex flex-col">+ {/* No size of its own, so the name takes the text size that quill gives a popover. */}+ <Text size={null} weight="medium" render={<span />}>+ {text.name}+ <span className="sr-only">{`: ${LEVEL_LABELS[check.level]}`}</span>+ </Text>+ <Text+ size="xs"+ variant="muted"+ className={status === 'checking' ? 'animate-pulse motion-reduce:animate-none' : undefined}+ render={<span />}>- <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>+ {text.result}+ </Text>+ </span>+ </span>+ )++ if (!hasDetails) {+ return (+ <div+ data-attr="insight-data-check"+ data-check={check.key}+ data-level={check.level}+ className="border-b border-(--border) px-2 py-1 last:border-b-0"+ >+ {header}+ </div>+ )+ }++ return (+ <AccordionItem value={check.key} data-attr="insight-data-check" data-check={check.key} data-level={check.level}>+ <AccordionTrigger className="px-2 py-1 font-normal">{header}</AccordionTrigger>+ <AccordionContent className="flex flex-col items-start gap-2 ps-6">+ {foundRepeats && <DuplicateEventsDetails insightProps={insightProps} check={check} />}+ {dependsOnFilter && <FilterCheckDetails insightProps={insightProps} check={check} />}+ </AccordionContent>+ </AccordionItem>)}diff --git a/products/product_analytics/frontend/insights/dataChecks/NotCaughtByFilter.tsx b/products/product_analytics/frontend/insights/dataChecks/NotCaughtByFilter.tsxnew file mode 100644index 000000000000..be21fbaa4c29--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/NotCaughtByFilter.tsx@@ -0,0 +1,48 @@+import { Text, Tooltip, TooltipContent, TooltipTrigger } from '@posthog/quill'++import { pluralize } from 'lib/utils/strings'++export function NotCaughtByFilter({+ count,+ noun,+ examples,+ fallback,+}: {+ count: number+ noun: string+ examples: string[]+ /** The subject when no example can be named. */+ fallback: string+}): JSX.Element {+ const others = Math.max(count - examples.length, 0)+ return (+ <Text size="xs" variant="muted">+ {count > 0 && examples.length > 0 ? (+ <Tooltip>+ <TooltipTrigger+ render={+ <button+ type="button"+ className="cursor-default border-0 bg-transparent p-0 text-inherit underline decoration-dotted underline-offset-2"+ data-attr={`insight-data-check-${noun}-examples`}+ />+ }+ >+ {pluralize(count, noun)}+ </TooltipTrigger>+ <TooltipContent>+ <span className="flex flex-col">+ {examples.map((example) => (+ <span key={example}>{example}</span>+ ))}+ {others > 0 && <span>{`and ${pluralize(others, 'other')}`}</span>}+ </span>+ </TooltipContent>+ </Tooltip>+ ) : (+ <span>{fallback}</span>+ )}+ <span>{` ${count === 1 ? "isn't" : "aren't"} caught by your internal and test users filter.`}</span>+ </Text>+ )+}diff --git a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.ts b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.tsindex 829aa9ea47b4..c524f5f0173b 100644--- a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.ts+++ b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.test.ts@@ -1,6 +1,7 @@import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'-import { dataCheckText } from './dataCheckText'+import { dataCheckText, filterCheckParts, removesBotTraffic, repeatedEventsTextOf } from './dataCheckText'+import type { FilterCheckParts } from './dataCheckText'const passed: InsightDataCheckApi = {key: 'bot_traffic',@@ -23,51 +24,139 @@ const passed: InsightDataCheckApi = {filter_to_add: null,}+const BOT_RULE = { key: '$virt_traffic_type', type: 'event', operator: 'is_not', value: ['AI Agent', 'Bot'] }+describe('dataCheckText', () => {- it.each<[string, Partial<InsightDataCheckApi>, boolean, string, string | null]>([+ it.each<[string, Partial<InsightDataCheckApi>, boolean, string]>([['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,+ 'Almost none (about 1,234 events)',],['internal users on an insight that filters them out',{ key: 'internal_users', method: 'counted' },true,- 'Internal and test users are filtered out',- null,+ 'Filtered out',+ ],+ [+ 'a finding from a sample',+ { level: 'problem', events: 4000, share: 0.4, scaled: true },+ false,+ 'About 40% of these events',],[- 'a finding from a sample, without the filter on',- { level: 'problem', events: 4000, share: 0.4, scaled: true, action: 'open_filter_settings' },+ 'a finding too small for a share',+ { key: 'local_development', level: 'warning', method: 'counted', events: 60, share: 0.001 },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.',+ '60 of these events',],+ ])('words the result of %s', (_name, check, filtersTestAccounts, expected) => {+ expect(dataCheckText({ ...passed, ...check }, filtersTestAccounts).result).toEqual(expected)+ })++ it.each<[string, Partial<InsightDataCheckApi>, boolean, boolean, FilterCheckParts]>([[- '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' },+ 'internal users the filter catches while the insight leaves it off, and team emails it misses',+ {+ key: 'internal_users',+ level: 'problem',+ events: 810,+ share: 0.81,+ removed_by_filter: 620,+ missed_by_filter: 190,+ top_emails: ['[email protected]', '[email protected]'],+ distinct_emails: 14,+ filter_to_add: { key: 'email', type: 'person', operator: 'is_not', value: ['[email protected]'] },+ },+ false,+ false,+ {+ caught: "Your filter catches 62% of these events, but it's off for this insight.",+ missed: {+ count: 14,+ noun: 'user',+ examples: ['[email protected]', '[email protected]'],+ fallback: "Your team's emails",+ excludeLabel: 'Exclude these users',+ },+ },+ ],+ [+ 'bots a traffic type rule catches while the insight leaves the filter off',+ {+ level: 'warning',+ events: 150,+ share: 0.14,+ top_bots: ['Googlebot'],+ distinct_bots: 1,+ filter_to_add: BOT_RULE,+ },+ false,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.',+ { caught: "Your filter catches this bot traffic, but it's off for this insight.", missed: null },],[- 'internal users that turning the filter on removes',- { key: 'internal_users', level: 'warning', events: 80, share: 0.08, action: 'filter_internal_users' },+ 'bots that get past a filter with no traffic type rule',+ {+ level: 'warning',+ events: 150,+ share: 0.14,+ top_bots: ['Googlebot', 'Ahrefs'],+ distinct_bots: 5,+ filter_to_add: BOT_RULE,+ },+ true,false,- '8% of these events came from internal and test users',- 'Turn on the internal and test users filter for this insight.',+ {+ caught: null,+ missed: {+ count: 5,+ noun: 'bot',+ examples: ['Googlebot', 'Ahrefs'],+ fallback: 'Bot events',+ excludeLabel: 'Exclude these bots',+ },+ },+ ],+ ['a check that found nothing', { key: 'local_development' }, true, false, { caught: null, missed: null }],+ ])('splits %s', (_name, check, filtersTestAccounts, filterRemovesBots, expected) => {+ expect(filterCheckParts({ ...passed, ...check }, filtersTestAccounts, filterRemovesBots)).toEqual(expected)+ })++ it.each<[string[], string]>([+ [['$pageview', 'signed_up'], 'Pageview and signed_up events are sent twice'],+ [[], 'Some events are sent twice'],+ ])('names the repeated events %j by their labels', (events, expected) => {+ expect(repeatedEventsTextOf({ ...passed, key: 'duplicate_events', top_events: events }).found).toContain(+ expected+ )+ })++ const trafficRule = (operator: string, value: string | string[]): Record<string, unknown> => ({+ key: '$virt_traffic_type',+ type: 'event',+ operator,+ value,+ })++ it.each<[string, Record<string, unknown>[], boolean]>([+ ['one rule that excludes both types', [trafficRule('is_not', ['Bot', 'AI Agent'])], true],+ [+ 'two rules that each exclude one type',+ [trafficRule('is_not', 'Bot'), trafficRule('is_not', ['AI Agent'])],+ true,],- ])('words %s', (_name, check, filtersTestAccounts, result, fix) => {- expect(dataCheckText({ ...passed, ...check }, filtersTestAccounts)).toMatchObject({ result, fix })+ ['a rule that excludes only AI agents', [trafficRule('is_not', ['AI Agent'])], false],+ ['a rule that keeps only bots', [trafficRule('exact', ['Bot', 'AI Agent'])], false],+ ])('knows whether the filter removes bot traffic with %s', (_name, rules, expected) => {+ expect(removesBotTraffic(rules)).toBe(expected)})})diff --git a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.ts b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.tsindex cffad566647a..01345690c110 100644--- a/products/product_analytics/frontend/insights/dataChecks/dataCheckText.ts+++ b/products/product_analytics/frontend/insights/dataChecks/dataCheckText.ts@@ -1,5 +1,8 @@-import { humanFriendlyNumber } from 'lib/utils/numbers'-import { capitalizeFirstLetter } from 'lib/utils/strings'+import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'+import { humanFriendlyNumber, percentage } from 'lib/utils/numbers'+import { capitalizeFirstLetter, humanList, pluralize } from 'lib/utils/strings'++import { getFilterLabel } from '~/taxonomy/helpers'import type {InsightDataCheckApi,@@ -7,35 +10,43 @@ import type {InsightDataCheckSkipReasonEnumApi,} from 'products/product_analytics/frontend/generated/api.schemas'+import { REPEAT_WINDOW_MILLISECONDS } from './repeatedEventsQuery'+export interface DataCheckText {+ /** The same whatever the result, so a row reads as the check it is. */name: string+ /** The result, short enough to sit under the name. */result: string- /** What to change. Null when the check found nothing to fix. */- fix: string | null}+const TRAFFIC_TYPE_PROPERTY = '$virt_traffic_type'+// The traffic types the bot check counts, as the backend's CRAWLER_TRAFFIC_TYPES lists them.+const CRAWLER_TRAFFIC_TYPES = ['Bot', 'AI Agent']++export function removesBotTraffic(+ rules: readonly { key?: string; type?: string; operator?: unknown; value?: unknown }[]+): boolean {+ // The filter's rules all apply, so values excluded by separate rules add up.+ const excluded = rules+ .filter((rule) => rule.key === TRAFFIC_TYPE_PROPERTY && rule.type === 'event' && rule.operator === 'is_not')+ .flatMap((rule) => (Array.isArray(rule.value) ? rule.value : [rule.value]))+ return CRAWLER_TRAFFIC_TYPES.every((type) => excluded.includes(type))+}++/** The checks that the project's internal and test users filter answers. */+export const FILTER_CHECK_KEYS: readonly InsightDataCheckKeyEnumApi[] = [+ 'internal_users',+ 'local_development',+ 'bot_traffic',+]+export const DATA_CHECK_NAMES: Record<InsightDataCheckKeyEnumApi, string> = {internal_users: 'Internal and test users',- local_development: 'Local development events',+ local_development: 'Localhost 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',@@ -52,53 +63,165 @@ function eventCount(check: InsightDataCheckApi): string {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.`+// Below 1% a share rounds to 0%, so the text gives a count instead.+function percentOf(share: number | null): string | null {+ return share !== null && share >= 0.01 ? percentage(share, 0) : null+}++function shareOf(check: InsightDataCheckApi, part: number): string | null {+ return percentOf(check.share !== null && check.events ? (check.share * part) / check.events : null)+}++export interface FilterCheckParts {+ /** Set when the project's filter catches these events but the insight leaves the filter off. */+ caught: string | null+ missed: { count: number; noun: string; examples: string[]; fallback: string; excludeLabel: string } | null+}++const NOTHING: FilterCheckParts = { caught: null, missed: null }++export function isFinding(check: InsightDataCheckApi): boolean {+ return check.level === 'warning' || check.level === 'problem'+}++function internalUsersParts(check: InsightDataCheckApi, filtersTestAccounts: boolean): FilterCheckParts {+ const removed = check.removed_by_filter+ const removedPercent = removed ? shareOf(check, removed) : null+ const caughtAmount = removedPercent ? `${removedPercent} of these events` : pluralize(removed ?? 0, 'event')+ return {+ caught:+ removed && !filtersTestAccounts+ ? `Your filter catches ${caughtAmount}, but it's off for this insight.`+ : null,+ missed: check.missed_by_filter+ ? {+ count: check.distinct_emails ?? check.top_emails.length,+ noun: 'user',+ examples: check.top_emails,+ fallback: "Your team's emails",+ excludeLabel: 'Exclude these users',+ }+ : null,+ }+}++function localHostParts(check: InsightDataCheckApi): FilterCheckParts {+ if (!isFinding(check) || !check.filter_to_add) {+ return NOTHING+ }+ return {+ caught: null,+ missed: {+ count: check.distinct_hosts ?? check.top_hosts.length,+ noun: 'host',+ examples: check.top_hosts,+ fallback: 'Localhost events',+ excludeLabel: 'Exclude these hosts',+ },+ }}-function fixOf(check: InsightDataCheckApi, filtersTestAccounts: boolean): string {+function botTrafficParts(+ check: InsightDataCheckApi,+ filtersTestAccounts: boolean,+ filterRemovesBots: boolean+): FilterCheckParts {+ if (!isFinding(check)) {+ return NOTHING+ }+ if (filterRemovesBots && !filtersTestAccounts) {+ return { caught: "Your filter catches this bot traffic, but it's off for this insight.", missed: null }+ }+ return {+ caught: null,+ missed: {+ count: check.distinct_bots ?? check.top_bots.length,+ noun: 'bot',+ examples: check.top_bots,+ fallback: 'Bot events',+ excludeLabel: 'Exclude these bots',+ },+ }+}++export function filterCheckParts(+ check: InsightDataCheckApi,+ filtersTestAccounts: boolean,+ filterRemovesBots: boolean+): FilterCheckParts {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+ return internalUsersParts(check, filtersTestAccounts)+ case 'local_development':+ return localHostParts(check)+ case 'bot_traffic':+ return botTrafficParts(check, filtersTestAccounts, filterRemovesBots)+ default:+ return NOTHING+ }+}++export function repeatedEventsTextOf(check: InsightDataCheckApi): { found: string; causes: string } {+ const names = humanList(check.top_events.map((event) => getFilterLabel(event, TaxonomicFilterGroupType.Events)))+ return {+ found: `${names ? `${names} events are` : 'Some events are'} sent twice by the same person, less than ${REPEAT_WINDOW_MILLISECONDS} ms apart.`,+ causes:+ "This could be because your event is set up twice, you're re-firing an event that's sent automatically, " ++ "or there's a bug in your code. It could even be that PostHog is initialized twice.",+ }+}++// The prompt holds only counts. Event names, hosts and emails come from captured events, which anyone with the+// project token can send, so PostHog AI reads them through its tools instead of as part of the request.+export function postHogAIPromptOf(check: InsightDataCheckApi): string {+ const percent = percentOf(check.share)+ const share = percent ? ` (${percent} of them)` : ''+ switch (check.key) {+ case 'duplicate_events':+ return (+ `The data checks on this insight found ${eventCount(check)} events${share} sent twice by the same ` ++ `person less than ${REPEAT_WINDOW_MILLISECONDS} ms apart. Find which events they are and why, for example the event being set ` ++ 'up twice, my code re-firing an event PostHog sends automatically, a bug in my code, or PostHog being ' ++ 'initialized twice, and tell me how to fix it.')case 'local_development':- return inTheFilter(- 'Send local traffic to a separate project, or exclude localhost in the internal and test users filter',- filtersTestAccounts+ return (+ `The data checks on this insight found ${eventCount(check)} events from local hosts that my project's ` ++ 'internal and test users filter keeps. Help me work out whether they come from people building the ' ++ 'app, and if they do, change the filter so it removes those hosts.')case 'bot_traffic':- return inTheFilter(- 'In the internal and test users filter, add "Traffic type is not Bot or AI Agent"',- filtersTestAccounts+ return (+ `The data checks on this insight found ${eventCount(check)} events${share} from bots and crawlers that ` ++ "my project's internal and test users filter keeps. Change the filter so it removes bot and AI agent " ++ 'traffic.'+ )+ case 'internal_users':+ return (+ `The data checks on this insight found ${pluralize(check.distinct_emails ?? 0, 'team email')} that my ` ++ "project's internal and test users filter doesn't catch. Change the filter so it removes my team's " ++ 'internal and test users without removing real users.')- case 'duplicate_events':- return 'Start PostHog once in your app, and send each event from one place only.'}}+function skippedResult(check: InsightDataCheckApi): string {+ const reason = check.skip_reason ? SKIP_REASONS[check.skip_reason] : null+ return reason ? `Not checked, because ${reason}` : 'Not checked'+}+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 }+ return { name, result: skippedResult(check) }}if (check.level === 'passed') {if (check.key === 'internal_users' && filtersTestAccounts) {- return { name, result: 'Internal and test users are filtered out', fix: null }+ return { name, result: 'Filtered out' }}- 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 }+ return { name, result: check.events ? `Almost none (${eventCount(check)} events)` : 'None found' }}- 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) }+ const percent = percentOf(check.share)+ const amount = percent ? `${check.scaled ? 'About ' : ''}${percent}` : capitalizeFirstLetter(eventCount(check))+ return { name, result: `${amount} of these events` }}diff --git a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.tsindex 306443852009..3290e9d28687 100644--- a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts+++ b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts@@ -4,6 +4,7 @@ import { FEATURE_FLAGS } from 'lib/constants'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { insightDataLogic } from 'scenes/insights/insightDataLogic'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'+import { teamLogic } from 'scenes/teamLogic'import { useMocks } from '~/mocks/jest'import { NodeKind } from '~/queries/schema/schema-general'@@ -12,6 +13,8 @@ import { initKeaTests } from '~/test/init'import { ChartDisplayType } from '~/types'import type { InsightShortId } from '~/types'+import type { InsightDataCheckApi } from 'products/product_analytics/frontend/generated/api.schemas'+import { insightDataChecksLogic } from './insightDataChecksLogic'const props = { dashboardItemId: '123' as InsightShortId }@@ -27,6 +30,7 @@ describe('insightDataChecksLogic', () => {const dataChecks = jest.fn()const cancelledQueryIds: string[] = []let sentSources: Record<string, unknown>[]+ let savedFilters: unknown[]const answer =(response: [number, Record<string, unknown>], held?: Promise<void>, onSent?: () => void) =>@@ -47,6 +51,7 @@ describe('insightDataChecksLogic', () => {beforeEach(() => {dataChecks.mockReset()sentSources = []+ savedFilters = []cancelledQueryIds.length = 0useMocks({get: {@@ -57,6 +62,13 @@ describe('insightDataChecksLogic', () => {'/api/environments/:team_id/query/:kind': { results: [] },'/api/projects/:team_id/insights/data_checks/': dataChecks,},+ patch: {+ '/api/projects/:team_id/': async ({ request }) => {+ const body = (await request.json()) as { test_account_filters: unknown[] }+ savedFilters.push(body.test_account_filters)+ return [200, { ...teamLogic.values.currentTeam, ...body }]+ },+ },delete: {'/api/projects/:team_id/query/:id/': ({ params }) => {cancelledQueryIds.push(params.id as string)@@ -270,4 +282,42 @@ describe('insightDataChecksLogic', () => {expect(dataChecks).toHaveBeenCalledTimes(2)expect(logic.values.status).toBe('passed')})++ it('turns the filter on for the insight from the checks, then checks the filtered chart', async () => {+ dataChecks.mockImplementation(answer([200, checksOf('warning')]))+ await expectLogic(logic, turnTheFlagOn).toFinishAllListeners()++ await expectLogic(logic, () =>+ logic.actions.setFiltersTestAccounts(true, 'internal_users')+ ).toFinishAllListeners()++ expect(vizData.values.querySource?.filterTestAccounts).toBe(true)+ expect(sentSources.map((source) => !!source.filterTestAccounts)).toEqual([false, true])+ })++ const rule = { key: 'email', type: 'person', operator: 'is_not', value: ['[email protected]'] }+ const addTheRule = (): void => {+ logic.actions.addFilterRule({ key: 'internal_users', filter_to_add: rule } as InsightDataCheckApi)+ }+ it("adds the check's rule to the project's filter, then checks again", async () => {+ const existing = teamLogic.values.currentTeam?.test_account_filters ?? []+ dataChecks.mockImplementation(answer([200, checksOf('warning')]))+ await expectLogic(logic, turnTheFlagOn).toFinishAllListeners()++ await expectLogic(logic, addTheRule).toFinishAllListeners()++ expect(savedFilters).toEqual([[...existing, rule]])+ expect(dataChecks).toHaveBeenCalledTimes(2)+ })++ it('refreshes a chart that applies the filter once a rule is added, so it drops the excluded events', async () => {+ dataChecks.mockImplementation(answer([200, checksOf('warning')]))+ vizData.actions.updateQuerySource({ filterTestAccounts: true })+ await expectLogic(logic, turnTheFlagOn).toFinishAllListeners()++ await expectLogic(logic, addTheRule).toDispatchActions(vizData, [+ (action) =>+ action.type === vizData.actionTypes.loadData && String(action.payload.refresh).startsWith('force'),+ ])+ })})diff --git a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.tsindex b8513e3dd1b9..b974716ca274 100644--- a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts+++ b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts@@ -15,7 +15,10 @@ import { insightDataLogic } from 'scenes/insights/insightDataLogic'import { insightLogic } from 'scenes/insights/insightLogic'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'+import { autoRunMaxPrompt } from 'scenes/max/maxPrompt'+import { teamLogic } from 'scenes/teamLogic'+import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'import { cancelQueryOnServer, concurrencyController } from '~/queries/nodes/DataNode/dataNodeLogic'import { NodeKind } from '~/queries/schema/schema-general'import type {@@ -26,17 +29,21 @@ import type {StickinessQuery,TrendsQuery,} from '~/queries/schema/schema-general'-import { isEventsNode, isInsightQueryWithSeries, isInsightVizNode } from '~/queries/utils'-import type { InsightLogicProps } from '~/types'+import { isEventsNode, isInsightQueryWithSeries, isInsightVizNode, shouldQueryBeAsync } from '~/queries/utils'+import { SidePanelTab } from '~/types'+import type { AnyPropertyFilter, InsightLogicProps, TeamPublicType, TeamType } from '~/types'import { insightsDataChecksCreate } from 'products/product_analytics/frontend/generated/api'import type {InsightDataCheckApi,+ InsightDataCheckKeyEnumApi,InsightDataChecksApi,InsightDataChecksRequestApi,InsightDataChecksStatusEnumApi,} from 'products/product_analytics/frontend/generated/api.schemas'+import { isFinding, postHogAIPromptOf, removesBotTraffic } from './dataCheckText'+/** The insight query the server checks. The server reads the events from the query this chart runs. */export interface DataChecksQuery {kind: NodeKind.InsightVizNode@@ -55,18 +62,27 @@ export interface insightDataChecksLogicValues {query: Node | null // insightDataLogichasOverrides: boolean // insightLogicinsightDataLoading: boolean // insightVizDataLogic+ selectedTab: SidePanelTab | null // sidePanelStateLogic+ sidePanelOpen: boolean // sidePanelStateLogic+ currentTeam: TeamPublicType | TeamType | null // teamLogic+ currentTeamLoading: boolean // teamLogiccheckAgainRequested: booleancheckedQuery: DataChecksQuery | nullchecks: InsightDataCheckApi[]dataChecksRun: DataChecksRun | nulldataChecksRunLoading: boolean+ filterRemovesBots: booleanfiltersTestAccounts: booleanfindings: InsightDataCheckApi[]labelHeldOpen: booleanlabelOpen: boolean+ lastResult: InsightDataChecksApi | null+ postHogAIOpen: boolean+ queryFiltersTestAccounts: booleanrequestedQuery: DataChecksQuery | nullresult: InsightDataChecksApi | nullstatus: InsightDataChecksStatus+ visibleChecks: InsightDataCheckApi[]}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -140,14 +156,32 @@ export interface insightDataChecksLogicActions {updateQuerySource: (querySource: import('scenes/insights/insightVizDataLogic').QuerySourceUpdate) => {querySource: import('scenes/insights/insightVizDataLogic').QuerySourceUpdate} // insightVizDataLogic+ openSidePanel: (+ tab: SidePanelTab,+ options?: string | undefined+ ) => {+ options: string | undefined+ tab: SidePanelTab+ } // sidePanelStateLogic+ updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic+ updateCurrentTeamSuccess: (+ currentTeam: TeamPublicType | TeamType,+ payload?: Partial<TeamType> | undefined+ ) => {+ currentTeam: TeamPublicType | TeamType+ payload?: Partial<TeamType>+ } // teamLogic+ addFilterRule: (check: InsightDataCheckApi) => {+ check: InsightDataCheckApi+ }checkData: (again?: boolean) => {again: boolean}closeLabel: () => {value: true}- filterOutInternalUsers: () => {- value: true+ fixWithPostHogAI: (check: InsightDataCheckApi) => {+ check: InsightDataCheckApi}loadDataChecks: (query: DataChecksQuery) => DataChecksQueryloadDataChecksFailure: (@@ -167,12 +201,21 @@ export interface insightDataChecksLogicActions {reportDataChecksViewed: () => {value: true}+ setFiltersTestAccounts: (+ filtersTestAccounts: boolean,+ check: InsightDataCheckKeyEnumApi+ ) => {+ check: InsightDataCheckKeyEnumApi+ filtersTestAccounts: boolean+ }}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface insightDataChecksLogicMeta {key: string__keaTypeGenInternalSelectorTypes: {+ postHogAIOpen: (sidePanelOpen: boolean, selectedTab: SidePanelTab | null) => boolean+ filterRemovesBots: (currentTeam: TeamPublicType | TeamType | null) => booleancheckedQuery: (query: Node<Record<string, any>> | null,featureFlags: FeatureFlagsSet,@@ -185,7 +228,13 @@ export interface insightDataChecksLogicMeta {dataChecksRun: DataChecksRun | null) => InsightDataChecksStatusresult: (dataChecksRun: DataChecksRun | null, status: InsightDataChecksStatus) => InsightDataChecksApi | null- filtersTestAccounts: (result: InsightDataChecksApi | null) => boolean+ filtersTestAccounts: (lastResult: InsightDataChecksApi | null) => boolean+ visibleChecks: (+ status: InsightDataChecksStatus,+ lastResult: InsightDataChecksApi | null,+ checks: InsightDataCheckApi[]+ ) => InsightDataCheckApi[]+ queryFiltersTestAccounts: (checkedQuery: DataChecksQuery | null) => booleanchecks: (result: InsightDataChecksApi | null) => InsightDataCheckApi[]findings: (checks: InsightDataCheckApi[]) => InsightDataCheckApi[]labelOpen: (status: InsightDataChecksStatus, labelHeldOpen: boolean) => boolean@@ -204,7 +253,6 @@ const FORCED_REFRESHES: (RefreshType | undefined)[] = ['force_async', 'force_bloconst DATA_CHECKS_TIMEOUT_MS = 120_000const LABEL_OPEN_MS = 2500const YIELDED_TO_CHART = 'yielded to chart'-export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> = kea<insightDataChecksLogicType>([props({} as InsightLogicProps),key(keyForInsightLogicProps('new')),@@ -219,18 +267,31 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =['hasOverrides'],featureFlagLogic,['featureFlags'],+ teamLogic,+ ['currentTeam', 'currentTeamLoading'],+ sidePanelStateLogic,+ ['sidePanelOpen', 'selectedTab'],],actions: [insightVizDataLogic(props),['loadData', 'loadDataSuccess', 'updateQuerySource'],featureFlagLogic,['setFeatureFlags'],+ sidePanelStateLogic,+ ['openSidePanel'],+ teamLogic,+ ['updateCurrentTeam', 'updateCurrentTeamSuccess'],],})),actions({+ addFilterRule: (check: InsightDataCheckApi) => ({ check }),checkData: (again: boolean = false) => ({ again }),closeLabel: true,- filterOutInternalUsers: true,+ setFiltersTestAccounts: (filtersTestAccounts: boolean, check: InsightDataCheckKeyEnumApi) => ({+ filtersTestAccounts,+ check,+ }),+ fixWithPostHogAI: (check: InsightDataCheckApi) => ({ check }),reportDataChecksViewed: true,}),loaders(({ cache }) => ({@@ -308,8 +369,26 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =closeLabel: () => false,},],+ // Shown while the checks run again, so the list keeps its place and its open rows.+ lastResult: [+ null as InsightDataChecksApi | null,+ {+ loadDataChecksSuccess: (lastResult, { dataChecksRun }) =>+ dataChecksRun?.ok ? dataChecksRun.result : lastResult,+ },+ ],}),selectors({+ postHogAIOpen: [+ (s) => [s.sidePanelOpen, s.selectedTab],+ (sidePanelOpen: boolean, selectedTab: SidePanelTab | null): boolean =>+ sidePanelOpen && selectedTab === SidePanelTab.Max,+ ],+ filterRemovesBots: [+ (s) => [s.currentTeam],+ (currentTeam: TeamPublicType | TeamType | null): boolean =>+ removesBotTraffic(currentTeam?.test_account_filters ?? []),+ ],// The server only checks insights whose series are all events, so no request goes out for the rest.// Compared by value, so a render that rebuilds the same query is not a new query.checkedQuery: [@@ -354,8 +433,21 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =dataChecksRun?.ok && status !== 'checking' ? dataChecksRun.result : null,],filtersTestAccounts: [- (s) => [s.result],- (result: InsightDataChecksApi | null): boolean => result?.coverage?.filters_test_accounts ?? false,+ (s) => [s.lastResult],+ (lastResult: InsightDataChecksApi | null): boolean => lastResult?.coverage?.filters_test_accounts ?? false,+ ],+ visibleChecks: [+ (s) => [s.status, s.lastResult, s.checks],+ (+ status: InsightDataChecksStatus,+ lastResult: InsightDataChecksApi | null,+ checks: InsightDataCheckApi[]+ ): InsightDataCheckApi[] => (status === 'checking' ? (lastResult?.checks ?? []) : checks),+ ],+ // The chart's own toggle, which changes before the checks run again.+ queryFiltersTestAccounts: [+ (s) => [s.checkedQuery],+ (checkedQuery: DataChecksQuery | null): boolean => !!checkedQuery?.source.filterTestAccounts,],checks: [(s) => [s.result],@@ -363,8 +455,7 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =],findings: [(s) => [s.checks],- (checks: InsightDataCheckApi[]): InsightDataCheckApi[] =>- checks.filter((check) => check.level === 'warning' || check.level === 'problem'),+ (checks: InsightDataCheckApi[]): InsightDataCheckApi[] => checks.filter(isFinding),],labelOpen: [(s) => [s.status, s.labelHeldOpen],@@ -407,10 +498,45 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =loadDataSuccess: ({ payload }) => {actions.checkData(FORCED_REFRESHES.includes(payload?.refresh))},- filterOutInternalUsers: () => {- actions.updateQuerySource({ filterTestAccounts: true })- // pinned: analytics event name, renaming breaks dashboards- posthog.capture('insight data check fix applied', { check: 'internal_users' })+ setFiltersTestAccounts: ({ filtersTestAccounts, check }) => {+ actions.updateQuerySource({ filterTestAccounts: filtersTestAccounts })+ if (filtersTestAccounts) {+ // pinned: analytics event name, renaming breaks dashboards+ posthog.capture('insight data check fix applied', { check })+ }+ },+ fixWithPostHogAI: ({ check }) => {+ // A second request while PostHog AI is open would start another run for the same finding.+ if (values.postHogAIOpen) {+ return+ }+ actions.openSidePanel(SidePanelTab.Max, autoRunMaxPrompt(postHogAIPromptOf(check)))+ posthog.capture('insight data check fix applied', { check: check.key, fix: 'posthog_ai' })+ },+ addFilterRule: ({ check }) => {+ if (!check.filter_to_add) {+ return+ }+ actions.updateCurrentTeam({+ test_account_filters: [+ ...(values.currentTeam?.test_account_filters ?? []),+ check.filter_to_add as AnyPropertyFilter,+ ],+ })+ posthog.capture('insight data check fix applied', { check: check.key, fix: 'add_filter_rule' })+ },+ // The project's filter decides what the checks report, so they run again once it changes. A chart that applies+ // the filter loads again first, and its forced refresh runs the checks when it ends.+ updateCurrentTeamSuccess: ({ payload }) => {+ if (!payload || !('test_account_filters' in payload)) {+ return+ }+ const source = values.checkedQuery?.source+ if (source?.filterTestAccounts) {+ actions.loadData(shouldQueryBeAsync(source) ? 'force_async' : 'force_blocking')+ } else {+ actions.checkData(true)+ }},reportDataChecksViewed: () => {// pinned: analytics event name, renaming breaks dashboardsdiff --git a/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.test.ts b/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.test.tsnew file mode 100644index 000000000000..d02a4a727db1--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.test.ts@@ -0,0 +1,18 @@+import { repeatedEventsQueryOf } from './repeatedEventsQuery'++const scope = { events: ['$pageview'], dateFrom: '2026-10-14', dateTo: '2026-10-20', sampleRate: 1 }++describe('repeatedEventsQueryOf', () => {+ it.each<[string, Partial<typeof scope>, string]>([+ ['escapes a quote in an event name', { events: ["it's done"] }, "event IN ['it\\'s done'] AND "],+ [+ 'reads every event when the check named none',+ { events: [] },+ "WHERE toDate(timestamp) >= toDate('2026-10-14')",+ ],+ ['reads the share of people the check sampled', { sampleRate: 0.01234 }, 'FROM events SAMPLE 0.0123\n'],+ ['reads everyone when the check read every event', {}, 'FROM events\n'],+ ])('%s', (_name, change, expected) => {+ expect(repeatedEventsQueryOf({ ...scope, ...change })).toContain(expected)+ })+})diff --git a/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.ts b/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.tsnew file mode 100644index 000000000000..447ecbade764--- /dev/null+++ b/products/product_analytics/frontend/insights/dataChecks/repeatedEventsQuery.ts@@ -0,0 +1,53 @@+import { hogql } from '~/queries/utils'++// Matches the check's window on the server, so the query lists the same repeats the check counted.+export const REPEAT_WINDOW_MILLISECONDS = 100++export interface RepeatedEventsScope {+ events: string[]+ dateFrom: string+ dateTo: string+ sampleRate: number+}++export function repeatedEventsQueryOf({ events, dateFrom, dateTo, sampleRate }: RepeatedEventsScope): string {+ const eventFilter = events.length ? hogql`event IN ${events} AND ` : ''+ const sample = sampleRate < 1 ? hogql` SAMPLE ${Number(sampleRate.toPrecision(3))}` : ''+ // The partition matches the check's, so an event counts as a repeat only when nothing but its time differs.+ return hogql`-- Events sent again by the same person less than ${REPEAT_WINDOW_MILLISECONDS} ms apart, on the days the check read+SELECT timestamp, event, distinct_id, url, library, library_version, milliseconds_after_previous+FROM (+ SELECT+ timestamp,+ event,+ distinct_id,+ properties.$current_url AS url,+ properties.$lib AS library,+ properties.$lib_version AS library_version,+ dateDiff(+ 'millisecond',+ lagInFrame(timestamp) OVER (+ PARTITION BY+ distinct_id,+ event,+ properties.$raw_user_agent,+ properties.$current_url,+ properties.$screen_name,+ properties.$feature_flag,+ properties.$event_type,+ elements_chain_texts,+ elements_chain_href,+ elements_chain_ids,+ elements_chain_elements+ ORDER BY timestamp+ ROWS BETWEEN 1 PRECEDING AND CURRENT ROW+ ),+ timestamp+ ) AS milliseconds_after_previous+ FROM events${hogql.raw(sample)}+ WHERE ${hogql.raw(eventFilter)}toDate(timestamp) >= toDate(${dateFrom}) AND toDate(timestamp) <= toDate(${dateTo})+)+WHERE milliseconds_after_previous < ${REPEAT_WINDOW_MILLISECONDS}+ORDER BY timestamp DESC+LIMIT 100`+}