: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(heatmaps): filter heatmap data on the width the preview renders at (#103031)
Jordan Mryyan4047621143c5
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex c9885872bdd9..f62aa746ae09 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -8801,13 +8801,13 @@ snapshots:scenes-app-heatmap--generating--light:hash: v1.k794b7964.3bb8caf591d48254690d88d7fd91f56d37f174fd436856e05068fc48b6447c8c.sj0fwc61KHPb3o_hlXH-J5n47Aa8TVTmCRmQvWWcaDMscenes-app-heatmap--iframe-example--dark:- hash: v1.k794b7964.5cd4e010533b3df3e82a89ad6eab978713942df82b20067abb3c2f37d6374265.VI_PTwwHdTsl9_8T8-_D3XBPwot4pnnptq1qjcoJJYs+ hash: v1.k794b7964.edc8641f41da1365aaa55e532b36b6fe272234b86f6ce1ef317d0d3d586eac79.kgs_gmdNN7IN2c8RZPsQrShFeZ8ChwmTy0atI73o1wsscenes-app-heatmap--iframe-example--light:- hash: v1.k794b7964.77385266e4a3b6119b954573b48ace591c15f1f76d844b8207a7e029a5f79f2d.THx-n62EiRghRMUtPRrKH_MKrT0AJVNd1ow6zFj8PxQ+ hash: v1.k794b7964.6fda90f06ebf5bbce69387216ff601333f61f245ae3d1b85ce44a550cf57d293.VbH_pBdTsLOFUYFRyAAextprvOpeuvmAqDeC-qHraFgscenes-app-heatmap--iframe-example-with-event-filter--dark:- hash: v1.k794b7964.8129d0645850536d3dabffb1bbb35740ecf8c4f974e4062eaa994deb74907324.I8PWfU6icIsVf76tg0E1y51m_XlpER5BG9gDTXUAeis+ hash: v1.k794b7964.08c2dc775da48f30794ee09ecccc94f46128d266f61c21ab97ff27e8602b05b8.zHSrBS5hw2G4HsrcwUAPNUd1mFVXHYhNv3lwrp8NAooscenes-app-heatmap--iframe-example-with-event-filter--light:- hash: v1.k794b7964.12522493c0ab3a98896d37a0592ee7beb478d734b45a1e264f33abec05060bd3.zB3z-DIVgnJwfPMgn1AmZkZkk6fNdodeLKC7TbShX1A+ hash: v1.k794b7964.171fb140a6c5d3bc5d62be40dc27f18f3bbaa1b742f29da071e7993c569f5886._zyomdb0yuLLhatWsYZgh481hj3CDOQf-wn_uyJ_MQMscenes-app-heatmap--new--dark:hash: v1.k794b7964.cfd075c10189496c867372e36391cf5985948fe5a8137c434545f5825b15ac7e.IUvQTi7vHNPSGeWj-q6upJsHMLU3UpumxHwGBndOhYEscenes-app-heatmap--new--light:@@ -8817,33 +8817,33 @@ snapshots:scenes-app-heatmap--page-settings-dialog--light:hash: v1.k794b7964.bbd49afc0166048d488899000747149fce60df059a3c9f89110d1a89f0c80795.QDtUmw44ex7iWirnh2Mod5h9M1h7LGp4aGpYZkEymfwscenes-app-heatmap--page-settings-dialog-narrow--dark:- hash: v1.k794b7964.d2dd7cb331a66c6bf5344f442980539fc729056d108ce5efaa9930acf0c501c3.qeUqv8SuN2ydGLqxXUwZl-GaiL8d3Fw3eAJ6JFVmXoY+ hash: v1.k794b7964.dc9236733578bf1b20f499de8c697575a9e115a505914c59e14988ed0dcea7af.w1S7AM2Bd1zMLvEvIK9-y9bBV922KqBN9zFS1M_TAokscenes-app-heatmap--page-settings-dialog-narrow--light:- hash: v1.k794b7964.3ba010ca2cd3b9e0938b9ee241a6183e96c9cf6effca4e09a678650683f89551.1VzbFVxVUrsFCRv9rJChRoJw6YKdhlcmrUMUNCWnytw+ hash: v1.k794b7964.c6fdd505da1fa2bf151c3fa554e3a93175b83f43a383c6a34a895dde0d7a7f45.EROVhdTYOT9KVpt7vdZswHP-v3dg9XbRPD7tKVW3Oj8scenes-app-heatmap--save-failed--dark:- hash: v1.k794b7964.3ad94c47ac1c47a13f60b1ea1def7846114364892e1d7184ef7b1e8b36792872.l_DBYv4Fl4b1YFuAX6rdlibaohCHCk2deiNOu5brmLQ+ hash: v1.k794b7964.e57ce09d875faa5a71687924d5191b6179e05d7bd6259ad668d125652aa6bd35.acWCHIYblfCuUBXaFZVTLsSNcWc1DQ5x85EXTKkzaKcscenes-app-heatmap--save-failed--light:- hash: v1.k794b7964.9b817d6a0e8ddbd7a518384ea5d98e646c3949ce92d6200e83fa69f88de3aa8a.6hIbOKTEYmnJaTZLheW3mCQvQnxBIs9BPNDqiWUvHi0+ hash: v1.k794b7964.e88e69f1b26d536373f325c4d1169091ce2e5c28b0ccceca978c547f7b33a6ff.Zg46pHfiUC6OgYMEvH0h1pjatreYfqUnFKiLijwlqZYscenes-app-heatmap--saved-successfully--dark:- hash: v1.k794b7964.5bff95f2e4dc2406c9627dd27d885e65bf5d30c0d470295a008ef2b449919947.wmXXs5ltQcLsXn6RSVQR9m8X4y9DG89K6WyD5yueIVA+ hash: v1.k794b7964.e314d67704def067476743c09ef3478e5c42dba7568b7d1692ac65058bfad0e6.QIipPIRgvThQuMQ3_myLIl7E5EmlzT7eUsa9hUmrB_Iscenes-app-heatmap--saved-successfully--light:- hash: v1.k794b7964.3c63a9dd9532a9a1b2173c575b6e297c2750657c91962aac5480d1285e25defe.PFMbnx3VY55-ViwcCHSHAID3voKjAbqOsK3Z023SDe8+ hash: v1.k794b7964.6d7667be266698d790399d0e1d94049a271146040c8a22ac91668e915912581b.sK7xk20UTF2wVjajXvDM7DZuIcKTtkn4UCD3OU7AbbAscenes-app-heatmap--saving--dark:- hash: v1.k794b7964.3d945a42a61064e4623a0f9dfcc796c8491f4f624aa882249e431d65bafc4f1f.NGHkVYXMric8yrowQZvqlma8WE6EL2EEScMy0zKRzlM+ hash: v1.k794b7964.4137108dcc7db8b5e16dfd6111c8189c1bd11043ff68df1322d0f5e2c2c44563.RE6LdPOhuTVhXDYmpJHH9shjywgYiog7io7W15q3HSQscenes-app-heatmap--saving--light:- hash: v1.k794b7964.f7e2f6089b103a48a44262857cb702c92a3e87534c08b330412d780d06a70501.ngtXoU_R6vMjgOZUivwnEucf6a4Eze6hFPiV19tT_9w+ hash: v1.k794b7964.fc2f22a4edd69c921d8d18fe4872612e54cf30d2e17a3f6db0e73cabbc0c06b8.fQcv8KIXV8aYIw0IwSFgqAPpuqFaSrcsp_CcrGmc-rsscenes-app-heatmap--switched-to-screenshot--dark:hash: v1.k794b7964.b214a15aca3b81c4ff73b8809f072975bbd431674c243e64e852f094c58cd296._MD7mE8e5TT33y0FDLPBEbIoZRpT7OYP_PLHuh1iJoEscenes-app-heatmap--switched-to-screenshot--light:hash: v1.k794b7964.458e49e51823b6c7e44eeea9b9c2931b002ddc4664a6b1f189e96ff1d6e1ae76.tA_kAuZxPz81nTPb80j45HRmR5Qi_N8l9Up2sf1ajvUscenes-app-heatmap--unsaved-changes--dark:- hash: v1.k794b7964.3ad94c47ac1c47a13f60b1ea1def7846114364892e1d7184ef7b1e8b36792872.lf_lmvHtnUO_nUcFt3fxeTn0OvqhrUrXxSdijdu6G84+ hash: v1.k794b7964.e57ce09d875faa5a71687924d5191b6179e05d7bd6259ad668d125652aa6bd35.R1ttFz0wfhk9gMErB7eEUHWPzA8TdKMErVpDKLfNYcMscenes-app-heatmap--unsaved-changes--light:- hash: v1.k794b7964.9b817d6a0e8ddbd7a518384ea5d98e646c3949ce92d6200e83fa69f88de3aa8a.bcLQMl06OeJSrBNgj4j0UHnlJp0cMH7MKDeZzrB6o5s+ hash: v1.k794b7964.e88e69f1b26d536373f325c4d1169091ce2e5c28b0ccceca978c547f7b33a6ff.oBwPYQgKlLhmRY5yQ_gq_O-JjbOlbq-pG-8C4vkvjnQscenes-app-heatmap--unsaved-changes-narrow--dark:- hash: v1.k794b7964.a0af73f9dc5f7736bb6f693cd7f5f612792544bca2031faf5bf2edd0d5fb4265.j_X9JteXJbQmsJKgTpOi0ju5BzHxttWRodzSofa1lmE+ hash: v1.k794b7964.5163b9515316006e9782b8bc3bfdd6ed1502adae085b8545aa4dd5453a500fde.Bi-9jv_5rHXUFUCGaG__5HvdyjZCzP0htzR2_lwCFjUscenes-app-heatmap--unsaved-changes-narrow--light:- hash: v1.k794b7964.51752209460601bd0c092efa1b558ce8e7463f9fc57abb3864e86a68f4ca7481.9k7F4Q_00I99zqd3sU6sddLo-L7LjG6gM8MPri7UTng+ hash: v1.k794b7964.cb39ef97453abf79fbd308d4fd61891f187837ea05a2b2812ac8005ea8d967a4.wZgL8UFkr23EH3sZOeYXQF9tmTWdLt6R7Z1OA4D8pjoscenes-app-heatmap-new--authenticated-review--dark:hash: v1.k794b7964.631bc050a0445eb0018c9ec039bd86da951df11350757600c0ccc9f25b2f7d90.YMjSo9vcVG36wTfwJwv89REgXYgfZq3tRnVRzAgQIVEscenes-app-heatmap-new--authenticated-review--light:@@ -8881,9 +8881,9 @@ snapshots:scenes-app-heatmap-new--unauthorized-iframe--light:hash: v1.k794b7964.8e355023754bf9c2c1dc31cbadba79e6b524ad9a66528c649ab150f7eaf0cc05.yqUC0NlgolBVjXQsucJcRfdlu5npQvynKZSy7oAnd_8scenes-app-heatmap-recording--recording-mode-with-clickmap--dark:- hash: v1.k794b7964.6c923e6ba9f58ab555cc4dc2b9c1789d8a72f549765ddde70c1f3a7b73cdfbe6.QQa2mS_nUNyAH6iXIu6RYQ5wu9YFzB6GjsocU8M4PmQ+ hash: v1.k794b7964.9369ded2efa326a9d9adaec0de1efb86baa001b7ee4dca6e5fa2446379413c9d.nxftFTmTFr1jVHJQssBp-fdpgkojfMux1YVzeJVtSfkscenes-app-heatmap-recording--recording-mode-with-clickmap--light:- hash: v1.k794b7964.09522d7c70db3016e974a1cc8def11a5c9594e2f0f4df50fa4cf290d54129c07.kwWeHESZph4vfH_U-uOGsccGiePlAsCnCzuQvd9ptnM+ hash: v1.k794b7964.966fa455f5651c32d6e6b0cf7092325c4ebb917a8c67eb372598c3bab977e5de.TE80vbs-abW4Nu6I2L_Uw-MHMs5w5xESaNBqrCXiorQscenes-app-heatmaps-saved--list--dark:hash: v1.k794b7964.8368377e136c567c3c500c97924486837234c230858baf0eb14678b6900d68c6.qpjMZIE3Ii8vpyLrfaKjCdym7waJtp38DGez1iugMMoscenes-app-heatmaps-saved--list--light:diff --git a/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsx b/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsxindex d92b118cc405..7d22ff553ea9 100644--- a/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsx+++ b/frontend/src/lib/components/heatmaps/HeatmapCanvas.test.tsx@@ -3,6 +3,7 @@ import { fireEvent, render } from '@testing-library/react'import { initKeaTests } from '~/test/init'import { HeatmapCanvas } from './HeatmapCanvas'+import { heatmapDataLogic } from './heatmapDataLogic'const mockSetData = jest.fn()const mockConfigure = jest.fn()@@ -44,6 +45,26 @@ describe('HeatmapCanvas', () => {expect(container.querySelector('[data-attr="heatmap-canvas"]')).toBeTruthy()})+ it('places and hit-tests points at the rendered width when the preview is narrower than the analysis width', () => {+ const logic = heatmapDataLogic({ context: 'in-app' })+ logic.mount()+ logic.actions.setWindowWidthOverride(1024)+ logic.actions.loadHeatmapSuccess({+ results: [{ pointer_relative_x: 0.5, pointer_target_fixed: false, pointer_y: 100, count: 3 }],+ })++ const { container } = render(<HeatmapCanvas context="in-app" widthOverride={512} />)++ expect(mockSetData).toHaveBeenLastCalledWith(expect.objectContaining({ data: [{ x: 256, y: 50, value: 3 }] }))++ fireEvent.click(container.querySelector('[data-attr="heatmap-canvas"]')!, { clientX: 256, clientY: 50 })+ expect(logic.values.selectedArea?.points).toEqual([{ x: 0.5, y: 100, target_fixed: false }])++ logic.actions.clearSelectedArea()+ fireEvent.click(container.querySelector('[data-attr="heatmap-canvas"]')!, { clientX: 512, clientY: 100 })+ expect(logic.values.selectedArea).toBeNull()+ })+it('does not crash when getValueAt throws during mouse tracking', () => {mockGetValueAt.mockImplementation(() => {// Firefox throws a raw non-Error value from a broken canvasdiff --git a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsxindex b86f23e485eb..84eb5837c24c 100644--- a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx+++ b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx@@ -3,7 +3,7 @@ import { useActions, useValues } from 'kea'import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } from 'react'import { heatmapDataLogic } from 'lib/components/heatmaps/heatmapDataLogic'-import { HeatmapAreaPoint } from 'lib/components/heatmaps/types'+import { HeatmapAreaPoint, HeatmapJsData } from 'lib/components/heatmaps/types'import { useShiftKeyPressed } from 'lib/components/heatmaps/useShiftKeyPressed'import { cn } from 'lib/utils/css-classes'import { pluralize } from 'lib/utils/strings'@@ -106,7 +106,7 @@ export function HeatmapCanvas({heightOverride,heatmapFixedPositionMode,filteredHeatmapElements,- windowWidthOverride,+ analysisWidth,} = useValues(heatmapDataLogic({ context, exportToken }))const { setSelectedArea } = useActions(heatmapDataLogic({ context, exportToken }))@@ -114,6 +114,7 @@ export function HeatmapCanvas({const heatmapsJsContainerRef = useRef<HTMLDivElement | null>()const isToolbar = context === 'toolbar'const { innerRef, scrollYRef } = useScrollSync(isToolbar)+ const renderScale = widthOverride && analysisWidth > 0 ? widthOverride / analysisWidth : 1const [hasValueUnderMouse, setHasValueUnderMouse] = useState(false)const heatmapJSColorGradient = useMemo((): Record<string, string> => {@@ -131,8 +132,22 @@ export function HeatmapCanvas({}}, [heatmapColorPalette])- const heatmapJsDataRef = useRef(heatmapJsData)- heatmapJsDataRef.current = heatmapJsData+ const renderedHeatmapJsData = useMemo(+ (): HeatmapJsData =>+ renderScale === 1+ ? heatmapJsData+ : {+ ...heatmapJsData,+ data: heatmapJsData.data.map((point) => ({+ ...point,+ x: Math.round(point.x * renderScale),+ y: Math.round(point.y * renderScale),+ })),+ },+ [heatmapJsData, renderScale]+ )+ const heatmapJsDataRef = useRef(renderedHeatmapJsData)+ heatmapJsDataRef.current = renderedHeatmapJsDataconst heatmapJSColorGradientRef = useRef(heatmapJSColorGradient)heatmapJSColorGradientRef.current = heatmapJSColorGradient@@ -142,15 +157,16 @@ export function HeatmapCanvas({const clickX = e.clientX - rect.leftconst clickY = isToolbar ? e.clientY - rect.top + scrollYRef.current : e.clientY - rect.top- const width = windowWidthOverride ?? windowWidth+ const renderedWidth = analysisWidth * renderScale// Find all elements within CLICK_RADIUS_PX of the clickconst nearbyElements: HeatmapAreaPoint[] = []let totalCount = 0for (const element of filteredHeatmapElements) {- const visualX = element.xPercentage * width- const distance = Math.sqrt(Math.pow(clickX - visualX, 2) + Math.pow(clickY - element.y, 2))+ const visualX = element.xPercentage * renderedWidth+ const visualY = element.y * renderScale+ const distance = Math.sqrt(Math.pow(clickX - visualX, 2) + Math.pow(clickY - visualY, 2))if (distance <= CLICK_RADIUS_PX) {nearbyElements.push({@@ -171,7 +187,7 @@ export function HeatmapCanvas({})}},- [filteredHeatmapElements, windowWidth, windowWidthOverride, setSelectedArea, isToolbar, scrollYRef]+ [filteredHeatmapElements, analysisWidth, renderScale, setSelectedArea, isToolbar, scrollYRef])const setHeatmapContainer = useCallback((container: HTMLDivElement | null): void => {@@ -195,11 +211,11 @@ export function HeatmapCanvas({useEffect(() => {try {- heatmapsJsRef.current?.setData(heatmapJsData)+ heatmapsJsRef.current?.setData(renderedHeatmapJsData)} catch (e) {console.error('error setting data', e)}- }, [heatmapJsData])+ }, [renderedHeatmapJsData])useEffect(() => {if (!heatmapsJsContainerRef.current) {diff --git a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts b/frontend/src/lib/components/heatmaps/heatmapDataLogic.tsindex 87de1a6f7529..79e8a3aa3317 100644--- a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts+++ b/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts@@ -165,6 +165,7 @@ export function isWithinBounds(// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface heatmapDataLogicValues {+ analysisWidth: numberareaEvents: HeatmapEventsResponse | nullareaEventsLoading: booleanareaEventsLoadingMore: boolean@@ -313,15 +314,15 @@ export interface heatmapDataLogicMeta {__keaTypeGenInternalSelectorTypes: {dateRange: (commonFilters: CommonFilters) => string | nullheatmapElements: (rawHeatmap: HeatmapResponseType | null) => HeatmapElement[]+ analysisWidth: (windowWidthOverride: number | null, windowWidth: number) => numberviewportRange: (heatmapFilters: HeatmapFilters,- windowWidth: number,- windowWidthOverride: number | null+ analysisWidth: number) => {max: numbermin: number}- widthOverride: (windowWidthOverride: number | null) => number+ widthOverride: (analysisWidth: number) => numberheatmapTooltipNoun: (heatmapFilters: HeatmapFilters) => stringheatmapEmpty: (rawHeatmap: HeatmapResponseType | null, rawHeatmapLoading: boolean) => booleanmaxYFromEvents: (heatmapElements: HeatmapElement[]) => number@@ -329,14 +330,12 @@ export interface heatmapDataLogicMeta {isHeightCapped: (maxYFromEvents: number) => booleanfilteredHeatmapElements: (heatmapElements: HeatmapElement[],- windowWidth: number,- windowWidthOverride: number | null,+ analysisWidth: number,heatmapBoundsFilter: HeatmapBoundsFilter | null) => HeatmapElement[]heatmapJsData: (filteredHeatmapElements: HeatmapElement[],- windowWidth: number,- windowWidthOverride: number | null,+ analysisWidth: number,heatmapFixedPositionMode: HeatmapFixedPositionMode) => HeatmapJsData}@@ -568,7 +567,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([},],})),- selectors({+ selectors(({ props }) => ({dateRange: [(s) => [s.commonFilters],(commonFilters: Partial<FilterType>) => {@@ -607,18 +606,26 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([},],- viewportRange: [- (s) => [s.heatmapFilters, s.windowWidth, s.windowWidthOverride],- (heatmapFilters: HeatmapFilters, windowWidth: number, windowWidthOverride: number | null) =>- calculateViewportRange(heatmapFilters, windowWidthOverride ?? windowWidth),+ // The viewport width this heatmap is being analyzed at. Both the rendered preview and the+ // viewport filter sent to the API must read this one value, because a preview at one width+ // over data collected at another places interactions where they never happened. In-app the+ // preview is a fixed-width frame picked with the screen width control, so the query has to+ // match that width and not the width of the analyst's own browser. In the toolbar the+ // overlay sits on the real page, so the live window width is the right anchor.+ analysisWidth: [+ (s) => [s.windowWidthOverride, s.windowWidth],+ (windowWidthOverride: number | null, windowWidth: number): number =>+ windowWidthOverride ?? (props.context === 'in-app' ? DEFAULT_HEATMAP_WIDTH : windowWidth),],- // Derived width with default applied- widthOverride: [- (s) => [s.windowWidthOverride],- (windowWidthOverride: number | null): number => windowWidthOverride ?? DEFAULT_HEATMAP_WIDTH,+ viewportRange: [+ (s) => [s.heatmapFilters, s.analysisWidth],+ (heatmapFilters: HeatmapFilters, analysisWidth: number) =>+ calculateViewportRange(heatmapFilters, analysisWidth),],+ widthOverride: [(s) => [s.analysisWidth], (analysisWidth: number): number => analysisWidth],+heatmapTooltipNoun: [(s) => [s.heatmapFilters],(heatmapFilters: HeatmapFilters) => {@@ -673,21 +680,19 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([// the one place the area bounds filter applies, so rendering (heatmapJsData) and// click-to-view-events hit testing can't disagree about which points existfilteredHeatmapElements: [- (s) => [s.heatmapElements, s.windowWidth, s.windowWidthOverride, s.heatmapBoundsFilter],+ (s) => [s.heatmapElements, s.analysisWidth, s.heatmapBoundsFilter],(heatmapElements: HeatmapElement[],- windowWidth: number,- windowWidthOverride: number | null,+ analysisWidth: number,heatmapBoundsFilter: HeatmapBoundsFilter | null): HeatmapElement[] => {if (!heatmapBoundsFilter) {return heatmapElements}- const width = windowWidthOverride ?? windowWidthreturn heatmapElements.filter((element) =>isWithinBounds({- x: Math.round(element.xPercentage * width),+ x: Math.round(element.xPercentage * analysisWidth),y: Math.round(element.y),targetFixed: element.targetFixed,},@@ -698,21 +703,19 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([],heatmapJsData: [- (s) => [s.filteredHeatmapElements, s.windowWidth, s.windowWidthOverride, s.heatmapFixedPositionMode],+ (s) => [s.filteredHeatmapElements, s.analysisWidth, s.heatmapFixedPositionMode],(filteredHeatmapElements: HeatmapElement[],- windowWidth: number,- windowWidthOverride: number | null,+ analysisWidth: number,heatmapFixedPositionMode: HeatmapFixedPositionMode): HeatmapJsData => {- const width = windowWidthOverride ?? windowWidthconst data = filteredHeatmapElements.reduce((acc, element) => {if (heatmapFixedPositionMode === 'hidden' && element.targetFixed) {return acc}const y = Math.round(element.y)- const x = Math.round(element.xPercentage * width)+ const x = Math.round(element.xPercentage * analysisWidth)acc.push({ x, y, value: element.count })return acc@@ -730,7 +733,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([}},],- }),+ })),listeners(({ actions, values, props }) => ({setCommonFilters: () => {// The open drill-down lists interactions for the filters it was opened with. Close it so itdiff --git a/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx b/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsxindex 99df69c09051..a5065f1b26ed 100644--- a/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx+++ b/products/web_analytics/frontend/heatmaps/components/FilterPanel.tsx@@ -289,7 +289,9 @@ export function FilterPanel({</div>{heatmapEmpty && !rawHeatmapLoading && !previewUnavailable ? (<p className="text-sm text-muted mt-2 mb-0">- No interactions found. Try a different date range or adjust your filters in Heatmap settings.+ {lockedWidth+ ? 'No interactions found at this screen width. Try a different date range, or adjust your filters in Heatmap settings.'+ : 'No interactions found at this screen width. Try another screen width, a different date range, or adjust your filters in Heatmap settings.'}</p>) : null}</div>diff --git a/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.test.ts b/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.test.tsindex 8056cd187435..7ab7672030b4 100644--- a/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.test.ts+++ b/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.test.ts@@ -140,6 +140,29 @@ describe('heatmapsBrowserLogic', () => {await expectLogic(logic).toFinishAllListeners()expect(dataLogic.values.href).toBe('https://example.com/pricing')})++ // The snapshot is a DOM captured at one width, so that width is the only one the overlay can+ // line up with. The viewport filter used to come from the analyst's own window instead, which+ // asked the API for viewports the recorded visitor never had and returned an empty heatmap.+ it('filters on the recording width instead of the analyst window width', async () => {+ const logic = heatmapsBrowserLogic({ iframeRef: { current: null } })+ logic.mount()+ const dataLogic = heatmapDataLogic({ context: 'in-app' })++ logic.actions.setReplayIframeData({+ html: '<html></html>',+ width: 390,+ height: 800,+ startDateTime: undefined,+ url: 'https://example.com/pricing',+ })+ await expectLogic(logic).toFinishAllListeners()++ // 390px at the default 0.9 viewport accuracy, so the preview width and the queried+ // range describe the same viewport+ expect(dataLogic.values.widthOverride).toBe(390)+ expect(dataLogic.values.viewportRange).toEqual({ min: 351, max: 429 })+ })})describe('non-recording stale href', () => {diff --git a/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.ts b/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.tsindex 48081edf7131..6cb7fcd4bf5e 100644--- a/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.ts+++ b/products/web_analytics/frontend/heatmaps/components/heatmapsBrowserLogic.ts@@ -43,6 +43,7 @@ import { HEATMAP_SCREENSHOT_COOKIE_NAME } from '../heatmapScreenshotCookie'import {ReplayIframeData,getStoredRecordingBackground,+ isReplayDimension,isUsableHeatmapUrl,removeReplayIframeDataFromLocalStorage,} from '../replayIframeData'@@ -670,6 +671,13 @@ export const heatmapsBrowserLogic = kea<heatmapsBrowserLogicType>([// Auto-detect match type for replay data URLs tooconst isPattern = isUrlPattern(replayIframeData.url)actions.setHrefMatchType(isPattern ? 'pattern' : 'exact')+ // The snapshot is a DOM captured at one width, and that width is the only one the+ // overlay can line up with. Without this the width falls back to the generic default,+ // so the query asks for viewports the recorded visitor never had and the heatmap+ // comes back empty on a page that clearly has interactions.+ if (isReplayDimension(replayIframeData.width)) {+ actions.setWindowWidthOverride(replayIframeData.width)+ }} else {removeReplayIframeDataFromLocalStorage()}diff --git a/products/web_analytics/frontend/heatmaps/replayIframeData.test.ts b/products/web_analytics/frontend/heatmaps/replayIframeData.test.tsindex 7165c8ee2dbf..00bc64c3498f 100644--- a/products/web_analytics/frontend/heatmaps/replayIframeData.test.ts+++ b/products/web_analytics/frontend/heatmaps/replayIframeData.test.ts@@ -48,6 +48,7 @@ describe('replayIframeData', () => {['malformed json', '{not json'],['a snapshot missing its dimensions', '{"html":"<body>x</body>"}'],['a blank snapshot', '{"html":" ","width":1,"height":2}'],+ ['a snapshot wider than any screen', '{"html":"<body>x</body>","width":100000,"height":2}'],])('reads %s as null', (_name, stored) => {localStorage.setItem('stored', stored)diff --git a/products/web_analytics/frontend/heatmaps/replayIframeData.ts b/products/web_analytics/frontend/heatmaps/replayIframeData.tsindex b1296d6e4e33..c815ea491c18 100644--- a/products/web_analytics/frontend/heatmaps/replayIframeData.ts+++ b/products/web_analytics/frontend/heatmaps/replayIframeData.ts@@ -24,10 +24,18 @@ export function isUsableHeatmapUrl(url: string | undefined | null): url is strin// renderer.export const MAX_REPLAY_IFRAME_HTML_CHARS = 2_000_000+export const MAX_REPLAY_IFRAME_DIMENSION_PX = 8_000++const replayDimensionSchema = z.number().int().positive().max(MAX_REPLAY_IFRAME_DIMENSION_PX)++export function isReplayDimension(value: number): boolean {+ return replayDimensionSchema.safeParse(value).success+}+const replayIframeDataSchema = z.object({html: z.string().refine((html) => !!html.trim()),- width: z.number(),- height: z.number(),+ width: replayDimensionSchema,+ height: replayDimensionSchema,startDateTime: z.union([z.string(), z.undefined()]),url: z.union([z.string(), z.undefined()]),})diff --git a/products/web_analytics/frontend/heatmaps/scenes/heatmap/HeatmapScene.tsx b/products/web_analytics/frontend/heatmaps/scenes/heatmap/HeatmapScene.tsxindex 3d76e4e0631c..d392d805afd6 100644--- a/products/web_analytics/frontend/heatmaps/scenes/heatmap/HeatmapScene.tsx+++ b/products/web_analytics/frontend/heatmaps/scenes/heatmap/HeatmapScene.tsx@@ -47,9 +47,9 @@ export function HeatmapScene({ id }: { id: string }): JSX.Element {screenshotUrl,generatingScreenshot,screenshotLoaded,- containerWidth,desiredNumericWidth,effectiveWidth,+ previewScale,scalePercent,isHeightCapped,lockedWidth,@@ -211,7 +211,7 @@ export function HeatmapScene({ id }: { id: string }): JSX.Element {<span className="min-w-0 flex-1 truncate" title={displayUrl ?? undefined}>{displayUrl}</span>- {typeof widthOverride === 'number' && containerWidth && widthOverride > containerWidth ? (+ {previewScale < 1 ? (<Tooltiptitle={`Scaled from ${widthOverride}px to ${Math.round(effectiveWidth as number)}px to fit the preview`}>@@ -295,22 +295,22 @@ export function HeatmapScene({ id }: { id: string }): JSX.Element {</div>) : (<div- className="relative"+ className="relative overflow-hidden"// eslint-disable-next-line react/forbid-dom-props- style={{ height: heightOverride }}+ style={{ height: heightOverride * previewScale }}>- <HeatmapCanvas- positioning="absolute"- widthOverride={desiredNumericWidth ?? undefined}- context="in-app"- /><iframekey={previewVersion}id="heatmap-iframe"title="Heatmap browser"className="bg-white rounded-b-lg"// eslint-disable-next-line react/forbid-dom-props- style={{ width: '100%', height: heightOverride }}+ style={{+ width: widthOverride,+ height: heightOverride,+ transform: `scale(${previewScale})`,+ transformOrigin: 'top left',+ }}src={displayUrl || ''}onLoad={onIframeLoad}// these two sandbox values are necessary so that the site and toolbar can run@@ -320,6 +320,11 @@ export function HeatmapScene({ id }: { id: string }): JSX.Element {// we don't allow things such as camera access thoughallow=""/>+ <HeatmapCanvas+ positioning="absolute"+ widthOverride={desiredNumericWidth ?? undefined}+ context="in-app"+ /></div>)}</div>diff --git a/products/web_analytics/frontend/heatmaps/scenes/heatmap/heatmapLogic.ts b/products/web_analytics/frontend/heatmaps/scenes/heatmap/heatmapLogic.tsindex 14b373d44d6b..eccc45c7e769 100644--- a/products/web_analytics/frontend/heatmaps/scenes/heatmap/heatmapLogic.ts+++ b/products/web_analytics/frontend/heatmaps/scenes/heatmap/heatmapLogic.ts@@ -85,6 +85,8 @@ function getCreationFailureCategory(error: unknown): 'validation' | 'permission'return 'unknown'}+const PREVIEW_SCALE_SLACK_PX = 8+function isValidPageUrl(url: string | null): boolean {if (!url) {return true@@ -162,6 +164,7 @@ export interface heatmapLogicValues {pageUrlDraft: stringpageUrlDraftIsPattern: booleanpreviewError: string | JSX.Element | null+ previewScale: numberpreviewType: HeatmapTypepreviewUnavailable: booleanpreviewVersion: number@@ -365,7 +368,8 @@ export interface heatmapLogicMeta {pageUrlDraftIsPattern: (pageUrlDraft: string) => booleandesiredNumericWidth: (widthOverride: number, containerWidth: number | null) => numbereffectiveWidth: (desiredNumericWidth: number) => number- scalePercent: (widthOverride: number, containerWidth: number | null) => number+ previewScale: (widthOverride: number, containerWidth: number | null) => number+ scalePercent: (previewScale: number) => number}}@@ -915,13 +919,14 @@ export const heatmapLogic = kea<heatmapLogicType>([},],effectiveWidth: [(s) => [s.desiredNumericWidth], (desiredNumericWidth: number) => desiredNumericWidth],- scalePercent: [+ previewScale: [(s) => [s.widthOverride, s.containerWidth],- (widthOverride: number, containerWidth: number | null) => {- const scale = containerWidth ? Math.min(1, containerWidth / widthOverride) : 1- return Math.round(scale * 100)- },+ (widthOverride: number, containerWidth: number | null) =>+ containerWidth && widthOverride > 0 && containerWidth < widthOverride - PREVIEW_SCALE_SLACK_PX+ ? containerWidth / widthOverride+ : 1,],+ scalePercent: [(s) => [s.previewScale], (previewScale: number) => Math.round(previewScale * 100)],}),selectors(({ props }) => ({[SIDE_PANEL_CONTEXT_KEY]: [