: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): edit nested event filters on performed event filters (#90222)
Sam Pennington0eb82cf75df6
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex e28a3dbf7ea7..46c31d5b3155 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -2413,13 +2413,13 @@ snapshots:filters-action-filter--trends-group-default-name--light:hash: v1.k794b7964.0ae44b0f25e02c062e985b302f4563d1736ca6b1b01f2ecd15bd1cd563d1cc78.-5AoeartLEtxntcPowA-5fjRJ04EoSdMhI1_lfYbj_Mfilters-behavioral-property-filter-row--action--dark:- hash: v1.k794b7964.64be13cd28775723861e4f207be903912d920a6c3fc07b0642fcf5b649ece71b.-g0cCMVhx80qaSirvtkcIWNXCCuDqQQFBw3s2S75JUk+ hash: v1.k794b7964.806596ab5714397a2ecf05b4d9d1effdce3b90d3912700d10d058c66d20b188a.E8WSKkhAnn9gMbT-kvFdzb04jrz3HyQpLJDM3Xsc9rQfilters-behavioral-property-filter-row--action--light:- hash: v1.k794b7964.3879bd7d908474b51cd37254eedab66fe23ab852a9736568d12cc985dcd55c34.8y4ho1W8dxU0Spoy39HHvf2O590lt5AkOvv6M8kfCtI+ hash: v1.k794b7964.c78306b5885e8d68e9d79f746f707191b3de3d75f49f1b4aefac3cf33080707c.goPdwuPRxYM5zsA9mnNeeRM8KSqC7HupmX7zt-tTTHgfilters-behavioral-property-filter-row--default--dark:- hash: v1.k794b7964.6c098c9d07b3fa2c86fe538ce7945c965a098a002ec6b0e56481f167e16b4ba4.6d-IwX4pVq2svuxGFXT1cCAVjKQ4Zv_cJ0cau6rXiXs+ hash: v1.k794b7964.2569cf5bbad9a703ede742f89c5761230dc6aebb571ac1d625661bac935be44c.LJzN_DnBqhbnjOOENqX9MNrDzgtjce-LxOtHuzcxu_Qfilters-behavioral-property-filter-row--default--light:- hash: v1.k794b7964.a487c6f8a0f019c2a8e8e390a384e50fb5ad829d3e3bc5bf1e44f65cf89779c2.pwncW6m1jZuhyuW-_EQFilUiLjOSA86CW3Z1cOokGGk+ hash: v1.k794b7964.887ff73691b613e8d933720df6ca4db528cf1372f8b06f58be78f53cad40f92b.bOuTAZ0ZQK2aOu4_Y8vq4QBeI47FNgg1P6RAjtK6c7Ufilters-behavioral-property-filter-row--read-only--dark:hash: v1.k794b7964.14387613fce7c7a7c5ba8bd849a0171880ad4fa50e82fc3df4a9ccd0659ed0cd.MbDp30kp-57hPNOb6VqaZutKy4arkZbmSf2rzcPd_scfilters-behavioral-property-filter-row--read-only--light:@@ -2428,6 +2428,14 @@ snapshots:hash: v1.k794b7964.8b0d6d3f9b613d643790d6afeb7d9fbd6fd37fc92c2a909ae1a6171b53693221.c982c9VZHohS7BM8Ypec9GmEhe7m-T1RUAmbf0FCAJofilters-behavioral-property-filter-row--read-only-with-count--light:hash: v1.k794b7964.b791a9c5ce7c345a68c8528ad5ab4097671704b92e7f749c114fdc8eb7f10f54.VufLlD1J093-NdMoyYxIz4FPQwkCCweZfKnA3I2eBV4+ filters-behavioral-property-filter-row--read-only-with-event-filters--dark:+ hash: v1.k794b7964.4fd3274118c516ecaac76273698f000b6e59be9bb25bd46e95ba3e9f87fc78e4.k1eZwEEO-KtlhznyKkdCtuNFgAdSOPuvXijZ4YsIs08+ filters-behavioral-property-filter-row--read-only-with-event-filters--light:+ hash: v1.k794b7964.d400b095209e55b32bd1cf26766d66a14f3e880343bc4eef201e02560630d0a0.tTT6CE8YKbjQsqTOq6obzv52Dm0dAWump4_ioJgF3sU+ filters-behavioral-property-filter-row--with-event-filters--dark:+ hash: v1.k794b7964.6d3a2b1b4dc826b673b8071d90ace4585a6a9362aedec4c12857c20f2d807b7d.0M0n8uhybmgEU8q39clAOR7ZDfHFSZbhoigu8T1WZQU+ filters-behavioral-property-filter-row--with-event-filters--light:+ hash: v1.k794b7964.8bde1dc87ff3cebedf513ef53f1f57e4020d98a2009e17bfa0fe02dd95a82a40.pGneyWUeJ0AZVn7ydb70ELqcqwI4hJbVa85-eFI3uiAfilters-breakdown-tag--breakdown-tag--dark:hash: v1.k794b7964.e6cbdc3a3393ecc4000ad415cc5ab5d834207538c4114398f2f55c2823e797cb.wQxm2Qz-ltdbkXWgwNfBwgF5-uZnsGK4y3eNDQTol1cfilters-breakdown-tag--breakdown-tag--light:diff --git a/frontend/src/lib/components/PropertyFilters/PropertyFilters.tsx b/frontend/src/lib/components/PropertyFilters/PropertyFilters.tsxindex b0bc7b209448..34d7bca1e5f8 100644--- a/frontend/src/lib/components/PropertyFilters/PropertyFilters.tsx+++ b/frontend/src/lib/components/PropertyFilters/PropertyFilters.tsx@@ -201,6 +201,7 @@ export function PropertyFilters({filter={item}onChange={(filter) => setFilter(index, filter)}editable={editable}+ pageKey={`${pageKey}-behavioral-${displayedFilterIds[index]}`}size={buttonSize}/></div>diff --git a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.stories.tsx b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.stories.tsxindex 5029e506b74e..25f8000f1cdd 100644--- a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.stories.tsx+++ b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.stories.tsx@@ -22,11 +22,13 @@ const meta: Meta<typeof BehavioralPropertyFilterRow> = {mswDecorator({get: {'/api/projects/:team_id/actions/': { count: 1, next: null, previous: null, results: [CHECKOUT_ACTION] },+ '/api/event/values/': [],},}),],args: {editable: true,+ pageKey: 'behavioral-row-story',},render: (props) => {const [filter, setFilter] = useState<BehavioralPropertyFilter>({@@ -66,6 +68,53 @@ export const Action: Story = {),}+export const WithEventFilters: Story = {+ render: (props) => {+ const [filter, setFilter] = useState<BehavioralPropertyFilter>({+ type: PropertyFilterType.Behavioral,+ value: BehavioralEventType.PerformEvent,+ key: 'insight created',+ event_type: 'events',+ negation: true,+ time_value: 30,+ time_interval: TimeUnitType.Day,+ event_filters: [+ { type: PropertyFilterType.Event, key: 'source', operator: PropertyOperator.Exact, value: ['web'] },+ {+ type: PropertyFilterType.Event,+ key: '$browser',+ operator: PropertyOperator.Exact,+ value: ['Chrome'],+ },+ ],+ })++ return <BehavioralPropertyFilterRow {...props} filter={filter} onChange={setFilter} />+ },+}++export const ReadOnlyWithEventFilters: Story = {+ args: { editable: false },+ render: (props) => (+ <BehavioralPropertyFilterRow+ {...props}+ filter={{+ type: PropertyFilterType.Behavioral,+ value: BehavioralEventType.PerformEvent,+ key: 'insight created',+ event_type: 'events',+ negation: true,+ time_value: 30,+ time_interval: TimeUnitType.Day,+ event_filters: [+ { type: PropertyFilterType.Event, key: 'source', operator: PropertyOperator.Exact, value: ['web'] },+ ],+ }}+ onChange={() => {}}+ />+ ),+}+export const ReadOnlyWithCount: Story = {args: { editable: false },render: (props) => (diff --git a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.test.ts b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.test.tsindex 3a42946009ce..1a576a1bf004 100644--- a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.test.ts+++ b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.test.ts@@ -1,17 +1,27 @@import {withBehavioralCount,+ withBehavioralEventFilters,withBehavioralNegation,} from 'lib/components/PropertyFilters/components/BehavioralPropertyFilterRow'import {BehavioralEventType,BehavioralPropertyFilter,+ EventPropertyFilter,PropertyFilterType,PropertyOperator,TimeUnitType,} from '~/types'describe('BehavioralPropertyFilterRow value mapping', () => {+ const sourceIsWeb: EventPropertyFilter = {+ type: PropertyFilterType.Event,+ key: 'source',+ operator: PropertyOperator.Exact,+ value: ['web'],+ }++ // event_filters in the fixture prove count/negation edits leave nested filters untouchedconst counted: BehavioralPropertyFilter = {type: PropertyFilterType.Behavioral,key: 'signed_up',@@ -21,6 +31,7 @@ describe('BehavioralPropertyFilterRow value mapping', () => {operator_value: 3,time_value: 30,time_interval: TimeUnitType.Day,+ event_filters: [sourceIsWeb],}test.each<[string, PropertyOperator, number, Partial<BehavioralPropertyFilter>]>([@@ -70,4 +81,18 @@ describe('BehavioralPropertyFilterRow value mapping', () => {negation: undefined,})})++ it('replaces the nested event filters', () => {+ const emailIsSet: EventPropertyFilter = {+ type: PropertyFilterType.Event,+ key: 'email',+ operator: PropertyOperator.IsSet,+ value: null,+ }+ expect(withBehavioralEventFilters(counted, [emailIsSet])).toEqual({ ...counted, event_filters: [emailIsSet] })+ })++ it('drops event_filters entirely when the last nested filter is removed', () => {+ expect(withBehavioralEventFilters(counted, [])).toEqual({ ...counted, event_filters: undefined })+ })})diff --git a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsxindex 83fb0d358fcd..764449c6ef08 100644--- a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx+++ b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx@@ -1,7 +1,10 @@import { useValues } from 'kea'+import { useState } from 'react'-import { LemonInput, LemonSelect } from '@posthog/lemon-ui'+import { IconFilter } from '@posthog/icons'+import { LemonButton, LemonInput, LemonSelect } from '@posthog/lemon-ui'+import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'import {BEHAVIORAL_COUNT_OPERATOR_LABELS,behavioralEntityLabel,@@ -9,9 +12,16 @@ import {} from 'lib/components/PropertyFilters/utils'import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'import { TaxonomicPopover } from 'lib/components/TaxonomicPopover/TaxonomicPopover'+import { IconWithCount } from 'lib/lemon-ui/icons'import { actionsModel } from '~/models/actionsModel'-import { BehavioralEventType, BehavioralPropertyFilter, PropertyOperator, TimeUnitType } from '~/types'+import {+ AnyPropertyFilter,+ BehavioralEventType,+ BehavioralPropertyFilter,+ PropertyOperator,+ TimeUnitType,+} from '~/types'import { PropertyFilterButton } from './PropertyFilterButton'@@ -19,9 +29,20 @@ export interface BehavioralPropertyFilterRowProps {filter: BehavioralPropertyFilteronChange: (filter: BehavioralPropertyFilter) => voideditable: boolean+ /** Keys the nested event-filter editor's own propertyFilterLogic, so it must be unique per row */+ pageKey: stringsize?: 'xsmall' | 'small' | 'medium'}+// Matches the schema: nested behavioral/cohort filters and groups are deliberately unsupported+const EVENT_FILTER_TAXONOMIC_GROUP_TYPES = [+ TaxonomicFilterGroupType.EventProperties,+ TaxonomicFilterGroupType.PersonProperties,+ TaxonomicFilterGroupType.EventFeatureFlags,+ TaxonomicFilterGroupType.Elements,+ TaxonomicFilterGroupType.HogQLExpression,+]+const COUNT_OPERATOR_OPTIONS = [PropertyOperator.GreaterThanOrEqual,PropertyOperator.LessThanOrEqual,@@ -63,14 +84,29 @@ export function withBehavioralNegation(filter: BehavioralPropertyFilter, negatio: { ...filter, negation: undefined }}+export function withBehavioralEventFilters(+ filter: BehavioralPropertyFilter,+ eventFilters: AnyPropertyFilter[]+): BehavioralPropertyFilter {+ // An empty list drops the key entirely, so the stored query stays free of empty arrays+ return {+ ...filter,+ event_filters: eventFilters.length ? (eventFilters as BehavioralPropertyFilter['event_filters']) : undefined,+ }+}+export function BehavioralPropertyFilterRow({filter,onChange,editable,+ pageKey,size = 'medium',}: BehavioralPropertyFilterRowProps): JSX.Element {// Only mounted for behavioral filters, so surfaces without one never pay for the actions fetchconst { actionsById } = useValues(actionsModel)+ // Derived (not mount-time state) so a row reused for a different filter after a group deletion doesn't keep a stale open/closed state+ const [userToggledEventFilters, setUserToggledEventFilters] = useState<boolean | null>(null)+ const eventFiltersVisible = userToggledEventFilters ?? !!filter.event_filters?.length// Filters created via the API with explicit date bounds have no window controls to map ontoif (!editable || filter.explicit_datetime) {@@ -102,31 +138,58 @@ export function BehavioralPropertyFilterRow({]}data-attr="behavioral-filter-negation"/>- <div className="min-w-32 flex-1">- <TaxonomicPopover- size={size}- groupType={- filter.event_type === 'actions'- ? TaxonomicFilterGroupType.Actions- : TaxonomicFilterGroupType.Events- }- groupTypes={[TaxonomicFilterGroupType.Events, TaxonomicFilterGroupType.Actions]}- value={filter.event_type === 'actions' ? Number(filter.key) : filter.key}- onChange={(value, groupType) =>- onChange({- ...filter,- key: String(value),- event_type: groupType === TaxonomicFilterGroupType.Actions ? 'actions' : 'events',- })- }- renderValue={() => <span>{behavioralEntityLabel(filter, actionsById)}</span>}- placeholder="Select an event or action"- fullWidth- truncate- data-attr="behavioral-filter-event"- />+ <div className="flex min-w-32 flex-1 items-center gap-2">+ <div className="min-w-0 flex-1">+ <TaxonomicPopover+ size={size}+ groupType={+ filter.event_type === 'actions'+ ? TaxonomicFilterGroupType.Actions+ : TaxonomicFilterGroupType.Events+ }+ groupTypes={[TaxonomicFilterGroupType.Events, TaxonomicFilterGroupType.Actions]}+ value={filter.event_type === 'actions' ? Number(filter.key) : filter.key}+ onChange={(value, groupType) =>+ onChange({+ ...filter,+ key: String(value),+ event_type:+ groupType === TaxonomicFilterGroupType.Actions ? 'actions' : 'events',+ })+ }+ renderValue={() => <span>{behavioralEntityLabel(filter, actionsById)}</span>}+ placeholder="Select an event or action"+ fullWidth+ truncate+ data-attr="behavioral-filter-event"+ />+ </div>+ <IconWithCount count={filter.event_filters?.length || 0} showZero={false}>+ <LemonButton+ icon={<IconFilter />}+ noPadding+ active={eventFiltersVisible}+ onClick={() => setUserToggledEventFilters(!eventFiltersVisible)}+ disabledReason={!filter.key ? 'Please select an event first' : undefined}+ tooltip="Show filters"+ data-attr="behavioral-filter-show-event-filters"+ />+ </IconWithCount></div></div>+ {eventFiltersVisible && (+ <div className="w-full min-w-0 pl-2" data-attr="behavioral-filter-event-filters">+ <PropertyFilters+ propertyFilters={filter.event_filters ?? []}+ onChange={(eventFilters) => onChange(withBehavioralEventFilters(filter, eventFilters))}+ pageKey={`${pageKey}-event-filters`}+ taxonomicGroupTypes={EVENT_FILTER_TAXONOMIC_GROUP_TYPES}+ eventNames={filter.event_type === 'events' && filter.key ? [filter.key] : []}+ disablePopover+ buttonSize={size}+ />+ </div>+ )}{!filter.negation && (<div className="flex min-w-0 flex-wrap items-center gap-2"><LemonSelectdiff --git a/frontend/src/lib/components/PropertyFilters/utils.test.ts b/frontend/src/lib/components/PropertyFilters/utils.test.tsindex d08db4108f4c..f183ab1871e9 100644--- a/frontend/src/lib/components/PropertyFilters/utils.test.ts+++ b/frontend/src/lib/components/PropertyFilters/utils.test.ts@@ -128,6 +128,28 @@ describe('formatPropertyLabel() for behavioral filters', () => {'Performed action 42 in the last 30\u00a0days',noActions,],+ [+ 'API-created negated filter includes its nested event filters',+ {+ negation: true,+ event_filters: [+ { type: PropertyFilterType.Event, key: 'source', operator: PropertyOperator.Exact, value: ['web'] },+ ],+ },+ 'Did not perform signed_up where Source = web in the last 30\u00a0days',+ noActions,+ ],+ [+ 'multiple nested event filters join with and',+ {+ event_filters: [+ { type: PropertyFilterType.Event, key: 'source', operator: PropertyOperator.Exact, value: ['web'] },+ { type: PropertyFilterType.Person, key: 'email', operator: PropertyOperator.IsSet, value: null },+ ],+ },+ 'Performed signed_up where Source = web and Email address ✓ is set in the last 30\u00a0days',+ noActions,+ ],])('%s', (_name, overrides, expected, actionsById) => {expect(formatPropertyLabel({ ...base, ...overrides }, {}, undefined, actionsById)).toEqual(expected)})diff --git a/frontend/src/lib/components/PropertyFilters/utils.ts b/frontend/src/lib/components/PropertyFilters/utils.tsindex 1539afcb7ae1..595b719fd3c3 100644--- a/frontend/src/lib/components/PropertyFilters/utils.ts+++ b/frontend/src/lib/components/PropertyFilters/utils.ts@@ -293,10 +293,13 @@ function formatBehavioralPropertyLabel('time')}`: ''+ const whereClause = item.event_filters?.length+ ? ` where ${item.event_filters.map((filter) => formatPropertyLabel(filter, {}).trim()).join(' and ')}`+ : ''const windowClause = item.explicit_datetime? ` since ${item.explicit_datetime}`: ` in the last ${pluralize(item.time_value ?? 30, item.time_interval ?? TimeUnitType.Day)}`- return `${item.negation ? 'Did not perform' : 'Performed'} ${eventLabel}${countClause}${windowClause}`+ return `${item.negation ? 'Did not perform' : 'Performed'} ${eventLabel}${countClause}${whereClause}${windowClause}`}// Filter keys whose value we offer a read-only group-info card for on hover.