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(marketing-analytics): drop unknown keys from the stored integration filter (#97541)

Javier Bahamondes6951525ae9b8
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.test.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.test.ts
new file mode 100644
index 000000000000..1fa88801b490
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.test.ts
@@ -0,0 +1,41 @@
+import { MOCK_TEAM_ID } from 'lib/api.mock'
+
+import { expectLogic } from 'kea-test-utils'
+
+import { initKeaTests } from '~/test/init'
+
+import { marketingAnalyticsLogic } from './marketingAnalyticsLogic'
+
+// Kea builds this from the reducer's path and name. It is pinned in the logic, so a rename cannot
+// silently point the reducer at a different key and abandon what someone already saved.
+const STORAGE_KEY = `${MOCK_TEAM_ID}__.scenes.webAnalytics.marketingAnalyticsLogic.integrationFilter`
+
+describe('marketingAnalyticsLogic', () => {
+ let logic: ReturnType<typeof marketingAnalyticsLogic.build>
+
+ beforeEach(() => {
+ localStorage.clear()
+ initKeaTests()
+ })
+
+ afterEach(() => {
+ if (logic?.cache.mounted) {
+ logic.unmount()
+ }
+ localStorage.clear()
+ })
+
+ it('keeps the selection and drops an unknown key from a filter saved by an older build', async () => {
+ localStorage.setItem(
+ STORAGE_KEY,
+ JSON.stringify({ integrationSourceIds: ['source-1'], includeNonIntegrated: true })
+ )
+
+ logic = marketingAnalyticsLogic()
+ logic.mount()
+
+ await expectLogic(logic).toMatchValues({
+ integrationFilter: { integrationSourceIds: ['source-1'] },
+ })
+ })
+})
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.ts
index 3c0daeaed76b..26a601a8b1c6 100644
--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.ts
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsLogic.ts
@@ -48,6 +48,7 @@ import {
NEEDED_FIELDS_FOR_NATIVE_MARKETING_ANALYTICS,
findSchemaByFieldName,
generateUniqueName,
+ sanitizeIntegrationFilter,
validColumnsForTiles,
} from './utils'
@@ -248,6 +249,7 @@ export interface marketingAnalyticsLogicValues {
dataWarehouseTables: DatabaseSchemaDataWarehouseTable[] // sourceManagementLogic
baseCurrency: CurrencyCode // teamLogic
_drillDownLevel: MarketingAnalyticsDrillDownLevel
+ _integrationFilter: IntegrationFilter
activeTab: MarketingAnalyticsTab
allAvailableSources: {
id: string
@@ -483,6 +485,7 @@ export interface marketingAnalyticsLogicActions {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface marketingAnalyticsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
+ integrationFilter: (_integrationFilter: IntegrationFilter) => IntegrationFilter
drillDownLevel: (
_drillDownLevel: MarketingAnalyticsDrillDownLevel,
featureFlags: FeatureFlagsSet
@@ -726,9 +729,12 @@ export const marketingAnalyticsLogic = kea<marketingAnalyticsLogicType>([
},
],
optionsOpen: [false as boolean, { setOptionsOpen: (_, { optionsOpen }) => optionsOpen }],
- integrationFilter: [
+ _integrationFilter: [
{ integrationSourceIds: [] } as IntegrationFilter,
- persistConfig,
+ // pinned: localStorage key. Kea derives it from the reducer name, so the rename above
+ // would otherwise point it at a fresh key. That resets a selection someone already made,
+ // and strands the unreadable value under the old key for a rollback to find again.
+ { ...persistConfig, storageKey: 'scenes.webAnalytics.marketingAnalyticsLogic.integrationFilter' },
{
setIntegrationFilter: (_, { integrationFilter }) => integrationFilter,
syncFromUrl: (state, { params }) => {
@@ -849,6 +855,10 @@ export const marketingAnalyticsLogic = kea<marketingAnalyticsLogicType>([
}
}),
selectors({
+ integrationFilter: [
+ (s) => [s._integrationFilter],
+ (stored: IntegrationFilter): IntegrationFilter => sanitizeIntegrationFilter(stored),
+ ],
drillDownLevel: [
(s) => [s._drillDownLevel, s.featureFlags],
(level: MarketingAnalyticsDrillDownLevel, featureFlags: Record<string, boolean | string>) => {
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.test.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.test.ts
index 391790c680c4..1e952ed043b8 100644
--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.test.ts
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.test.ts
@@ -20,6 +20,7 @@ import {
getSortedColumnsByArray,
orderArrayByPreference,
rowMatchesSearch,
+ sanitizeIntegrationFilter,
validColumnsForTiles,
} from './utils'
@@ -476,4 +477,23 @@ describe('marketing analytics utils', () => {
expect(goalSumsAProperty(goalWithMath(math))).toBe(expected)
})
})
+
+ describe('sanitizeIntegrationFilter', () => {
+ it('drops a key the query schema no longer accepts', () => {
+ const stored = { integrationSourceIds: ['abc'], includeNonIntegrated: true }
+ expect(sanitizeIntegrationFilter(stored)).toEqual({ integrationSourceIds: ['abc'] })
+ })
+
+ it.each([
+ ['selected ids survive', { integrationSourceIds: ['a', 'b'] }, ['a', 'b']],
+ ['empty selection', { integrationSourceIds: [] }, []],
+ ['missing field', {}, []],
+ ['null', null, []],
+ ['undefined', undefined, []],
+ ['non-array ids', { integrationSourceIds: 'a' }, []],
+ ['non-string entries', { integrationSourceIds: ['a', 3, null] }, ['a']],
+ ])('%s', (_name, stored, expected) => {
+ expect(sanitizeIntegrationFilter(stored)).toEqual({ integrationSourceIds: expected })
+ })
+ })
})
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.ts
index 520efe8fa2f5..4a5e633fe24f 100644
--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.ts
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils.ts
@@ -5,6 +5,7 @@ import {
ConversionGoalFilter,
DataWarehouseNode,
ExternalDataSourceType,
+ IntegrationFilter,
MARKETING_INTEGRATION_CONFIGS,
MarketingAnalyticsColumnsSchemaNames,
MarketingAnalyticsConstants,
@@ -827,3 +828,10 @@ export function rowMatchesSearch(record: unknown, searchTerm: string): boolean {
return false
})
}
+
+/** The stored filter is whatever an older build of this page wrote, so keep only the field we still read.
+ * A key the query schema no longer accepts makes the backend reject every request the dashboard sends. */
+export function sanitizeIntegrationFilter(stored: unknown): IntegrationFilter {
+ const ids = (stored as IntegrationFilter | null | undefined)?.integrationSourceIds
+ return { integrationSourceIds: Array.isArray(ids) ? ids.filter((id) => typeof id === 'string') : [] }
+}