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.

feat(replay-vision): suggest a scanner's tags on a classifier alert (#104822)

Kim Svatos Dugan4dd061eb12f6
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertCreateModal.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertCreateModal.tsx
index ed2437a242b0..fdd8e38307bc 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertCreateModal.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertCreateModal.tsx
@@ -4,6 +4,7 @@ import { Form } from 'kea-forms'
import { LemonBanner, LemonModal } from '@posthog/lemon-ui'
import { WizardReview } from 'lib/components/WizardReview'
+import type { LemonInputSelectOption } from 'lib/lemon-ui/LemonInputSelect'
import { AlertEditorFormDetails } from 'products/alerts/frontend/components/AlertEditor'
import { AlertWizard, AlertWizardStep } from 'products/alerts/frontend/components/AlertWizard'
@@ -17,6 +18,7 @@ import { ScannerAlertNotifications } from './ScannerAlertNotifications'
interface ScannerAlertCreateModalProps {
scannerId: string
scannerType?: string
+ tagOptions?: LemonInputSelectOption[]
isOpen: boolean
onClose: () => void
}
@@ -24,13 +26,19 @@ interface ScannerAlertCreateModalProps {
export function ScannerAlertCreateModal({
scannerId,
scannerType,
+ tagOptions,
isOpen,
onClose,
}: ScannerAlertCreateModalProps): JSX.Element {
return (
<LemonModal isOpen={isOpen} onClose={onClose} title="" simple width={900}>
{isOpen ? (
- <ScannerAlertCreateModalContent scannerId={scannerId} scannerType={scannerType} onClose={onClose} />
+ <ScannerAlertCreateModalContent
+ scannerId={scannerId}
+ scannerType={scannerType}
+ tagOptions={tagOptions}
+ onClose={onClose}
+ />
) : null}
</LemonModal>
)
@@ -39,10 +47,12 @@ export function ScannerAlertCreateModal({
function ScannerAlertCreateModalContent({
scannerId,
scannerType,
+ tagOptions,
onClose,
}: {
scannerId: string
scannerType?: string
+ tagOptions?: LemonInputSelectOption[]
onClose: () => void
}): JSX.Element {
const formLogicProps = { scannerId, alert: null, onSubmitSuccess: onClose }
@@ -76,7 +86,7 @@ function ScannerAlertCreateModalContent({
cannotAdvanceReason: thresholdError,
renderContent: (showValidationErrors) => (
<div className="max-w-2xl space-y-6">
- <ScannerAlertSelectionFields scannerType={scannerType} />
+ <ScannerAlertSelectionFields scannerType={scannerType} tagOptions={tagOptions} />
<ScannerAlertTrigger thresholdError={showValidationErrors ? thresholdError : undefined} />
</div>
),
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertEditModal.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertEditModal.tsx
index a8dbff8f70b9..2f6f6ebfbdd2 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertEditModal.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertEditModal.tsx
@@ -4,6 +4,8 @@ import { Form } from 'kea-forms'
import { IconBell, IconGraph, IconPulse, IconTarget } from '@posthog/icons'
import { LemonButton, LemonDialog, LemonModal, LemonSwitch } from '@posthog/lemon-ui'
+import type { LemonInputSelectOption } from 'lib/lemon-ui/LemonInputSelect'
+
import { AlertEditor, AlertEditorFormDetails } from 'products/alerts/frontend/components/AlertEditor'
import { AlertSummaryParts } from 'products/alerts/frontend/components/alertSummary'
import { SnoozeButton } from 'products/alerts/frontend/components/SnoozeButton'
@@ -22,6 +24,7 @@ import { ScannerAlertStateTag } from './ScannerAlertStateTag'
interface ScannerAlertEditModalProps {
scannerId: string
scannerType?: string
+ tagOptions?: LemonInputSelectOption[]
alert: VisionAlertConfigurationApi | null
onClose: () => void
}
@@ -29,6 +32,7 @@ interface ScannerAlertEditModalProps {
export function ScannerAlertEditModal({
scannerId,
scannerType,
+ tagOptions,
alert,
onClose,
}: ScannerAlertEditModalProps): JSX.Element {
@@ -38,6 +42,7 @@ export function ScannerAlertEditModal({
<ScannerAlertEditModalContent
scannerId={scannerId}
scannerType={scannerType}
+ tagOptions={tagOptions}
alert={alert}
onClose={onClose}
/>
@@ -49,11 +54,13 @@ export function ScannerAlertEditModal({
function ScannerAlertEditModalContent({
scannerId,
scannerType,
+ tagOptions,
alert,
onClose,
}: {
scannerId: string
scannerType?: string
+ tagOptions?: LemonInputSelectOption[]
alert: VisionAlertConfigurationApi
onClose: () => void
}): JSX.Element {
@@ -93,7 +100,7 @@ function ScannerAlertEditModalContent({
<div className="space-y-3 pt-3">
<AlertEditorFormDetails nameError={nameError} />
<div className="max-w-2xl space-y-6">
- <ScannerAlertSelectionFields scannerType={scannerType} />
+ <ScannerAlertSelectionFields scannerType={scannerType} tagOptions={tagOptions} />
</div>
</div>
),
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertForm.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertForm.tsx
index e2dd1b90c929..574f6d911284 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertForm.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertForm.tsx
@@ -3,6 +3,7 @@ import { useValues } from 'kea'
import { LemonInput, LemonInputSelect, LemonSegmentedButton, LemonSelect } from '@posthog/lemon-ui'
import { LemonField } from 'lib/lemon-ui/LemonField'
+import type { LemonInputSelectOption } from 'lib/lemon-ui/LemonInputSelect'
import { teamLogic } from 'scenes/teamLogic'
import { AlertAdvancedOptions } from 'products/alerts/frontend/components/AlertAdvancedOptions'
@@ -57,7 +58,13 @@ export function ScannerAlertKindPicker(): JSX.Element {
)
}
-export function ScannerAlertSelectionFields({ scannerType }: { scannerType?: string }): JSX.Element {
+export function ScannerAlertSelectionFields({
+ scannerType,
+ tagOptions,
+}: {
+ scannerType?: string
+ tagOptions?: LemonInputSelectOption[]
+}): JSX.Element {
return (
<AlertEditorSection
title="Which observations count"
@@ -86,8 +93,9 @@ export function ScannerAlertSelectionFields({ scannerType }: { scannerType?: str
allowCustomValues
value={value}
onChange={onChange}
- options={[]}
- placeholder="Any tag"
+ options={tagOptions ?? []}
+ virtualized={(tagOptions?.length ?? 0) > 50}
+ placeholder={tagOptions?.length ? 'Select tags' : 'Any tag'}
data-attr="vision-alert-tags"
/>
)}
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.test.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.test.tsx
new file mode 100644
index 000000000000..699f121c6742
--- /dev/null
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.test.tsx
@@ -0,0 +1,92 @@
+import { render, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+import { AccessControlLevel, AccessControlResourceType, type AppContext } from '~/types'
+
+import { ScannerAlertsTab } from './ScannerAlertsTab'
+
+const SCANNER_ID = '01a014ea-854f-72b5-8192-bb6ac9f212a5'
+
+// A classifier alert matches on exact tag strings, so the form has to offer the tags the scanner
+// already has: its configured categories, plus the freeform ones its observations carried.
+describe('ScannerAlertsTab', () => {
+ beforeEach(() => {
+ initKeaTests()
+ window.POSTHOG_APP_CONTEXT = {
+ ...window.POSTHOG_APP_CONTEXT,
+ resource_access_control: {
+ ...window.POSTHOG_APP_CONTEXT?.resource_access_control,
+ [AccessControlResourceType.ReplayScanner]: AccessControlLevel.Editor,
+ [AccessControlResourceType.SessionRecording]: AccessControlLevel.Viewer,
+ },
+ } as AppContext
+ useMocks({
+ get: {
+ '/api/projects/:team/vision/scanners/:id/': [
+ 200,
+ {
+ id: SCANNER_ID,
+ name: 'Checkout friction',
+ scanner_type: 'classifier',
+ scanner_config: {
+ prompt: 'Sort the session.',
+ tags: ['checkout', 'signup'],
+ multi_label: true,
+ },
+ sampling_rate: 1,
+ enabled: true,
+ user_access_level: AccessControlLevel.Editor,
+ },
+ ],
+ '/api/projects/:team/vision/scanners/:id/observations/': [200, { results: [], count: 0 }],
+ '/api/projects/:team/vision/scanners/:id/observations/stats/': [
+ 200,
+ {
+ status_counts: {},
+ coverage: {},
+ labels: {},
+ available_tags: ['checkout', 'payment-declined'],
+ monitor: null,
+ classifier: null,
+ scorer: null,
+ },
+ ],
+ '/api/projects/:team/vision/alerts/': [200, { results: [], count: 0 }],
+ '/api/projects/:team/hog_functions/': [200, { results: [], count: 0 }],
+ '/api/projects/:team/integrations/': [200, { results: [], count: 0 }],
+ },
+ })
+ })
+
+ afterEach(() => {
+ delete (window as { POSTHOG_APP_CONTEXT?: AppContext }).POSTHOG_APP_CONTEXT
+ })
+
+ it('offers the configured categories and the observed freeform tags on a new classifier alert', async () => {
+ render(<ScannerAlertsTab scannerId={SCANNER_ID} />)
+
+ const newAlert = await screen.findByText('New alert')
+ await userEvent.click(newAlert)
+
+ const nameInput = await waitFor(() => {
+ const input = document.querySelector('[data-attr="alert-name"] input, input[name="name"]')
+ expect(input).not.toBeNull()
+ return input as HTMLInputElement
+ })
+ await userEvent.type(nameInput, 'Payment problems')
+ await userEvent.click(screen.getByText('Continue'))
+
+ const tagsInput = await waitFor(() => {
+ const input = document.querySelector('input[data-attr="vision-alert-tags"]')
+ expect(input).not.toBeNull()
+ return input as HTMLInputElement
+ })
+ await userEvent.click(tagsInput)
+
+ expect(await screen.findByText('checkout')).not.toBeNull()
+ expect(screen.getByText('signup')).not.toBeNull()
+ expect(screen.getByText('payment-declined')).not.toBeNull()
+ })
+})
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.tsx
index 991ee21a3301..a67bd48aa602 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerAlertsTab.tsx
@@ -11,7 +11,7 @@ import type { VisionAlertConfigurationApi } from '../../generated/api.schemas'
import { getReplayVisionEditDisabledReason } from '../../utils/accessControl'
import { replayScannerLogic } from '../replayScannerLogic'
import { scannerAlertsLogic } from '../scannerAlertsLogic'
-import { conditionSummary, selectionSummary } from '../scannerAlertUtils'
+import { alertTagOptions, conditionSummary, selectionSummary } from '../scannerAlertUtils'
import { ScannerAlertCreateModal } from './ScannerAlertCreateModal'
import { ScannerAlertEditModal } from './ScannerAlertEditModal'
import { ScannerAlertStateTag } from './ScannerAlertStateTag'
@@ -34,8 +34,14 @@ function ScannerAlertsTabContent({ scannerId }: { scannerId: string }): JSX.Elem
openEditAlertModal,
closeEditAlertModal,
} = useActions(scannerAlertsLogic)
- const { scanner } = useValues(replayScannerLogic({ id: scannerId }))
+ const { scanner, availableTags } = useValues(replayScannerLogic({ id: scannerId }))
const scannerType = scanner?.scanner_type
+ // Configured categories plus the freeform tags observations have carried, so the alert's tag
+ // field suggests every tag this scanner can match instead of asking for them from memory.
+ const tagOptions = alertTagOptions(
+ scanner?.scanner_type === 'classifier' ? (scanner.scanner_config.tags ?? []) : [],
+ availableTags
+ )
const editDisabledReason = getReplayVisionEditDisabledReason(scanner?.user_access_level)
const columns: LemonTableColumns<VisionAlertConfigurationApi> = [
@@ -159,12 +165,14 @@ function ScannerAlertsTabContent({ scannerId }: { scannerId: string }): JSX.Elem
<ScannerAlertCreateModal
scannerId={scannerId}
scannerType={scannerType}
+ tagOptions={tagOptions}
isOpen={isCreateAlertModalOpen}
onClose={closeCreateAlertModal}
/>
<ScannerAlertEditModal
scannerId={scannerId}
scannerType={scannerType}
+ tagOptions={tagOptions}
alert={editingAlert}
onClose={closeEditAlertModal}
/>
diff --git a/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.test.ts b/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.test.ts
new file mode 100644
index 000000000000..5ef469b6f73a
--- /dev/null
+++ b/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.test.ts
@@ -0,0 +1,22 @@
+import { alertTagOptions } from './scannerAlertUtils'
+
+describe('alertTagOptions', () => {
+ it('keeps the configured categories first and in configuration order', () => {
+ expect(alertTagOptions(['checkout', 'onboarding'], ['analytics'])).toEqual([
+ { key: 'checkout', label: 'checkout' },
+ { key: 'onboarding', label: 'onboarding' },
+ { key: 'analytics', label: 'analytics', tooltip: 'Freeform tag seen in observations' },
+ ])
+ })
+
+ it('does not repeat a configured category that observations also carried', () => {
+ expect(alertTagOptions(['checkout', 'checkout'], ['checkout', 'rage'])).toEqual([
+ { key: 'checkout', label: 'checkout' },
+ { key: 'rage', label: 'rage', tooltip: 'Freeform tag seen in observations' },
+ ])
+ })
+
+ it('returns nothing when the scanner has no tags at all', () => {
+ expect(alertTagOptions([], [])).toEqual([])
+ })
+})
diff --git a/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.ts b/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.ts
index d2c1ca197530..f8684082b6c3 100644
--- a/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.ts
+++ b/products/replay_vision/frontend/replay_scanners/scannerAlertUtils.ts
@@ -1,3 +1,5 @@
+import type { LemonInputSelectOption } from 'lib/lemon-ui/LemonInputSelect'
+
import { CyclotronJobFiltersType, HogFunctionType, PropertyFilterType, PropertyOperator } from '~/types'
import type { VisionAlertConfigurationApi } from '../generated/api.schemas'
@@ -36,6 +38,28 @@ export type VisionAlertDestinationGroup = {
enabled: boolean
}
+// The alert predicate compares tag strings exactly, so an option's `label` stays the raw tag.
+// Configured categories come first because they are the vocabulary the classifier picks from;
+// the tags only seen in observations are freeform ones, and they follow in the order the stats
+// endpoint sorted them.
+export function alertTagOptions(configuredTags: string[], observedTags: string[]): LemonInputSelectOption[] {
+ const options: LemonInputSelectOption[] = []
+ const seen = new Set<string>()
+ for (const tag of configuredTags) {
+ if (!seen.has(tag)) {
+ seen.add(tag)
+ options.push({ key: tag, label: tag })
+ }
+ }
+ for (const tag of observedTags) {
+ if (!seen.has(tag)) {
+ seen.add(tag)
+ options.push({ key: tag, label: tag, tooltip: 'Freeform tag seen in observations' })
+ }
+ }
+ return options
+}
+
// Matches every HogFunction belonging to this alert regardless of event kind: the create
// endpoint fans out into one HogFunction per event kind, and the alert_id property alone
// identifies the whole group.