: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): scope in-app heatmap data requests to the page's team (#63004)
Xandera16c138a3a75
diff --git a/frontend/src/lib/components/heatmaps/heatmapDataLogic.test.ts b/frontend/src/lib/components/heatmaps/heatmapDataLogic.test.tsnew file mode 100644index 000000000000..16cd6b25331d--- /dev/null+++ b/frontend/src/lib/components/heatmaps/heatmapDataLogic.test.ts@@ -0,0 +1,33 @@+import { heatmapApiPath } from 'lib/components/heatmaps/heatmapDataLogic'++import { AppContext } from '~/types'++describe('heatmapApiPath', () => {+ let priorAppContext: AppContext | undefined++ beforeEach(() => {+ priorAppContext = window.POSTHOG_APP_CONTEXT+ })++ afterEach(() => {+ window.POSTHOG_APP_CONTEXT = priorAppContext+ })++ it.each([+ // in-app requests must pin the team the page was loaded for, not the user's global current project+ ['in-app', 42, '', '/api/projects/42/heatmaps/'],+ ['in-app', 42, 'events/', '/api/projects/42/heatmaps/events/'],+ // the toolbar has no app context and keeps the legacy unscoped route+ ['toolbar', 42, '', '/api/heatmap/'],+ ['toolbar', 42, 'events/', '/api/heatmap/events/'],+ // without an app context team there is nothing to scope to, so fall back to the legacy route+ ['in-app', null, '', '/api/heatmap/'],+ ['in-app', null, 'events/', '/api/heatmap/events/'],+ ] as const)('context %s with team %s and endpoint %s resolves %s', (context, teamId, endpoint, expected) => {+ window.POSTHOG_APP_CONTEXT = (teamId === null+ ? undefined+ : { current_team: { id: teamId } }) as unknown as AppContext++ expect(heatmapApiPath(context, endpoint)).toBe(expected)+ })+})diff --git a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts b/frontend/src/lib/components/heatmaps/heatmapDataLogic.tsindex 1b4f7b90d760..bded77b54083 100644--- a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts+++ b/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts@@ -22,6 +22,7 @@ import {import { LemonSelectOption } from 'lib/lemon-ui/LemonSelect'import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'import { dateFilterToText } from 'lib/utils'+import { getAppContext } from 'lib/utils/getAppContext'import { toolbarConfigLogic, toolbarFetch } from '~/toolbar/toolbarConfigLogic'import { HeatmapElement, HeatmapResponseType } from '~/toolbar/types'@@ -61,6 +62,19 @@ export interface HeatmapDataLogicProps {exportToken?: string | null}+export function heatmapApiPath(context: HeatmapDataLogicProps['context'], endpoint: '' | 'events/'): string {+ if (context === 'in-app') {+ // The unscoped /api/heatmap/ route resolves the team from the user's *global* current+ // project, which any other tab can change, so pin the team this page was loaded for+ // instead. The app context team is also set on export renders (team_for_public_context).+ const teamId = getAppContext()?.current_team?.id+ if (teamId != null) {+ return `/api/projects/${teamId}/heatmaps/${endpoint}`+ }+ }+ return `/api/heatmap/${endpoint}`+}+export type HrefMatchType = 'exact' | 'pattern'export const heatmapDataLogic = kea<heatmapDataLogicType>([@@ -197,7 +211,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([const { type, aggregation } = values.heatmapFilters// toolbar fetch collapses queryparams but this URL has multiple with the same name- const apiURL = `/api/heatmap/${encodeParams(+ const apiURL = `${heatmapApiPath(props.context, '')}${encodeParams({type,date_from,@@ -249,7 +263,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([const { date_from, date_to, filter_test_accounts, cohort_ids } = values.commonFiltersconst { type } = values.heatmapFilters- const apiURL = `/api/heatmap/events/${encodeParams(+ const apiURL = `${heatmapApiPath(props.context, 'events/')}${encodeParams({type,date_from,@@ -440,7 +454,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([const { type } = values.heatmapFiltersconst nextOffset = currentEvents.results.length- const apiURL = `/api/heatmap/events/${encodeParams(+ const apiURL = `${heatmapApiPath(props.context, 'events/')}${encodeParams({type,date_from,diff --git a/frontend/src/scenes/heatmaps/scenes/heatmap/HeatmapScene.stories.tsx b/frontend/src/scenes/heatmaps/scenes/heatmap/HeatmapScene.stories.tsxindex 4e22b76232ce..a8fc4405c55e 100644--- a/frontend/src/scenes/heatmaps/scenes/heatmap/HeatmapScene.stories.tsx+++ b/frontend/src/scenes/heatmaps/scenes/heatmap/HeatmapScene.stories.tsx@@ -88,7 +88,7 @@ export const IframeExample: Story = {get: {'/api/environments/:team_id/saved/hm_iframe/': (_req, res, ctx) =>res(ctx.status(200), ctx.json(makeIframeSaved())),- '/api/heatmap/': (_req, res, ctx) =>+ '/api/projects/:team_id/heatmaps/': (_req, res, ctx) =>res(ctx.status(200),ctx.json({