: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(toolbar): filter heatmaps by experiment variant (#129421)
Javier Bahamondes145f44267fbb
diff --git a/frontend/src/lib/components/heatmaps/heatmapVariantFilter.ts b/frontend/src/lib/components/heatmaps/heatmapVariantFilter.tsnew file mode 100644index 000000000000..84ed11007e9c--- /dev/null+++ b/frontend/src/lib/components/heatmaps/heatmapVariantFilter.ts@@ -0,0 +1,64 @@+import { AnyPropertyFilter, CombinedFeatureFlagAndValueType, PropertyFilterType, PropertyOperator } from '~/types'++import type { HeatmapEventFilter } from './types'++export type HeatmapVariantFilter = {+ flagKey: string+ variant: string+}++export type HeatmapVariantFlag = {+ flagKey: string+ variants: HeatmapVariantFilter[]+}++const FLAG_CALLED_EVENT = '$feature_flag_called'++// Heatmap rows carry no event properties, so the heatmap keeps the sessions where the flag returned this variant.+export function variantToEventFilter({ flagKey, variant }: HeatmapVariantFilter): HeatmapEventFilter {+ return {+ id: FLAG_CALLED_EVENT,+ name: FLAG_CALLED_EVENT,+ type: 'events',+ order: 0,+ properties: [+ { key: '$feature_flag', value: flagKey, operator: PropertyOperator.Exact, type: PropertyFilterType.Event },+ {+ key: '$feature_flag_response',+ value: variant,+ operator: PropertyOperator.Exact,+ type: PropertyFilterType.Event,+ },+ ],+ }+}++export function variantFromEventFilters(events: HeatmapEventFilter[] | undefined): HeatmapVariantFilter | null {+ const flagCalled = events?.find((event) => event.id === FLAG_CALLED_EVENT)+ const valueOf = (key: string): unknown => flagCalled?.properties?.find((property) => property.key === key)?.value+ const flagKey = valueOf('$feature_flag')+ const variant = valueOf('$feature_flag_response')+ return typeof flagKey === 'string' && typeof variant === 'string' ? { flagKey, variant } : null+}++export function variantFlagsFromUserFlags(userFlags: CombinedFeatureFlagAndValueType[]): HeatmapVariantFlag[] {+ return userFlags+ .filter(({ feature_flag }) => feature_flag.filters?.multivariate?.variants?.length)+ .map(({ feature_flag }) => ({+ flagKey: feature_flag.key,+ variants: (feature_flag.filters.multivariate?.variants ?? []).map(({ key }) => ({+ flagKey: feature_flag.key,+ variant: key,+ })),+ }))+}++// Autocapture events carry the $feature/<key> property, so the clickmap can filter each click directly.+export function variantToElementStatsProperty({ flagKey, variant }: HeatmapVariantFilter): AnyPropertyFilter {+ return {+ key: `$feature/${flagKey}`,+ value: variant,+ operator: PropertyOperator.Exact,+ type: PropertyFilterType.Event,+ }+}diff --git a/frontend/src/toolbar/extension/components/stats/ExtensionHeatmapMenu.tsx b/frontend/src/toolbar/extension/components/stats/ExtensionHeatmapMenu.tsxindex b3a3b16f988d..cb6fc4ad3cb8 100644--- a/frontend/src/toolbar/extension/components/stats/ExtensionHeatmapMenu.tsx+++ b/frontend/src/toolbar/extension/components/stats/ExtensionHeatmapMenu.tsx@@ -145,7 +145,7 @@ export function ExtensionHeatmapMenu(): JSX.Element {onChange={(dateFrom, dateTo) =>sendHeatmapCommand({type: 'set-common-filters',- filters: { date_from: dateFrom, date_to: dateTo },+ filters: { ...heatmap.commonFilters, date_from: dateFrom, date_to: dateTo },})}dateOptions={heatmapDateOptions}diff --git a/frontend/src/toolbar/floating/components/stats/HeatmapToolbarMenu.tsx b/frontend/src/toolbar/floating/components/stats/HeatmapToolbarMenu.tsxindex 757b1b3e51d2..8c95ffac776b 100644--- a/frontend/src/toolbar/floating/components/stats/HeatmapToolbarMenu.tsx+++ b/frontend/src/toolbar/floating/components/stats/HeatmapToolbarMenu.tsx@@ -13,6 +13,7 @@ import { Spinner } from 'lib/lemon-ui/Spinner'import { Tooltip } from 'lib/lemon-ui/Tooltip'import { ToolbarMenu } from '~/toolbar/floating/components/bar/ToolbarMenu'+import { HeatmapVariantPicker } from '~/toolbar/floating/components/stats/HeatmapVariantPicker'import { elementsLogic } from '~/toolbar/shared/elements/elementsLogic'import { heatmapToolbarMenuLogic } from '~/toolbar/shared/elements/heatmapToolbarMenuLogic'import { currentPageLogic } from '~/toolbar/shared/stats/currentPageLogic'@@ -183,15 +184,16 @@ export const HeatmapToolbarMenu = (): JSX.Element => {</p>)}- <div className="flex flex-row items-center gap-2 py-2 border-b">+ <div className="flex flex-row flex-wrap items-center gap-2 py-2 border-b"><DateFilterdateFrom={commonFilters.date_from}dateTo={commonFilters.date_to}onChange={(fromDate, toDate) => {- setCommonFilters({ date_from: fromDate, date_to: toDate })+ setCommonFilters({ ...commonFilters, date_from: fromDate, date_to: toDate })}}dateOptions={heatmapDateOptions}/>+ <HeatmapVariantPicker /><LemonButtonsize="small"type="secondary"diff --git a/frontend/src/toolbar/floating/components/stats/HeatmapVariantPicker.tsx b/frontend/src/toolbar/floating/components/stats/HeatmapVariantPicker.tsxnew file mode 100644index 000000000000..09c4fb34ed1c--- /dev/null+++ b/frontend/src/toolbar/floating/components/stats/HeatmapVariantPicker.tsx@@ -0,0 +1,59 @@+import { useActions, useValues } from 'kea'+import { useEffect } from 'react'++import { LemonSelect, LemonSelectSection } from '@posthog/lemon-ui'++import { HeatmapVariantFilter } from 'lib/components/heatmaps/heatmapVariantFilter'+import { FEATURE_FLAGS } from 'lib/constants'++import { heatmapToolbarMenuLogic } from '~/toolbar/shared/elements/heatmapToolbarMenuLogic'+import { flagsToolbarLogic } from '~/toolbar/shared/flags/flagsToolbarLogic'+import { useToolbarFeatureFlag } from '~/toolbar/shared/toolbarPosthogJS'++const optionKey = ({ flagKey, variant }: HeatmapVariantFilter): string => JSON.stringify([flagKey, variant])++export function HeatmapVariantPicker(): JSX.Element | null {+ const eventFilterEnabled = useToolbarFeatureFlag(FEATURE_FLAGS.HEATMAPS_EVENT_FILTER)+ const { heatmapVariantFilter, heatmapVariantFlags } = useValues(heatmapToolbarMenuLogic)+ const { setHeatmapVariantFilter } = useActions(heatmapToolbarMenuLogic)+ const { getUserFlags } = useActions(flagsToolbarLogic)++ useEffect(() => {+ if (eventFilterEnabled) {+ getUserFlags()+ }+ }, [eventFilterEnabled, getUserFlags])++ // A persisted filter stays visible so it can be cleared, even when its flag or the gate no longer loads.+ if (!heatmapVariantFilter && (!eventFilterEnabled || !heatmapVariantFlags.length)) {+ return null+ }++ const sections: LemonSelectSection<string | null>[] = heatmapVariantFlags.map(({ flagKey, variants }) => ({+ title: flagKey,+ options: variants.map((variantFilter) => ({ label: variantFilter.variant, value: optionKey(variantFilter) })),+ }))++ return (+ <LemonSelect+ size="small"+ dropdownMatchSelectWidth={false}+ data-attr="heatmap-variant-filter"+ tooltip="Heatmap: sessions that were shown this variant. Clickmap: clicks from people in this variant, even if they weren't shown it."+ value={heatmapVariantFilter ? optionKey(heatmapVariantFilter) : null}+ options={[{ options: [{ label: 'All variants', value: null }] }, ...sections]}+ renderButtonContent={() =>+ heatmapVariantFilter+ ? `${heatmapVariantFilter.flagKey}: ${heatmapVariantFilter.variant}`+ : 'All variants'+ }+ onChange={(value) =>+ setHeatmapVariantFilter(+ heatmapVariantFlags+ .flatMap(({ variants }) => variants)+ .find((variantFilter) => optionKey(variantFilter) === value) ?? null+ )+ }+ />+ )+}diff --git a/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.test.ts b/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.test.tsindex 530fe1341a78..9c11387789e2 100644--- a/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.test.ts+++ b/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.test.ts@@ -7,6 +7,7 @@ import { initKeaTests } from '~/test/init'import { currentPageLogic } from '~/toolbar/shared/stats/currentPageLogic'import { toolbarApi } from '~/toolbar/shared/toolbarApi'import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'+import { toolbarPosthogJS } from '~/toolbar/shared/toolbarPosthogJS'import { ElementsEventType } from '~/toolbar/shared/types'import { TOOLBAR_ROOT_ATTRIBUTE } from '~/toolbar/shared/utils'@@ -484,6 +485,33 @@ describe('heatmapToolbarMenuLogic', () => {}})+ it.each([+ { eventFilterEnabled: true, expectedProperties: 2 },+ { eventFilterEnabled: false, expectedProperties: 1 },+ ])(+ 'reloads the clickmap with the variant filter only when the event filter flag is $eventFilterEnabled',+ async ({ eventFilterEnabled, expectedProperties }) => {+ jest.spyOn(toolbarPosthogJS, 'getFeatureFlag').mockReturnValue(eventFilterEnabled)+ await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([+ 'getElementStatsSuccess',+ ])++ const variantFilter = { flagKey: 'checkout', variant: 'test' }+ await expectLogic(logic, () => logic.actions.setHeatmapVariantFilter(variantFilter)).toDispatchActions([+ 'getElementStatsSuccess',+ ])++ expect(logic.values.heatmapVariantFilter).toEqual(variantFilter)+ const lastCall = (toolbarApi.elementStats.list as jest.Mock).mock.calls.at(-1)[0]+ expect(lastCall.properties).toHaveLength(expectedProperties)+ if (eventFilterEnabled) {+ expect(lastCall.properties).toContainEqual(+ expect.objectContaining({ key: '$feature/checkout', value: 'test', type: 'event' })+ )+ }+ }+ )+it('loads every remaining page when load all starts, and ends the run on the last page', async () => {jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({ok: true,diff --git a/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.ts b/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.tsindex 0d6ca50f7d48..72107551e157 100644--- a/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.ts+++ b/frontend/src/toolbar/shared/elements/heatmapToolbarMenuLogic.ts@@ -23,6 +23,14 @@ import {heatmapUrlPatternToRegex,resolveHeatmapUrlFilter,} from 'lib/components/heatmaps/heatmapUrlMatch'+import {+ HeatmapVariantFilter,+ HeatmapVariantFlag,+ variantFlagsFromUserFlags,+ variantFromEventFilters,+ variantToElementStatsProperty,+ variantToEventFilter,+} from 'lib/components/heatmaps/heatmapVariantFilter'import { HeatmapBoundsFilter, ScrollDepthPosthogJsError } from 'lib/components/heatmaps/types'import { FEATURE_FLAGS } from 'lib/constants'import { createSliceYielder } from 'lib/utils/async'@@ -37,6 +45,7 @@ import {matchEventToElementUsingSelectors,matchIsChainConsistent,} from '~/toolbar/shared/elements/domElementIndex'+import { flagsToolbarLogic } from '~/toolbar/shared/flags/flagsToolbarLogic'import { productToursLogic } from '~/toolbar/shared/product-tours/productToursLogic'import { currentPageLogic } from '~/toolbar/shared/stats/currentPageLogic'import { toolbarApi } from '~/toolbar/shared/toolbarApi'@@ -52,7 +61,7 @@ import {invalidateZoomCache,trimElement,} from '~/toolbar/shared/utils'-import { AnyPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'+import { AnyPropertyFilter, CombinedFeatureFlagAndValueType, PropertyFilterType, PropertyOperator } from '~/types'import type { HrefMatchType } from '../../../lib/components/heatmaps/heatmapDataLogic'import type { CommonFilters, HeatmapFilters, HeatmapFixedPositionMode } from '../../../lib/components/heatmaps/types'@@ -203,6 +212,13 @@ export function buildElementStatsProperties(return properties}+// The heatmap API drops the variant when this flag is off, so the clickmap drops it too.+function clickmapVariantProperties(variantFilter: HeatmapVariantFilter | null): AnyPropertyFilter[] {+ return variantFilter && toolbarPosthogJS.getFeatureFlag(FEATURE_FLAGS.HEATMAPS_EVENT_FILTER)+ ? [variantToElementStatsProperty(variantFilter)]+ : []+}+// mirrors how posthog-js decides target_fixed when recording heatmap points, so an area// and its points classify the same wayexport function isInFixedContainer(element: HTMLElement): boolean {@@ -344,6 +360,7 @@ const emptyElementsStatsPages: PaginatedResponse<ElementsEventType> = {export interface heatmapToolbarMenuLogicValues {href: string // currentPageLogicwildcardHref: string // currentPageLogic+ userFlags: CombinedFeatureFlagAndValueType[] // flagsToolbarLogiccommonFilters: CommonFilters // heatmapDataLogicdateRange: string | null // heatmapDataLogicheatmapColorPalette: string | null // heatmapDataLogic@@ -380,6 +397,8 @@ export interface heatmapToolbarMenuLogicValues {selector: string | null} | nullheatmapEnabled: boolean+ heatmapVariantFilter: HeatmapVariantFilter | null+ heatmapVariantFlags: HeatmapVariantFlag[]highestClickCount: numberisRefreshing: booleanlastElementStatsRequest: {@@ -563,6 +582,9 @@ export interface heatmapToolbarMenuLogicActions {element: HTMLElement | nullselector: string | null}+ setHeatmapVariantFilter: (variantFilter: HeatmapVariantFilter | null) => {+ variantFilter: HeatmapVariantFilter | null+ }setIsRefreshing: (isRefreshing: boolean) => {isRefreshing: boolean}@@ -611,6 +633,8 @@ export interface heatmapToolbarMenuLogicActions {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface heatmapToolbarMenuLogicMeta {__keaTypeGenInternalSelectorTypes: {+ heatmapVariantFilter: (commonFilters: CommonFilters) => HeatmapVariantFilter | null+ heatmapVariantFlags: (userFlags: CombinedFeatureFlagAndValueType[]) => HeatmapVariantFlag[]countedElements: (processedElements: CountedHTMLElement[],elementMetrics: Map<@@ -665,6 +689,8 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([['href', 'wildcardHref'],toolbarConfigLogic,['posthog'],+ flagsToolbarLogic,+ ['userFlags'],heatmapDataLogic,['commonFilters',@@ -727,6 +753,7 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([stepAreaHover: (direction: 'up' | 'down') => ({ direction }),setAreaCandidates: (candidates: HTMLElement[]) => ({ candidates }),selectHeatmapAreaFilter: (element: HTMLElement | null) => ({ element }),+ setHeatmapVariantFilter: (variantFilter: HeatmapVariantFilter | null) => ({ variantFilter }),setHeatmapAreaFilter: (element: HTMLElement | null, selector: string | null) => ({ element, selector }),// swap the tracked node for a re-resolved one without refetching: the selector is// unchanged, so the server response would be byte-identical@@ -925,11 +952,14 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([await toolbarApi.elementStats.page(url, options): await toolbarApi.elementStats.list({- properties: buildElementStatsProperties(- href,- wildcardHref,- values.heatmapAreaFilter?.selector ?? null- ),+ properties: [+ ...buildElementStatsProperties(+ href,+ wildcardHref,+ values.heatmapAreaFilter?.selector ?? null+ ),+ ...clickmapVariantProperties(values.heatmapVariantFilter),+ ],date_from: values.commonFilters.date_from,date_to: values.commonFilters.date_to,paginate_response: true,@@ -972,6 +1002,16 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([],})),selectors(() => ({+ heatmapVariantFilter: [+ (s) => [s.commonFilters],+ (commonFilters: CommonFilters): HeatmapVariantFilter | null =>+ variantFromEventFilters(commonFilters.events),+ ],+ heatmapVariantFlags: [+ (s) => [s.userFlags],+ (userFlags: CombinedFeatureFlagAndValueType[]): HeatmapVariantFlag[] =>+ variantFlagsFromUserFlags(userFlags),+ ],countedElements: [(s) => [s.processedElements, s.elementMetrics],(@@ -1277,6 +1317,14 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([}},+ setHeatmapVariantFilter: ({ variantFilter }) => {+ actions.setCommonFilters({+ ...values.commonFilters,+ events: variantFilter ? [variantToEventFilter(variantFilter)] : [],+ })+ toolbarPosthogJS.capture('toolbar heatmap variant filter changed', { enabled: !!variantFilter })+ },+selectHeatmapAreaFilter: ({ element }) => {const selector = element? elementToActionStep(element, toolbarConfigLogic.values.dataAttributes).selector || null