: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
feat(metrics): keep a metrics tab in the insight type tabs (#112085)
Frank Hamand9096209bfb08
diff --git a/frontend/src/queries/nodes/InsightViz/utils.ts b/frontend/src/queries/nodes/InsightViz/utils.tsindex 12254b9c2a4b..b6b34b6b9dff 100644--- a/frontend/src/queries/nodes/InsightViz/utils.ts+++ b/frontend/src/queries/nodes/InsightViz/utils.ts@@ -10,6 +10,7 @@ import {HogQuery,InsightQueryNode,InsightVizNode,+ MetricsQuery,Node,NodeKind,} from '~/queries/schema/schema-general'@@ -95,13 +96,17 @@ export const queryFromKind = (export const getDefaultQuery = (insightType: InsightType,filterTestAccountsDefault: boolean-): DataTableNode | DataVisualizationNode | HogQuery | InsightVizNode => {+): DataTableNode | DataVisualizationNode | HogQuery | InsightVizNode | MetricsQuery => {// Web Analytics insights should always come from Web Analytics tiles with a pre-configured query// This is a fallback that should rarely be usedif (insightType === InsightType.WEB_ANALYTICS) {throw new Error('Web Analytics insights must be created from Web Analytics tiles')}+ if (insightType === InsightType.METRICS) {+ return { kind: NodeKind.MetricsQuery, clauses: [], dateRange: { date_from: '-1h' } }+ }+if ([InsightType.SQL, InsightType.JSON, InsightType.HOG].includes(insightType)) {if (insightType === InsightType.JSON) {return examples.TotalEventsTable as DataTableNodediff --git a/frontend/src/scenes/insights/InsightNav/insightNavLogic.test.ts b/frontend/src/scenes/insights/InsightNav/insightNavLogic.test.tsindex fd262fb49528..89e36075f65b 100644--- a/frontend/src/scenes/insights/InsightNav/insightNavLogic.test.ts+++ b/frontend/src/scenes/insights/InsightNav/insightNavLogic.test.ts@@ -17,6 +17,7 @@ import {EventsQuery,FunnelsQuery,InsightVizNode,+ MetricsQuery,NodeKind,Node,ProductKey,@@ -233,6 +234,50 @@ describe('insightNavLogic', () => {})})+ describe('metrics tab visibility', () => {+ const enableMetricsBuilder = (): void => {+ featureFlagLogic.actions.setFeatureFlags([], {+ [FEATURE_FLAGS.METRICS]: true,+ [FEATURE_FLAGS.METRICS_INSIGHT_BUILDER]: true,+ })+ }++ it('hides the metrics tab without both flags', () => {+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS_INSIGHT_BUILDER]: true })+ expect(logic.values.tabs.map((tab) => tab.type)).not.toContain(InsightType.METRICS)+ })++ it('keeps the metrics tab and its draft after switching to another tab and back', async () => {+ enableMetricsBuilder()++ await expectLogic(builtInsightDataLogic, () => {+ logic.actions.setActiveView(InsightType.METRICS)+ }).toFinishAllListeners()+ expect(logic.values.activeView).toEqual(InsightType.METRICS)+ expect(builtInsightDataLogic.values.query).toMatchObject({ kind: NodeKind.MetricsQuery, clauses: [] })++ const editedQuery: MetricsQuery = {+ kind: NodeKind.MetricsQuery,+ clauses: [{ name: 'a', metricName: 'requests_total', aggregation: 'rate' }],+ dateRange: { date_from: '-6h' },+ }+ await expectLogic(builtInsightDataLogic, () => {+ builtInsightDataLogic.actions.setQuery(editedQuery)+ }).toFinishAllListeners()++ await expectLogic(builtInsightDataLogic, () => {+ logic.actions.setActiveView(InsightType.TRENDS)+ }).toFinishAllListeners()+ expect(logic.values.tabs.map((tab) => tab.type)).toContain(InsightType.METRICS)++ await expectLogic(builtInsightDataLogic, () => {+ logic.actions.setActiveView(InsightType.METRICS)+ }).toFinishAllListeners()+ expect(logic.values.activeView).toEqual(InsightType.METRICS)+ expect(builtInsightDataLogic.values.query).toEqual(editedQuery)+ })+ })+describe('query cache', () => {const trendsQuery: InsightVizNode = {kind: NodeKind.InsightVizNode,diff --git a/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx b/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsxindex 3ea68a8b2e81..c43583ad1a64 100644--- a/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx+++ b/frontend/src/scenes/insights/InsightNav/insightNavLogic.tsx@@ -43,6 +43,7 @@ import {StickinessQuery,TrendsFilter,TrendsQuery,+ MetricsQuery,} from '~/queries/schema/schema-general'import {containsHogQLQuery,@@ -61,6 +62,7 @@ import {isInsightVizNode,isLifecycleDataWarehouseNode,isLifecycleQuery,+ isMetricsQuery,isPathsQuery,isPathsV2Query,isRetentionQuery,@@ -510,6 +512,7 @@ export interface insightNavLogicValues {filterTestAccountsDefault: boolean // filterTestAccountsDefaultsLogicquery: Node | null // insightDataLogicactiveView: InsightType+ metricsQueryCache: MetricsQuery | nullqueryPropertyCache: QueryPropertyCache | nulltabs: Tab[]}@@ -526,6 +529,9 @@ export interface insightNavLogicActions {setActiveView: (view: InsightType) => {view: InsightType}+ updateMetricsQueryCache: (query: MetricsQuery) => {+ query: MetricsQuery+ }updateQueryPropertyCache: (cache: QueryPropertyCache) => {cache: QueryPropertyCache}@@ -565,6 +571,7 @@ export const insightNavLogic = kea<insightNavLogicType>([actions({setActiveView: (view: InsightType) => ({ view }),updateQueryPropertyCache: (cache: QueryPropertyCache) => ({ cache }),+ updateMetricsQueryCache: (query: MetricsQuery) => ({ query }),}),reducers({queryPropertyCache: [@@ -576,6 +583,13 @@ export const insightNavLogic = kea<insightNavLogicType>([}),},],+ // Metrics clauses share nothing with the product analytics query cache, so the draft is kept whole.+ metricsQueryCache: [+ null as MetricsQuery | null,+ {+ updateMetricsQueryCache: (_, { query }) => query,+ },+ ],}),selectors({activeView: [@@ -587,6 +601,8 @@ export const insightNavLogic = kea<insightNavLogicType>([return InsightType.SQL} else if (isHogQuery(query)) {return InsightType.HOG+ } else if (isMetricsQuery(query)) {+ return InsightType.METRICS} else if (isInsightVizNode(query)) {// Check for Web Analytics queries first before using the mappingif (isWebAnalyticsInsightQuery(query.source)) {@@ -661,6 +677,17 @@ export const insightNavLogic = kea<insightNavLogicType>([})}+ if (+ (featureFlags[FEATURE_FLAGS.METRICS] && featureFlags[FEATURE_FLAGS.METRICS_INSIGHT_BUILDER]) ||+ activeView === InsightType.METRICS+ ) {+ tabs.push({+ label: 'Metrics',+ type: InsightType.METRICS,+ dataAttr: 'insight-metrics-tab',+ })+ }+if (activeView === InsightType.WEB_ANALYTICS) {// Like the json only, this is a temporary tab for Web Analytics insights.// We don't display it otherwise and humans shouldn't be able to click to select this tab@@ -694,6 +721,10 @@ export const insightNavLogic = kea<insightNavLogicType>([}),listeners(({ values, actions }) => ({setActiveView: ({ view }) => {+ if (view === InsightType.METRICS && values.metricsQueryCache) {+ actions.setQuery(values.metricsQueryCache)+ return+ }const query = getDefaultQuery(view, values.filterTestAccountsDefault)if (isDataVisualizationNode(query)) {@@ -711,7 +742,9 @@ export const insightNavLogic = kea<insightNavLogicType>([}},setQuery: ({ query }) => {- if (isInsightVizNode(query)) {+ if (isMetricsQuery(query)) {+ actions.updateMetricsQueryCache(query)+ } else if (isInsightVizNode(query)) {actions.updateQueryPropertyCache(cachePropertiesFromQuery(query.source, values.queryPropertyCache))} else if (isDataTableNode(query)) {const seeded = cachePropertiesFromDataTable(query)@@ -722,7 +755,9 @@ export const insightNavLogic = kea<insightNavLogicType>([},})),afterMount(({ values, actions }) => {- if (values.query && isInsightVizNode(values.query)) {+ if (isMetricsQuery(values.query)) {+ actions.updateMetricsQueryCache(values.query)+ } else if (values.query && isInsightVizNode(values.query)) {actions.updateQueryPropertyCache(cachePropertiesFromQuery(values.query.source, values.queryPropertyCache))} else if (values.query && isDataTableNode(values.query)) {const seeded = cachePropertiesFromDataTable(values.query)diff --git a/frontend/src/scenes/insights/utils.tsx b/frontend/src/scenes/insights/utils.tsxindex ce670716bbdc..58c91b938148 100644--- a/frontend/src/scenes/insights/utils.tsx+++ b/frontend/src/scenes/insights/utils.tsx@@ -530,6 +530,7 @@ export const INSIGHT_TYPE_URLS: Record<InsightType | string, string> = {[InsightType.PATHS]: urls.insightNew({ type: InsightType.PATHS }),[InsightType.JOURNEYS]: urls.insightNew({ type: InsightType.JOURNEYS }),[InsightType.WEB_ANALYTICS]: urls.insightNew({ type: InsightType.WEB_ANALYTICS }),+ [InsightType.METRICS]: urls.insightNew({ type: InsightType.METRICS }),JSON: urls.insightNew({ query: examples.EventsTableFull }),HOG: urls.insightNew({ query: examples.Hoggonacci }),SQL: urls.sqlEditor({ query: (examples.HogQLForDataVisualization as HogQLQuery)['query'] }),diff --git a/frontend/src/scenes/saved-insights/NewInsightMenu.tsx b/frontend/src/scenes/saved-insights/NewInsightMenu.tsxindex c8c61db8e879..a5e12bcd852e 100644--- a/frontend/src/scenes/saved-insights/NewInsightMenu.tsx+++ b/frontend/src/scenes/saved-insights/NewInsightMenu.tsx@@ -23,7 +23,7 @@ import {import { Scene } from 'scenes/sceneTypes'import { urls } from 'scenes/urls'-import { InsightVizNode, MetricsQuery, NodeKind, TrendsQuery } from '~/queries/schema/schema-general'+import { InsightVizNode, NodeKind, TrendsQuery } from '~/queries/schema/schema-general'import { AccessControlLevel, AccessControlResourceType, BaseMathType, ChartDisplayType, InsightType } from '~/types'import {@@ -158,17 +158,15 @@ const AI_CARD: NewInsightCardSpec = {dataAttr: 'new-insight-menu-ai',}-const NEW_METRICS_QUERY: MetricsQuery = { kind: NodeKind.MetricsQuery, clauses: [], dateRange: { date_from: '-1h' } }-const METRICS_CARD: NewInsightCardSpec = {key: 'metrics',name: QUERY_TYPES_METADATA[NodeKind.MetricsQuery].name,description: QUERY_TYPES_METADATA[NodeKind.MetricsQuery].description ?? '',icon: QUERY_TYPES_METADATA[NodeKind.MetricsQuery].icon,sketch: GenericInsightSketch,- to: urls.insightNew({ query: NEW_METRICS_QUERY }),+ to: urls.insightNew({ type: InsightType.METRICS }),dataAttr: 'new-insight-menu-metrics',- onClick: () => eventUsageLogic.actions.reportSavedInsightNewInsightClicked('METRICS'),+ onClick: () => eventUsageLogic.actions.reportSavedInsightNewInsightClicked(InsightType.METRICS),}function useNewInsightCards(): {diff --git a/frontend/src/scenes/saved-insights/insightTypesMetadata.tsx b/frontend/src/scenes/saved-insights/insightTypesMetadata.tsxindex 3e7873f511a2..ade2ba269e30 100644--- a/frontend/src/scenes/saved-insights/insightTypesMetadata.tsx+++ b/frontend/src/scenes/saved-insights/insightTypesMetadata.tsx@@ -771,6 +771,8 @@ export const INSIGHT_TYPES_METADATA: Record<InsightType, InsightTypeMetadata> =inMenu: false,flag: FEATURE_FLAGS.HOG,},+ // The new insight menu adds its own Metrics card, because the card needs two flags.+ [InsightType.METRICS]: { ...QUERY_TYPES_METADATA[NodeKind.MetricsQuery], inMenu: false },[InsightType.WEB_ANALYTICS]: {name: 'Web Analytics',description: 'Web analytics insights from your website data.',diff --git a/frontend/src/types.ts b/frontend/src/types.tsindex 12c48993ac2b..1dd4b552fce9 100644--- a/frontend/src/types.ts+++ b/frontend/src/types.ts@@ -3170,6 +3170,7 @@ export enum InsightType {SQL = 'SQL',HOG = 'HOG',WEB_ANALYTICS = 'WEB_ANALYTICS',+ METRICS = 'METRICS',}export enum PathType {