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(insights): keep a shared link's date range and interval on load (#77406)

Sam Pennington9054366b3dc3
diff --git a/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx b/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx
index c91ee5f8b907..db721e25b4b6 100644
--- a/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx
+++ b/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx
@@ -413,7 +413,11 @@ export interface insightNavLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightNavLogicActions {
- setQuery: (query: Node<Record<string, any>> | null) => {
+ setQuery: (
+ query: Node<Record<string, any>> | null,
+ fromUrl?: boolean | undefined
+ ) => {
+ fromUrl: boolean
query: Node<Record<string, any>> | null
} // insightDataLogic
setActiveView: (view: InsightType) => {
diff --git a/frontend/src/scenes/insights/insightDataLogic.test.ts b/frontend/src/scenes/insights/insightDataLogic.test.ts
index 14756715fc12..853c7cea1a3e 100644
--- a/frontend/src/scenes/insights/insightDataLogic.test.ts
+++ b/frontend/src/scenes/insights/insightDataLogic.test.ts
@@ -293,6 +293,47 @@ describe('insightDataLogic', () => {
})
})
+ describe('a query carried in the URL', () => {
+ it('is not overwritten when the saved insight loads', async () => {
+ const savedQuery = setLatestVersionsOnQuery({
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [],
+ dateRange: { date_from: '-7d' },
+ interval: 'day',
+ },
+ }) as InsightVizNode
+ const sharedQuery = setLatestVersionsOnQuery({
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [],
+ dateRange: { date_from: '-30d' },
+ interval: 'week',
+ },
+ }) as InsightVizNode
+
+ useMocks({
+ get: {
+ '/api/environments/:team_id/insights/': {
+ results: [{ id: 1, short_id: Insight123, query: savedQuery }],
+ },
+ },
+ })
+
+ theInsightDataLogic.actions.setQuery(sharedQuery, true)
+
+ await expectLogic(theInsightDataLogic, () => {
+ theInsightLogic.actions.loadInsight(Insight123)
+ })
+ .toDispatchActions(theInsightLogic, ['loadInsightSuccess'])
+ .toFinishAllListeners()
+ .toNotHaveDispatchedActions(['syncQueryFromProps'])
+ .toMatchValues({ query: sharedQuery })
+ })
+ })
+
describe('reacts when the insight changes', () => {
const q = examples.InsightTrends
diff --git a/frontend/src/scenes/insights/insightDataLogic.tsx b/frontend/src/scenes/insights/insightDataLogic.tsx
index 155ab1508906..94bdca204c86 100644
--- a/frontend/src/scenes/insights/insightDataLogic.tsx
+++ b/frontend/src/scenes/insights/insightDataLogic.tsx
@@ -136,6 +136,7 @@ export interface insightDataLogicValues {
propsQuery: QuerySchema | null | undefined
query: Node | null
queryChanged: boolean
+ queryFromUrl: boolean
showDebugPanel: boolean
showQueryEditor: boolean
}
@@ -380,7 +381,11 @@ export interface insightDataLogicActions {
persistDisplayOptions: (query: Node) => {
query: Node<Record<string, any>>
}
- setQuery: (query: Node | null) => {
+ setQuery: (
+ query: Node | null,
+ fromUrl?: boolean
+ ) => {
+ fromUrl: boolean
query: Node<Record<string, any>> | null
}
syncQueryFromProps: (query: Node | null) => {
@@ -488,7 +493,7 @@ export const insightDataLogic = kea<insightDataLogicType>([
})),
actions({
- setQuery: (query: Node | null) => ({ query }),
+ setQuery: (query: Node | null, fromUrl: boolean = false) => ({ query, fromUrl }),
syncQueryFromProps: (query: Node | null) => ({ query }),
toggleQueryEditorPanel: true,
toggleDebugPanel: true,
@@ -504,6 +509,13 @@ export const insightDataLogic = kea<insightDataLogicType>([
syncQueryFromProps: (_, { query }) => query,
},
],
+ queryFromUrl: [
+ false,
+ {
+ setQuery: (_, { fromUrl }) => fromUrl,
+ syncQueryFromProps: () => false,
+ },
+ ],
showQueryEditor: [
false,
{
@@ -770,6 +782,12 @@ export const insightDataLogic = kea<insightDataLogicType>([
}
},
loadInsightSuccess: ({ insight }) => {
+ // A shared link's query (`#q=`) is applied before the saved insight arrives, so re-syncing
+ // here would silently discard the date range and interval the sender chose.
+ if (values.queryFromUrl) {
+ return
+ }
+
// `internalQuery` wins over `insight.query` in the `query` selector, and the SQL editor
// updates a different logic instance — so a reload alone leaves this scene on the stale
// query until a hard refresh. Re-sync the override to the freshly loaded query.
diff --git a/frontend/src/scenes/insights/insightSceneLogic.test.ts b/frontend/src/scenes/insights/insightSceneLogic.test.ts
index 3da4ff173ada..782d9f2ae18e 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.test.ts
+++ b/frontend/src/scenes/insights/insightSceneLogic.test.ts
@@ -12,6 +12,7 @@ import { urls } from 'scenes/urls'
import { useMocks } from '~/mocks/jest'
import { examples } from '~/queries/examples'
import { InsightVizNode, NodeKind, ProductKey } from '~/queries/schema/schema-general'
+import { setLatestVersionsOnQuery } from '~/queries/utils'
import { initKeaTests } from '~/test/init'
import { ActivityScope, InsightShortId, InsightType, ItemMode } from '~/types'
@@ -216,6 +217,49 @@ describe('insightSceneLogic', () => {
expect(query.source?.tags?.productKey).toEqual(ProductKey.PRODUCT_ANALYTICS)
})
+ it("applies a shared link's #q= query to a saved insight on in-app navigation", async () => {
+ // Opening a shared link inside the app is a router PUSH, not a cold load, so it misses the
+ // upgradeQuery path.
+ const savedQuery = setLatestVersionsOnQuery({
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [],
+ dateRange: { date_from: '-7d' },
+ interval: 'day',
+ },
+ }) as InsightVizNode
+ const sharedQuery = setLatestVersionsOnQuery({
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [],
+ dateRange: { date_from: '-30d' },
+ interval: 'week',
+ },
+ }) as InsightVizNode
+
+ useMocks({
+ get: {
+ '/api/environments/:team_id/insights/': {
+ results: [{ id: 42, short_id: Insight42, result: ['result from api'], query: savedQuery }],
+ },
+ },
+ })
+
+ router.actions.push(urls.insightView(Insight42))
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ router.actions.push(combineUrl(urls.insightView(Insight42), {}, { q: JSON.stringify(sharedQuery) }).url)
+ await expectLogic(logic).toFinishAllListeners()
+
+ const dataLogic = logic.values.insightDataLogicRef
+ expect(dataLogic).not.toBeNull()
+ expect(dataLogic!.logic.values.query).toEqual(sharedQuery)
+ })
+
it('does not overwrite an existing productKey on a DataTableNode drill-down query', async () => {
// A drill-down query that already declares its product (e.g. web analytics) must keep it.
const dataTableQuery = {
diff --git a/frontend/src/scenes/insights/insightSceneLogic.tsx b/frontend/src/scenes/insights/insightSceneLogic.tsx
index 41567a89c9d9..35625182bd72 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.tsx
+++ b/frontend/src/scenes/insights/insightSceneLogic.tsx
@@ -717,6 +717,13 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
}
},
upgradeQuery: async ({ query }) => {
+ // Capture the target insight before the await — a navigation while the upgrade request
+ // is in flight remounts insightLogicRef/insightDataLogicRef for the new insight, and
+ // applying the old URL's query to them would leak it onto the wrong insight.
+ const insightIdAtStart = values.insightId
+ const insightLogicRefAtStart = values.insightLogicRef
+ const insightDataLogicRefAtStart = values.insightDataLogicRef
+
let upgradedQuery: Node | null = null
if (!checkLatestVersionsOnQuery(query)) {
@@ -728,6 +735,14 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
upgradedQuery = convertDataTableNodeToDataVisualizationNode(upgradedQuery)
+ if (
+ values.insightId !== insightIdAtStart ||
+ values.insightLogicRef !== insightLogicRefAtStart ||
+ values.insightDataLogicRef !== insightDataLogicRefAtStart
+ ) {
+ return
+ }
+
if (values.insightId === 'new' || values.insightId?.startsWith('new-')) {
values.insightLogicRef?.logic.actions.setInsight(
{
@@ -741,7 +756,7 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
}
)
} else {
- values.insightDataLogicRef?.logic.actions.setQuery(upgradedQuery)
+ values.insightDataLogicRef?.logic.actions.setQuery(upgradedQuery, true)
}
},
})),
@@ -887,6 +902,15 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
}
eventUsageLogic.actions.reportInsightStarted(query)
+ } else {
+ // queryFromUrl can also come from the insightType hash param (above), so only
+ // treat it as a shared link's query when q itself is present.
+ const sharedQueryFromUrl = q ? queryFromUrl : null
+ if (sharedQueryFromUrl) {
+ // In-app navigation to a shared link — the saved insight loads fresh, so the
+ // query the link carries has to be applied on top of it.
+ actions.upgradeQuery(sharedQueryFromUrl)
+ }
}
}
},
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.ts b/frontend/src/scenes/insights/insightVizDataLogic.ts
index 19ddb6aede69..b76c18a31860 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.ts
@@ -373,7 +373,11 @@ export interface insightVizDataLogicActions {
| TraceSpansAttributeBreakdownQueryResponse
| TraceSpansQueryResponse
| TraceSpansTreeQueryResponse // insightDataLogic
- setQuery: (query: Node<Record<string, any>> | null) => {
+ setQuery: (
+ query: Node<Record<string, any>> | null,
+ fromUrl?: boolean | undefined
+ ) => {
+ fromUrl: boolean
query: Node<Record<string, any>> | null
} // insightDataLogic
removeFormulaNode: (formulas: TrendsFormulaNode[]) => {
@@ -1123,7 +1127,7 @@ export interface insightVizDataLogicMeta {
actions: import('~/types').ActionType[]
) => string[]
theme: (
- getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null,
+ getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null, // dataThemeLogic
querySource:
| FunnelsQuery
| LifecycleQuery
diff --git a/products/alerts/frontend/logic/insightAlertsLogic.ts b/products/alerts/frontend/logic/insightAlertsLogic.ts
index 9779e3be762a..218f590722c6 100644
--- a/products/alerts/frontend/logic/insightAlertsLogic.ts
+++ b/products/alerts/frontend/logic/insightAlertsLogic.ts
@@ -146,7 +146,11 @@ export interface insightAlertsLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightAlertsLogicActions {
- setQuery: (query: Node<Record<string, any>> | null) => {
+ setQuery: (
+ query: Node<Record<string, any>> | null,
+ fromUrl?: boolean | undefined
+ ) => {
+ fromUrl: boolean
query: Node<Record<string, any>> | null
} // insightVizDataLogic
clearSimulationAnomalyPoints: () => {
diff --git a/products/customer_analytics/frontend/components/CustomerJourneys/journeyBuilderLogic.ts b/products/customer_analytics/frontend/components/CustomerJourneys/journeyBuilderLogic.ts
index 99610f84d1ca..fc134562c25b 100644
--- a/products/customer_analytics/frontend/components/CustomerJourneys/journeyBuilderLogic.ts
+++ b/products/customer_analytics/frontend/components/CustomerJourneys/journeyBuilderLogic.ts
@@ -259,7 +259,11 @@ export interface journeyBuilderLogicActions {
collapsePath: () => {
value: true
} // funnelPathsExpansionLogic
- setInsightQuery: (query: Node<Record<string, any>> | null) => {
+ setInsightQuery: (
+ query: Node<Record<string, any>> | null,
+ fromUrl?: boolean | undefined
+ ) => {
+ fromUrl: boolean
query: Node<Record<string, any>> | null
} // insightDataLogic
addStep: (insertAtIndex: number) => {