: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
fix(cdp): align mapping filters with event preview (#95791)
ella-c5243c765d984
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.tsindex 335d4873115a..9b03dce969fe 100644--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts@@ -15,6 +15,7 @@ import { initKeaTests } from '~/test/init'import {CyclotronJobFilterEvents,CyclotronJobFiltersType,+ FilterLogicalOperator,HogFunctionTemplateType,HogFunctionType,PropertyFilterType,@@ -319,6 +320,170 @@ describe('hogFunctionConfigurationLogic', () => {})})+ describe('mapping preview filters', () => {+ beforeEach(() => {+ initKeaTests()+ })++ it('combines mapping matchers with global properties and ignores unsupported global matchers', async () => {+ const globalFilters: CyclotronJobFiltersType = {+ events: [{ id: '$pageview', name: '$pageview', type: 'events' }],+ actions: [{ id: '99', name: 'Globally configured action', type: 'actions' }],+ properties: [{ type: PropertyFilterType.HogQL, key: "properties.plan = 'paid'" }],+ }+ mockApi.getTemplate.mockResolvedValue({+ ...HOG_TEMPLATE,+ filters: globalFilters,+ mapping_templates: [+ {+ name: 'Purchase',+ include_by_default: true,+ filters: { actions: [{ id: '42', name: 'Purchased', type: 'actions' }] },+ },+ {+ name: 'Signup',+ include_by_default: true,+ filters: { events: [{ id: 'signed up', name: 'signed up', type: 'events' }] },+ },+ ],+ })+ logic = hogFunctionConfigurationLogic({ templateId: 'test' })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadTemplateSuccess'])++ expect(logic.values.matchingFilters).toEqual({+ type: FilterLogicalOperator.And,+ values: [+ {+ type: FilterLogicalOperator.Or,+ values: [+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: "event = 'signed up'" }],+ },+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: 'matchesAction(42)' }],+ },+ ],+ },+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: "properties.plan = 'paid'" }],+ },+ ],+ })+ expect(logic.values.configuration.filters).toEqual(globalFilters)+ })++ it('matches every event for an all-events mapping', async () => {+ mockApi.getTemplate.mockResolvedValue({+ ...HOG_TEMPLATE,+ mapping_templates: [+ {+ name: 'All events',+ include_by_default: true,+ use_all_events_by_default: true,+ },+ ],+ })+ logic = hogFunctionConfigurationLogic({ templateId: 'all-events' })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadTemplateSuccess'])++ expect(logic.values.matchingFilters).toEqual({+ type: FilterLogicalOperator.And,+ values: [+ {+ type: FilterLogicalOperator.Or,+ values: [+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: 'true' }],+ },+ ],+ },+ ],+ })+ })++ it('matches every event when a mapping has no event or action matchers', async () => {+ mockApi.getTemplate.mockResolvedValue({+ ...HOG_TEMPLATE,+ mapping_templates: [+ {+ name: 'Mapping with removed matchers',+ include_by_default: true,+ filters: {},+ },+ ],+ })+ logic = hogFunctionConfigurationLogic({ templateId: 'removed-matchers' })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadTemplateSuccess'])++ expect(logic.values.matchingFilters).toEqual({+ type: FilterLogicalOperator.And,+ values: [+ {+ type: FilterLogicalOperator.Or,+ values: [+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: 'true' }],+ },+ ],+ },+ ],+ })+ })++ it('matches every event across mixed mappings while preserving global properties', async () => {+ const globalProperties: NonNullable<CyclotronJobFiltersType['properties']> = [+ { type: PropertyFilterType.HogQL, key: "properties.plan = 'paid'" },+ ]+ mockApi.getTemplate.mockResolvedValue({+ ...HOG_TEMPLATE,+ filters: { properties: globalProperties },+ mapping_templates: [+ {+ name: 'Signup',+ include_by_default: true,+ filters: { events: [{ id: 'signed up', name: 'signed up', type: 'events' }] },+ },+ {+ name: 'All events',+ include_by_default: true,+ use_all_events_by_default: true,+ },+ ],+ })+ logic = hogFunctionConfigurationLogic({ templateId: 'mixed-mappings' })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadTemplateSuccess'])++ expect(logic.values.matchingFilters).toEqual({+ type: FilterLogicalOperator.And,+ values: [+ {+ type: FilterLogicalOperator.Or,+ values: [+ {+ type: FilterLogicalOperator.And,+ values: [{ type: PropertyFilterType.HogQL, key: 'true' }],+ },+ ],+ },+ {+ type: FilterLogicalOperator.And,+ values: globalProperties,+ },+ ],+ })+ expect(logic.values.baseEventsQuery?.fixedProperties).toEqual([logic.values.matchingFilters])+ })+ })+describe('loading a missing function', () => {beforeEach(() => {initKeaTests()diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsxindex 5d979834eedf..46bddace9710 100644--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx@@ -1649,11 +1649,22 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTyreturn null}- // Copied before the mappings are merged in: these arrays are the form's own state.- const allPossibleEventFilters = [...(configuration.filters?.events ?? [])]- const allPossibleActionFilters = [...(configuration.filters?.actions ?? [])]+ // Mapped destinations execute mapping-level matchers, not global event/action filters.+ // Keep these arrays separate from configuration so preview calculation cannot mutate form state.+ const allPossibleEventFilters = useMapping ? [] : [...(configuration.filters?.events ?? [])]+ const allPossibleActionFilters = useMapping ? [] : [...(configuration.filters?.actions ?? [])]++ const mappingMatchesAllEvents =+ useMapping &&+ configuration.mappings?.some(+ (mapping) =>+ (mapping.filters?.events?.length ?? 0) === 0 &&+ (mapping.filters?.actions?.length ?? 0) === 0+ )- if (Array.isArray(configuration.mappings)) {+ if (mappingMatchesAllEvents) {+ allPossibleEventFilters.push({ id: '', type: 'events' })+ } else if (useMapping && Array.isArray(configuration.mappings)) {for (const mapping of configuration.mappings) {if (mapping.filters?.events) {allPossibleEventFilters.push(...mapping.filters.events)diff --git a/frontend/src/scenes/hog-functions/filters/HogFunctionFilters.tsx b/frontend/src/scenes/hog-functions/filters/HogFunctionFilters.tsxindex da625d145caa..f01adb15f067 100644--- a/frontend/src/scenes/hog-functions/filters/HogFunctionFilters.tsx+++ b/frontend/src/scenes/hog-functions/filters/HogFunctionFilters.tsx@@ -614,6 +614,12 @@ export function HogFunctionFilters({</div>)+ // Mapping-level matchers have their own editor and persistence model. The global filter tool cannot safely edit+ // them, so do not offer it here until it has a mapping-specific contract.+ if (useMapping) {+ return mainContent+ }+return (<MaxToolidentifier="create_hog_function_filters"diff --git a/frontend/src/scenes/hog-functions/metrics/HogFunctionEventEstimates.tsx b/frontend/src/scenes/hog-functions/metrics/HogFunctionEventEstimates.tsxindex b4ded3d3a066..5e685718ed9b 100644--- a/frontend/src/scenes/hog-functions/metrics/HogFunctionEventEstimates.tsx+++ b/frontend/src/scenes/hog-functions/metrics/HogFunctionEventEstimates.tsx@@ -16,7 +16,7 @@ import { hogFunctionConfigurationLogic } from '../configuration/hogFunctionConficonst EVENT_THRESHOLD_ALERT_LEVEL = 8000export function HogFunctionEventEstimates(): JSX.Element | null {- const { sparkline, sparklineLoading, eventsDataTableNode, showEventsList, type, configuration } =+ const { sparkline, sparklineLoading, eventsDataTableNode, showEventsList, type, configuration, useMapping } =useValues(hogFunctionConfigurationLogic)const { setShowEventsList } = useActions(hogFunctionConfigurationLogic)@@ -33,28 +33,41 @@ export function HogFunctionEventEstimates(): JSX.Element | null {}const insightUrl = urls.insightNew({ type: InsightType.SQL, query: dataTableNode })+ const matchCount = sparkline?.count ?? 0+ const matchCountDescription = useMapping ? (+ <>+ At least one mapping matched{' '}+ <strong>+ {matchCount} event{matchCount !== 1 ? 's' : ''}+ </strong>+ </>+ ) : (+ <>+ This {type} would have triggered{' '}+ <strong>+ {matchCount} time{matchCount !== 1 ? 's' : ''}+ </strong>+ </>+ )return (<div className="relative p-3 rounded border deprecated-space-y-2 bg-surface-primary">- <LemonLabel>Matching events</LemonLabel>+ <LemonLabel>{useMapping ? 'Matching events across all mappings' : 'Matching events'}</LemonLabel>+ {useMapping ? (+ <p className="text-sm text-secondary">+ This preview counts each matching event once, even if it matches several mappings. Matchers only+ filter their own mapping, and one event may trigger multiple destination invocations.+ </p>+ ) : null}{sparkline && !sparklineLoading ? (<>{sparkline.count > EVENT_THRESHOLD_ALERT_LEVEL && type !== 'transformation' ? (<LemonBanner type="warning">- <b>Warning:</b> This destination would have triggered{' '}- <strong>- {sparkline.count ?? 0} time{sparkline.count !== 1 ? 's' : ''}- </strong>{' '}- in the last 7 days. Consider the impact of this function on your destination.+ <b>Warning:</b> {matchCountDescription} in the last 7 days. Consider the impact of this+ function on your destination.</LemonBanner>) : (- <p>- This {type} would have triggered{' '}- <strong>- {sparkline.count ?? 0} time{sparkline.count !== 1 ? 's' : ''}- </strong>{' '}- in the last 7 days.- </p>+ <p>{matchCountDescription} in the last 7 days.</p>)}{hasMasking && <p>The estimate does not take into account trigger options.</p>}