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.

fix(cohorts): guard cohort criteria row against unmapped behavioral value (#71858)

posthog[bot]b37fe4f8dcea
diff --git a/frontend/src/scenes/cohorts/CohortEdit.test.tsx b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
index 457c712c5ecd..2f97801b52f1 100644
--- a/frontend/src/scenes/cohorts/CohortEdit.test.tsx
+++ b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
@@ -12,7 +12,7 @@ import { toPaginatedResponse } from '~/mocks/handlers'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { mockCohort } from '~/test/mocks'
-import { AnyCohortCriteriaType, BehavioralEventType } from '~/types'
+import { AnyCohortCriteriaType, BehavioralEventType, FilterLogicalOperator } from '~/types'
import { CohortEdit } from './CohortEdit'
@@ -607,4 +607,54 @@ describe('cohortEditLogic', () => {
expect(populateFromContainer?.tagName).not.toBe('BUTTON')
})
})
+
+ describe('criteria with unmapped behavioral value', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ it('renders the editor instead of crashing when a criterion has a value with no ROWS entry', async () => {
+ // Stored criteria can carry a behavioral value with no ROWS entry, which the row builder
+ // still has to render. A throw here replaces the whole scene with the error boundary, so
+ // finding the cohort name is what proves the criteria row rendered.
+ const cohortId = 11
+
+ useMocks({
+ get: {
+ [`/api/projects/:team_id/cohorts/${cohortId}/`]: {
+ id: cohortId,
+ name: 'Unmapped Criteria Cohort',
+ is_static: false,
+ filters: {
+ properties: {
+ id: '1',
+ type: FilterLogicalOperator.Or,
+ values: [
+ {
+ id: '2',
+ type: FilterLogicalOperator.Or,
+ values: [
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: 'legacy_unknown_value',
+ key: '$pageview',
+ },
+ ],
+ },
+ ],
+ },
+ },
+ version: 1,
+ pending_version: 1,
+ is_calculating: false,
+ last_calculation: '2024-01-01T00:00:00Z',
+ },
+ },
+ })
+
+ render(<CohortEdit id={cohortId} />)
+
+ expect(await screen.findByText('Unmapped Criteria Cohort')).toBeInTheDocument()
+ })
+ })
})
diff --git a/frontend/src/scenes/cohorts/CohortFilters/CohortCriteriaRowBuilder.tsx b/frontend/src/scenes/cohorts/CohortFilters/CohortCriteriaRowBuilder.tsx
index 1730a0775a2b..9ef4a6fb8f75 100644
--- a/frontend/src/scenes/cohorts/CohortFilters/CohortCriteriaRowBuilder.tsx
+++ b/frontend/src/scenes/cohorts/CohortFilters/CohortCriteriaRowBuilder.tsx
@@ -37,7 +37,10 @@ export function CohortCriteriaRowBuilder({
onChangeType,
}: CohortCriteriaRowBuilderProps): JSX.Element {
const { setCriteria, duplicateFilter, removeFilter } = useActions(cohortEditLogic)
- const rowShape = ROWS[type]
+ // Stored criteria can carry a behavioral value that isn't a known ROWS key (legacy or
+ // otherwise unmapped). Fall back to the default event row so the cohort still renders and
+ // stays editable instead of throwing when we later read rowShape.fields.
+ const rowShape = ROWS[type] ?? ROWS[BehavioralEventType.PerformEvent]
const renderFieldComponent = (_field: Field, i: number): JSX.Element => {
return (