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.

fix(web-analytics): Clear drilled-in filters on back navigation (#80169)

Jordan Mryyand3ab33c8dc4e
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index 9f0aa9f1d0de..28239e24f472 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -525,6 +525,7 @@ export const FEATURE_FLAGS = {
VISUAL_REVIEW: 'visual-review', // owner: #team-devex
WAREHOUSE_PERSON_PROPERTIES: 'warehouse-person-properties', // owner: @tomowers #team-warehouse-sources, gates warehouse -> person properties (person-target custom property sources)
WEB_ANALYTICS_ACHIEVEMENTS: 'web-analytics-achievements', // owner: @jordanm-posthog #team-web-analytics
+ WEB_ANALYTICS_BACK_NAVIGATION_RESET: 'web-analytics-back-navigation-reset', // owner: @jordanm-posthog #team-web-analytics
WEB_ANALYTICS_BOT_ANALYSIS: 'web-analytics-bot-analysis', // owner: @lricoy #team-web-analytics
WEB_ANALYTICS_CONVERSION_GOAL_PREAGG: 'web-analytics-conversion-goal-preagg', // owner: @lricoy #team-web-analytics
WEB_ANALYTICS_DRAG_TO_ZOOM: 'web-analytics-drag-to-zoom', // owner: @jordanm-posthog #team-web-analytics
diff --git a/frontend/src/scenes/web-analytics/webAnalyticsLogic.test.ts b/frontend/src/scenes/web-analytics/webAnalyticsLogic.test.ts
index 164d15abd470..f73597af9bfe 100644
--- a/frontend/src/scenes/web-analytics/webAnalyticsLogic.test.ts
+++ b/frontend/src/scenes/web-analytics/webAnalyticsLogic.test.ts
@@ -10,7 +10,7 @@ import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { userLogic } from 'scenes/userLogic'
import { initKeaTests } from '~/test/init'
-import { UserType } from '~/types'
+import { PropertyFilterType, PropertyOperator, UserType } from '~/types'
import { GraphsTab, ProductTab, TileId } from './common'
import { FOCUS_MODE_TILE_IDS } from './focus-mode/focusModeMapping'
@@ -441,4 +441,80 @@ describe('webAnalyticsLogic URL restoration', () => {
expect(router.values.searchParams[key]).toBe(expected)
})
+
+ const enableBackNavReset = (): void => {
+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.WEB_ANALYTICS_BACK_NAVIGATION_RESET], {
+ [FEATURE_FLAGS.WEB_ANALYTICS_BACK_NAVIGATION_RESET]: true,
+ })
+ }
+
+ const FILTER_A = {
+ type: PropertyFilterType.Session as const,
+ key: '$entry_utm_source',
+ operator: PropertyOperator.Exact,
+ value: ['google'],
+ }
+ const FILTER_B = {
+ type: PropertyFilterType.Session as const,
+ key: '$entry_utm_medium',
+ operator: PropertyOperator.Exact,
+ value: ['cpc'],
+ }
+
+ it('clears a drilled-in filter when navigating back to a URL without filters (flag on)', async () => {
+ enableBackNavReset()
+
+ router.actions.push('/web', { filters: [FILTER_A] })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+
+ router.actions.push('/web')
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([])
+ })
+
+ it('keeps the outer filter when stepping back between two drill levels (flag on)', async () => {
+ enableBackNavReset()
+
+ router.actions.push('/web', { filters: [FILTER_A, FILTER_B] })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A, FILTER_B])
+
+ router.actions.push('/web', { filters: [FILTER_A] })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+ })
+
+ it('leaves a drilled-in filter in place on back-navigation when the flag is off', async () => {
+ router.actions.push('/web', { filters: [FILTER_A] })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+
+ router.actions.push('/web')
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+ })
+
+ it('keeps filters when moving to the Live tab, which never serializes them (flag on)', async () => {
+ enableBackNavReset()
+
+ router.actions.push('/web', { filters: [FILTER_A] })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+
+ router.actions.push('/web/live')
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+ })
+
+ it('keeps filters when a cold /web/bots load redirects to /web with the bots flag off (reset flag on)', async () => {
+ enableBackNavReset()
+ logic.actions.setWebAnalyticsFilters([FILTER_A])
+ await expectLogic(logic).toFinishAllListeners()
+ logic.cache.hasRestoredWebUrl = false
+
+ router.actions.push('/web/bots')
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.rawWebAnalyticsFilters).toEqual([FILTER_A])
+ })
})
diff --git a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
index 663e2fa7bfec..b3d0e63b386e 100644
--- a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
+++ b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
@@ -104,6 +104,7 @@ import {
INITIAL_DATE_FROM,
INITIAL_DATE_TO,
INITIAL_INTERVAL,
+ INITIAL_WEB_ANALYTICS_FILTER,
PathTab,
ProductTab,
SourceTab,
@@ -3237,7 +3238,8 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
device_type,
tile_visualizations,
include_host_path,
- }: Record<string, any>
+ }: Record<string, any>,
+ isInitialRestore: boolean
): void => {
if (
![
@@ -3261,6 +3263,8 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
return
}
+ cache.hasRestoredWebUrl = true
+
// Stamp the last-used timestamp for feature flag targeting (throttled to once per day per browser).
const stampKey = `ph_last_web_analytics_stamp_${posthog.get_distinct_id()}`
const oneDayMs = 24 * 60 * 60 * 1000
@@ -3270,17 +3274,27 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
localStorage.setItem(stampKey, Date.now().toString())
}
- const parsedFilters = filters ? (isWebAnalyticsPropertyFilters(filters) ? filters : []) : undefined
- if (parsedFilters) {
+ const applyRestoredFilters = (nextFilters: WebAnalyticsPropertyFilters): void => {
if (productTab === ProductTab.BOT_ANALYTICS) {
const botLogic = botAnalyticsLogic.findMounted()
- if (botLogic && !objectsEqual(parsedFilters, botLogic.values.rawBotAnalyticsFilters)) {
- botLogic.actions.setBotAnalyticsFilters(parsedFilters)
+ if (botLogic && !objectsEqual(nextFilters, botLogic.values.rawBotAnalyticsFilters)) {
+ botLogic.actions.setBotAnalyticsFilters(nextFilters)
}
- } else if (!objectsEqual(parsedFilters, values.rawWebAnalyticsFilters)) {
- actions.setWebAnalyticsFilters(parsedFilters)
+ } else if (!objectsEqual(nextFilters, values.rawWebAnalyticsFilters)) {
+ actions.setWebAnalyticsFilters(nextFilters)
}
}
+
+ const tabSerializesFilters = productTab !== ProductTab.LIVE && productTab !== ProductTab.HEALTH
+ const shouldResetAbsentFilters =
+ !isInitialRestore && !!values.featureFlags[FEATURE_FLAGS.WEB_ANALYTICS_BACK_NAVIGATION_RESET]
+
+ const parsedFilters = filters ? (isWebAnalyticsPropertyFilters(filters) ? filters : []) : undefined
+ if (parsedFilters) {
+ applyRestoredFilters(parsedFilters)
+ } else if (shouldResetAbsentFilters && tabSerializesFilters) {
+ applyRestoredFilters(INITIAL_WEB_ANALYTICS_FILTER)
+ }
if (
conversionGoalActionId &&
conversionGoalActionId !== (values.conversionGoal as ActionConversionGoal)?.actionId
@@ -3396,9 +3410,10 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
// into a burst of redundant URL evaluations and trip the rapid-URL-change detector. The depth
// counter keeps the guard correct if a restore re-enters (e.g. the bots-flag redirect).
const toAction = (params: { productTab?: ProductTab }, searchParams: Record<string, any>): void => {
+ const isInitialRestore = !cache.hasRestoredWebUrl
cache.applyUrlStateDepth = (cache.applyUrlStateDepth ?? 0) + 1
try {
- applyUrlState(params, searchParams)
+ applyUrlState(params, searchParams, isInitialRestore)
} finally {
cache.applyUrlStateDepth -= 1
}