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.

refactor(heatmaps): edit the event filter with the series picker (#126220)

Thomas Obermüller36e2b7d2db86
diff --git a/frontend/src/lib/components/heatmaps/types.ts b/frontend/src/lib/components/heatmaps/types.ts
index d9746f13636e..a086fb20b1e9 100644
--- a/frontend/src/lib/components/heatmaps/types.ts
+++ b/frontend/src/lib/components/heatmaps/types.ts
@@ -4,7 +4,7 @@ export type CommonFilters = {
filter_test_accounts?: boolean
cohort_ids?: number[]
// A subset of FilterType['events'], spelled out because ~/types imports this file. Each entry is an
- // event name plus optional property filters, matching what ActionFilter emits.
+ // event name plus optional property filters, matching what the heatmap event picker emits.
events?: HeatmapEventFilter[]
}
diff --git a/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx b/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx
index 2d554fa98b24..75fbc6599538 100644
--- a/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx
+++ b/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx
@@ -7,7 +7,6 @@ import { LemonBadge, LemonButton, LemonLabel, LemonSelect } from '@posthog/lemon
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { HEATMAP_LOADING_DEBOUNCE_MS, heatmapDataLogic } from 'lib/components/heatmaps/heatmapDataLogic'
import { HeatmapsSettings } from 'lib/components/heatmaps/HeatMapsSettings'
-import { HeatmapEventFilter } from 'lib/components/heatmaps/types'
import { heatmapDateOptions } from 'lib/components/IframedToolbarBrowser/utils'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
@@ -18,15 +17,22 @@ import { Popover } from 'lib/lemon-ui/Popover'
import { inStorybook, inStorybookTestRunner } from 'lib/utils/dom'
import { percentage } from 'lib/utils/numbers'
import { COHORTS_ONLY_SUPPORT_IN_PICKER_PROPS } from 'scenes/feature-flags/cohortPickerProps'
-import { ActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFilter'
+import { SeriesActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFilter'
import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'
import { TestAccountFilter } from 'scenes/insights/filters/TestAccountFilter'
-import { AnyPropertyFilter, CohortPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'
+import {
+ AnyPropertyFilter,
+ ChartDisplayCategory,
+ CohortPropertyFilter,
+ PropertyFilterType,
+ PropertyOperator,
+} from '~/types'
import { HEATMAP_PRESET_WIDTHS } from './heatmapCoverage'
import { heatmapCoverageLogic } from './heatmapCoverageLogic'
import { HeatmapEmptyState } from './HeatmapEmptyState'
+import { heatmapEventFiltersToSeries, seriesToHeatmapEventFilters } from './heatmapEventSeries'
const cohortIdsToPropertyFilters = (ids: number[]): AnyPropertyFilter[] =>
ids.map((id) => ({
@@ -182,17 +188,16 @@ export function FilterPanel({
// The filter bar is a single row of controls, so the event list, which grows a
// row per event, sits in a popover rather than stretching the row it lives in.
<div className="p-2 w-96">
- <ActionFilter
+ <SeriesActionFilter
bordered
- filters={{ events: commonFilters?.events ?? [] }}
- setFilters={(filters) => {
+ series={heatmapEventFiltersToSeries(commonFilters?.events)}
+ onChange={(series) => {
setCommonFilters?.({
...commonFilters,
- // ActionFilter types events as the loose Record shape; narrow
- // back to what heatmapDataLogic serializes.
- events: (filters.events ?? []) as HeatmapEventFilter[],
+ events: seriesToHeatmapEventFilters(series),
})
}}
+ trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
typeKey="heatmap-events"
buttonCopy="Add event"
mathAvailability={MathAvailability.None}
diff --git a/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.test.ts b/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.test.ts
new file mode 100644
index 000000000000..360a92ecd4a3
--- /dev/null
+++ b/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.test.ts
@@ -0,0 +1,43 @@
+import { HeatmapEventFilter } from 'lib/components/heatmaps/types'
+import { SeriesNode } from 'scenes/insights/filters/ActionFilter/seriesNode'
+
+import { NodeKind } from '~/queries/schema/schema-general'
+
+import { heatmapEventFiltersToSeries, seriesToHeatmapEventFilters } from './heatmapEventSeries'
+
+const pricingPage = { key: '$current_url', value: 'pricing', operator: 'icontains', type: 'event' }
+const flagOn = { key: '$feature/new-checkout', value: ['true'], operator: 'exact', type: 'event' }
+
+describe('heatmapEventSeries', () => {
+ test.each<[string, HeatmapEventFilter[], HeatmapEventFilter[]]>([
+ [
+ 'entries come back in order with their names and properties',
+ [
+ { id: 'purchased', name: 'purchased', type: 'events', order: 4, properties: [flagOn] },
+ { id: '$pageview', name: '$pageview', type: 'events', order: 1, properties: [pricingPage] },
+ ],
+ [
+ { name: '$pageview', properties: [pricingPage], id: '$pageview', type: 'events', order: 0 },
+ { name: 'purchased', properties: [flagOn], id: 'purchased', type: 'events', order: 1 },
+ ],
+ ],
+ [
+ 'an entry with no order or type reads as an event',
+ [{ id: 'signed_up' }],
+ [{ id: 'signed_up', type: 'events', order: 0 }],
+ ],
+ ])('%s', (_, stored, expected) => {
+ expect(JSON.stringify(seriesToHeatmapEventFilters(heatmapEventFiltersToSeries(stored)))).toEqual(
+ JSON.stringify(expected)
+ )
+ })
+
+ test('a row the picker built drops its node version', () => {
+ const series = [
+ { kind: NodeKind.EventsNode, event: '$autocapture', name: 'Autocapture', version: 1 } as SeriesNode,
+ ]
+ expect(JSON.stringify(seriesToHeatmapEventFilters(series))).toEqual(
+ JSON.stringify([{ name: 'Autocapture', id: '$autocapture', type: 'events', order: 0 }])
+ )
+ })
+})
diff --git a/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.ts b/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.ts
new file mode 100644
index 000000000000..94f05816608b
--- /dev/null
+++ b/products/web_analytics/frontend/heatmaps/components/heatmapEventSeries.ts
@@ -0,0 +1,31 @@
+import { HeatmapEventFilter } from 'lib/components/heatmaps/types'
+import { convertPropertyGroupToProperties } from 'lib/components/PropertyFilters/utils'
+import { SeriesNode, isEventsSeriesNode } from 'scenes/insights/filters/ActionFilter/seriesNode'
+
+import { NodeKind } from '~/queries/schema/schema-general'
+import { EntityTypes } from '~/types'
+
+const NODE_IDENTITY_KEYS = ['kind', 'version', 'event', 'id', 'nodes']
+
+// The `events` request parameter is the JSON of these entries, so their key order is part of the wire format.
+export function heatmapEventFiltersToSeries(events: HeatmapEventFilter[] | undefined): SeriesNode[] {
+ return [...(events ?? [])]
+ .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
+ .map(({ type, id, order, ...shared }) => {
+ if (Array.isArray(shared.properties)) {
+ shared.properties = convertPropertyGroupToProperties(shared.properties)
+ }
+ return { ...shared, kind: NodeKind.EventsNode, event: id } as SeriesNode
+ })
+}
+
+export function seriesToHeatmapEventFilters(series: SeriesNode[]): HeatmapEventFilter[] {
+ const filters: HeatmapEventFilter[] = []
+ series.forEach((node, order) => {
+ if (isEventsSeriesNode(node)) {
+ const shared = Object.fromEntries(Object.entries(node).filter(([key]) => !NODE_IDENTITY_KEYS.includes(key)))
+ filters.push({ ...shared, id: node.event ?? null, type: EntityTypes.EVENTS, order })
+ }
+ })
+ return filters
+}