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 persons-modal drill-down query on open as new insight (#63836)

Steven Shultsc511da341e6b
diff --git a/frontend/src/scenes/insights/insightSceneLogic.test.ts b/frontend/src/scenes/insights/insightSceneLogic.test.ts
index 6bce6337140e..58968b5a97ff 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.test.ts
+++ b/frontend/src/scenes/insights/insightSceneLogic.test.ts
@@ -141,6 +141,92 @@ describe('insightSceneLogic', () => {
})
})
+ it('tags a DataTableNode drill-down query on cold load via the upgrade path', async () => {
+ // The persons-modal "View events" button opens in a new tab, so the scene cold-loads with the
+ // query already in the URL (initial navigation -> upgradeQuery). A DataTableNode's source query
+ // is executed directly, so without a productKey tag ClickHouse rejects it as untagged — the
+ // upgrade path must tag the source when materializing the new insight.
+ const dataTableQuery = {
+ kind: NodeKind.DataTableNode,
+ source: {
+ kind: NodeKind.ActorsQuery,
+ select: ['person'],
+ },
+ }
+ router.actions.push(urls.insightNew({ query: dataTableQuery as any }))
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['upgradeQuery']).toFinishAllListeners()
+
+ const query = logic.values.insightLogicRef?.logic.values.insight.query as any
+ expect(query.kind).toEqual(NodeKind.DataTableNode)
+ expect(query.source?.tags?.productKey).toEqual(ProductKey.PRODUCT_ANALYTICS)
+ })
+
+ it('tags a DataTableNode drill-down query on in-app navigation', async () => {
+ // The "Open as new insight" button navigates in-app (router PUSH, not an initial load), which
+ // routes through urlToAction's PUSH branch rather than upgradeQuery. That path must tag too.
+ const dataTableQuery = {
+ kind: NodeKind.DataTableNode,
+ source: {
+ kind: NodeKind.ActorsQuery,
+ select: ['person'],
+ },
+ }
+
+ // Settle a new-insight scene first, then navigate in-app to the drill-down query
+ router.actions.push(urls.insightNew())
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ router.actions.push(urls.insightNew({ query: dataTableQuery as any }))
+ await expectLogic(logic).toFinishAllListeners()
+
+ const query = logic.values.insightLogicRef?.logic.values.insight.query as any
+ expect(query.kind).toEqual(NodeKind.DataTableNode)
+ expect(query.source?.tags?.productKey).toEqual(ProductKey.PRODUCT_ANALYTICS)
+ })
+
+ 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 = {
+ kind: NodeKind.DataTableNode,
+ source: {
+ kind: NodeKind.ActorsQuery,
+ select: ['person'],
+ tags: { productKey: ProductKey.WEB_ANALYTICS },
+ },
+ }
+ router.actions.push(urls.insightNew({ query: dataTableQuery as any }))
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ const query = logic.values.insightLogicRef?.logic.values.insight.query as any
+ expect(query.source?.tags?.productKey).toEqual(ProductKey.WEB_ANALYTICS)
+ })
+
+ it('does not inject tags into a DataTableNode with an EventsNode source', async () => {
+ // EventsNode forbids extra keys in its schema, so tagging it would produce an invalid query.
+ // The source must be left untouched (no tags field added).
+ const dataTableQuery = {
+ kind: NodeKind.DataTableNode,
+ source: {
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
+ },
+ }
+ router.actions.push(urls.insightNew({ query: dataTableQuery as any }))
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ const query = logic.values.insightLogicRef?.logic.values.insight.query as any
+ expect(query.source.kind).toEqual(NodeKind.EventsNode)
+ expect(query.source.tags).toBeUndefined()
+ })
+
it('persists edit mode in the url', async () => {
logic = insightSceneLogic()
logic.mount()
diff --git a/frontend/src/scenes/insights/insightSceneLogic.tsx b/frontend/src/scenes/insights/insightSceneLogic.tsx
index 56a91af00040..449ef03318bc 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.tsx
+++ b/frontend/src/scenes/insights/insightSceneLogic.tsx
@@ -23,10 +23,19 @@ import { urls } from 'scenes/urls'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { getDefaultQuery } from '~/queries/nodes/InsightViz/utils'
-import { DashboardFilter, FileSystemIconType, HogQLVariable, Node, TileFilters } from '~/queries/schema/schema-general'
+import {
+ DashboardFilter,
+ FileSystemIconType,
+ HogQLVariable,
+ Node,
+ NodeKind,
+ QueryLogTags,
+ TileFilters,
+} from '~/queries/schema/schema-general'
import {
checkLatestVersionsOnQuery,
convertDataTableNodeToDataVisualizationNode,
+ isDataTableNode,
isInsightVizNode,
} from '~/queries/utils'
import {
@@ -66,6 +75,29 @@ function normalizeItemId(itemId: string | undefined): string | number | null {
return itemId
}
+// Tag a new insight's query with the product_analytics productKey (on the executed source query) so
+// ClickHouse doesn't reject it as untagged. Leaves an existing productKey untouched.
+function withDefaultProductAnalyticsTags(query: Node): Node {
+ if (isInsightVizNode(query) && !query.source.tags?.productKey) {
+ return {
+ ...query,
+ source: { ...query.source, tags: { ...query.source.tags, ...PRODUCT_ANALYTICS_DEFAULT_QUERY_TAGS } },
+ } as Node
+ }
+ // EventsNode is the only DataTableNode source kind without a `tags` field and its schema forbids
+ // extra keys, so tagging it would make the payload invalid.
+ if (isDataTableNode(query) && query.source.kind !== NodeKind.EventsNode) {
+ const source = query.source as { tags?: QueryLogTags | null }
+ if (!source.tags?.productKey) {
+ return {
+ ...query,
+ source: { ...query.source, tags: { ...source.tags, ...PRODUCT_ANALYTICS_DEFAULT_QUERY_TAGS } },
+ } as Node
+ }
+ }
+ return query
+}
+
export const insightSceneLogic = kea<insightSceneLogicType>([
path(['scenes', 'insights', 'insightSceneLogic']),
connect(() => ({
@@ -434,7 +466,7 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
{
...createEmptyInsight('new'),
...(values.dashboardId ? { dashboards: [values.dashboardId] } : {}),
- query: upgradedQuery,
+ query: upgradedQuery ? withDefaultProductAnalyticsTags(upgradedQuery) : upgradedQuery,
},
{
fromPersistentApi: false,
@@ -570,16 +602,7 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
if ((initial || queryFromUrl || method === 'PUSH') && !validatingQuery) {
if (insightId === 'new' || insightId.startsWith('new-')) {
const query = queryFromUrl || getDefaultQuery(InsightType.TRENDS, values.filterTestAccountsDefault)
- const taggedQuery =
- isInsightVizNode(query) && !query.source.tags?.productKey
- ? {
- ...query,
- source: {
- ...query.source,
- tags: { ...query.source.tags, ...PRODUCT_ANALYTICS_DEFAULT_QUERY_TAGS },
- },
- }
- : query
+ const taggedQuery = withDefaultProductAnalyticsTags(query)
values.insightLogicRef?.logic.actions.setInsight(
{
...createEmptyInsight('new'),