: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(bi): add editable question starters (#124730)
Marius Andra02497f44ca40
diff --git a/docs/published/handbook/engineering/data-warehouse.md b/docs/published/handbook/engineering/data-warehouse.mdindex 5a737a60d220..7cfc992971cd 100644--- a/docs/published/handbook/engineering/data-warehouse.md+++ b/docs/published/handbook/engineering/data-warehouse.md@@ -416,7 +416,7 @@ Catalog SQL metrics can open as BI query snapshots. Opening a metric shows a loaAfter running a worksheet, **Propose as catalog metric** sends its resolved SQL and date window through catalog review. A pending or failed query cannot be proposed. Proposing an existing metric name updates its definition for review. Direct-connection worksheets cannot be proposed as catalog metrics.-The worksheet library suggests approved, non-drifted SQL metrics and certified sources. Each list requests at most three eligible candidates; drifted metrics are omitted without scanning more pages. Source context keeps certification visible and groups descriptions and relationship review status under **Catalog details**, collapsed by default to leave room for fields. Neutral catalog buttons show an external-link icon to indicate that they leave the worksheet. Relationship proposals open the catalog with the source prefilled; they become usable joins only after approval. Optional catalog suggestions do not prevent opening the worksheet library when the catalog is unavailable. Browser Back returns from a starter to the worksheet library; confirming discarded edits does not replace that history entry with an empty worksheet.+The worksheet library offers editable question starters for daily events, daily distinct people, events ranked by distinct people, and page views by referring domain. Each uses the last seven days of event data and opens as an unsaved worksheet; press **Run** to query it. The page-view starter requires captured `$pageview` events with `$referring_domain` for a useful breakdown. The worksheet library also suggests approved, non-drifted SQL metrics and certified sources. Each list requests at most three eligible candidates; drifted metrics are omitted without scanning more pages. Source context keeps certification visible and groups descriptions and relationship review status under **Catalog details**, collapsed by default to leave room for fields. Neutral catalog buttons show an external-link icon to indicate that they leave the worksheet. Relationship proposals open the catalog with the source prefilled; they become usable joins only after approval. Optional catalog suggestions do not prevent opening the worksheet library when the catalog is unavailable. Browser Back returns from a starter to the worksheet library; confirming discarded edits does not replace that history entry with an empty worksheet.The BI assistant receives the current worksheet draft, including its unsaved name, and shared catalog capabilities. Worksheet and catalog content remain untrusted context. Dismissing the saved worksheet attachment also removes its hidden draft and metric reference. Completed foreground insight tools refresh the worksheet library. Updates to the open worksheet refresh its saved version while preserving local edits. If both changed the query or name differently, saving over the worksheet is blocked until the local draft is discarded or saved as a copy. This conflict protection survives reloading a worksheet link. A failed refresh can be retried before saving.diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex cb4c7b367012..97fc64455cb9 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -6965,9 +6965,9 @@ snapshots:products-business-intelligence-worksheet-exploration--table-conditional-formatting--light:hash: v1.k794b7964.351c0fda7bf0696591ba8fae59fee71acf2a28865cfc5ce637468823c8303fdd.h8tIQMd2l9RRcDLLWKJ9e7fOe_vtc4lvbO0Ak93-5WIproducts-business-intelligence-worksheets--worksheets--dark:- hash: v1.k794b7964.838bf04ffdacba54c2f70f91dbc672e423e58a4ca8755f797d231d994dce8405.ZSy44AUoYYG-8us1HscrH2qR_SbXbBOzizBhHLB2Kak+ hash: v1.k794b7964.d1df9387de5567d9926e1a4f1af6432e787451bd86b2791c80ead800a38f0bc0.oy5LT9ZePUH1pJcr6VUR75vT8i76u9g9p6MU3WHzqgYproducts-business-intelligence-worksheets--worksheets--light:- hash: v1.k794b7964.b435a26273c02bd78bfd01da482544be433e3d08278d12414964992e8a302680.3XeiqFTbhaITSZgaEJ42jC4UfSZB7Rdwk9f9Iq_HpNc+ hash: v1.k794b7964.0654432f426ef88b0ef02fc6e2565d438ac2ca026366683b4f1c06db07148c03.z-7Vqzq0H7LR9bxYgn7nLIKxX45E_4f1U9INfUp67wgproducts-dashboards--dashboard-states--dark:hash: v1.k794b7964.cc591635f83447f7bcfb802062302b8b1bbcd51826110b12fa0f68d4830dcfd3.CIgt_KxCyQRgbn7DPF4MDXc5JBlSxbaCPS0Snyx3cRsproducts-dashboards--dashboard-states--light:diff --git a/products/business_intelligence/frontend/BIStarters.tsx b/products/business_intelligence/frontend/BIStarters.tsxindex 9ad9202137a4..88020599d48b 100644--- a/products/business_intelligence/frontend/BIStarters.tsx+++ b/products/business_intelligence/frontend/BIStarters.tsx@@ -2,39 +2,11 @@ import { useValues } from 'kea'import { LemonButton, LemonTag } from '@posthog/lemon-ui'-import { urls } from 'scenes/urls'--import { BIConfig, BIVisualizationNode } from '~/queries/schema/schema-business-intelligence'-import { NodeKind } from '~/queries/schema/schema-general'-import { ChartDisplayType } from '~/types'-import { captureBIWorksheetAction } from './biEditorAnalytics'-import { buildBIQuery, DEFAULT_BI_CONFIG } from './biEditorTypes'+import { DEFAULT_BI_CONFIG } from './biEditorTypes'+import { BI_QUESTION_STARTERS, starterWorksheetUrl } from './biQuestionStarters'import { biStartersLogic } from './biStartersLogic'-export function starterWorksheetUrl(config: BIConfig): string {- const built = buildBIQuery(config)!- const worksheet: BIVisualizationNode = { ...built.node, kind: NodeKind.BIVisualizationNode, config }- return `${urls.businessIntelligenceNew()}?starter=1#q=${encodeURIComponent(JSON.stringify(worksheet))}`-}--const EVENTS_CONFIG: BIConfig = {- ...DEFAULT_BI_CONFIG,- source: { table: 'events' },- dateRange: { date_from: '-7d' },- chartType: ChartDisplayType.ActionsLineGraph,- rows: [- {- id: 'starter:timestamp',- name: 'timestamp',- expression: 'timestamp',- source: { table: 'events' },- type: 'datetime',- dateBucket: 'day',- },- ],-}-export function BIStarters(): JSX.Element {const { sources } = useValues(biStartersLogic)return (@@ -64,16 +36,23 @@ export function BIStarters(): JSX.Element {</LemonButton>)})}- <LemonButton- type="secondary"- size="small"- to={starterWorksheetUrl(EVENTS_CONFIG)}- onClick={() =>- captureBIWorksheetAction('starter_selected', EVENTS_CONFIG, { starter_kind: 'events' })- }- >- Events per day · Last 7 days- </LemonButton>+ {BI_QUESTION_STARTERS.map((starter) => (+ <LemonButton+ key={starter.id}+ type="secondary"+ size="small"+ className="max-w-full"+ to={starterWorksheetUrl(starter.config)}+ onClick={() =>+ captureBIWorksheetAction('starter_selected', starter.config, { starter_kind: starter.id })+ }+ >+ <span className="flex min-w-0 flex-col items-start whitespace-normal py-1 text-left">+ <span>{starter.title}</span>+ <span className="text-xs font-normal text-secondary">{starter.description}</span>+ </span>+ </LemonButton>+ ))}</div><p className="m-0 text-xs text-secondary">Choose a starting point, add fields, then Run. Save your worksheet to return to it or add it to adiff --git a/products/business_intelligence/frontend/biEditorAnalytics.ts b/products/business_intelligence/frontend/biEditorAnalytics.tsindex f6f5e2b717dc..b1e65484a50d 100644--- a/products/business_intelligence/frontend/biEditorAnalytics.ts+++ b/products/business_intelligence/frontend/biEditorAnalytics.ts@@ -128,7 +128,7 @@ export function captureBIWorksheetAction(hierarchy_axis?: 'rows' | 'columns'hierarchy_depth?: numberexpanded?: boolean- starter_kind?: 'metric' | 'certified_source' | 'events'+ starter_kind?: 'metric' | 'certified_source' | 'events' | 'daily_people' | 'event_reach' | 'pageview_sources'source_certification?: 'certified' | 'deprecated' | 'unmarked'setting_keys?: string[]rule_count?: numberdiff --git a/products/business_intelligence/frontend/biQuestionStarters.test.ts b/products/business_intelligence/frontend/biQuestionStarters.test.tsnew file mode 100644index 000000000000..b948425f6a1f--- /dev/null+++ b/products/business_intelligence/frontend/biQuestionStarters.test.ts@@ -0,0 +1,27 @@+import { BIVisualizationNode } from '~/queries/schema/schema-business-intelligence'++import { BIEditorView, buildBIQuery, parseBIEditorState } from './biEditorTypes'+import { BI_QUESTION_STARTERS, starterWorksheetUrl } from './biQuestionStarters'++it('opens each question as editable shelves with a seven-day window, without a SQL snapshot', () => {+ for (const starter of BI_QUESTION_STARTERS) {+ const url = new URL(starterWorksheetUrl(starter.config), 'https://example.com')+ const worksheet = JSON.parse(new URLSearchParams(url.hash.slice(1)).get('q')!) as BIVisualizationNode+ const restored = parseBIEditorState(BIEditorView.BI, JSON.stringify(worksheet.config))!.config+ expect(url.searchParams.get('starter')).toBe('1')+ expect(restored.querySnapshot).toBeUndefined()+ expect(restored.dateRange?.date_from).toBe('-7d')+ expect(buildBIQuery(restored)?.node.source).toEqual(worksheet.source)+ }+})++it('counts people distinctly and restricts referral counts to page views', () => {+ const people = BI_QUESTION_STARTERS.find(({ id }) => id === 'daily_people')!.config+ const reach = BI_QUESTION_STARTERS.find(({ id }) => id === 'event_reach')!.config+ const referrers = BI_QUESTION_STARTERS.find(({ id }) => id === 'pageview_sources')!.config+ expect(buildBIQuery(people)?.query).toContain('count(DISTINCT person_id)')+ expect(reach.sort?.direction).toBe('desc')+ expect(buildBIQuery(reach)?.query).toContain('count(DISTINCT person_id)')+ expect(referrers.filters).toEqual([expect.objectContaining({ operator: 'equals', value: '$pageview' })])+ expect(buildBIQuery(referrers)?.query).toContain('properties.$referring_domain')+})diff --git a/products/business_intelligence/frontend/biQuestionStarters.ts b/products/business_intelligence/frontend/biQuestionStarters.tsnew file mode 100644index 000000000000..5ef2ba0723cd--- /dev/null+++ b/products/business_intelligence/frontend/biQuestionStarters.ts@@ -0,0 +1,89 @@+import { urls } from 'scenes/urls'++import { BIConfig, BIVisualizationNode } from '~/queries/schema/schema-business-intelligence'+import { NodeKind } from '~/queries/schema/schema-general'+import { ChartDisplayType } from '~/types'++import { buildBIQuery, DEFAULT_BI_CONFIG, getBIValueSortKey } from './biEditorTypes'++export function starterWorksheetUrl(config: BIConfig): string {+ const built = buildBIQuery(config)!+ const worksheet: BIVisualizationNode = { ...built.node, kind: NodeKind.BIVisualizationNode, config }+ return `${urls.businessIntelligenceNew()}?starter=1#q=${encodeURIComponent(JSON.stringify(worksheet))}`+}++export const EVENTS_CONFIG: BIConfig = {+ ...DEFAULT_BI_CONFIG,+ source: { table: 'events' },+ dateRange: { date_from: '-7d' },+ chartType: ChartDisplayType.ActionsLineGraph,+ rows: [+ {+ id: 'starter:timestamp',+ name: 'timestamp',+ expression: 'timestamp',+ source: { table: 'events' },+ type: 'datetime',+ dateBucket: 'day',+ },+ ],+}++const people = {+ field: {+ id: 'starter:person_id',+ name: 'person_id',+ expression: 'person_id',+ source: { table: 'events' },+ type: 'string' as const,+ },+ aggregation: 'count_distinct' as const,+ display: { label: 'People' },+}+const eventReach: BIConfig = {+ ...EVENTS_CONFIG,+ chartType: ChartDisplayType.ActionsBar,+ rows: [{ id: 'starter:event', name: 'event', expression: 'event', source: { table: 'events' }, type: 'string' }],+ values: [people],+ limit: 100,+}++export const BI_QUESTION_STARTERS = [+ {+ id: 'events' as const,+ title: 'How many events happen each day?',+ description: 'Daily event count · Last 7 days',+ config: EVENTS_CONFIG,+ },+ {+ id: 'daily_people' as const,+ title: 'How many people are active each day?',+ description: 'Distinct people per day · Last 7 days',+ config: { ...EVENTS_CONFIG, values: [people] },+ },+ {+ id: 'event_reach' as const,+ title: 'Which events reach the most people?',+ description: 'Events ranked by distinct people · Last 7 days',+ config: { ...eventReach, sort: { key: getBIValueSortKey(eventReach, 0)!, direction: 'desc' as const } },+ },+ {+ id: 'pageview_sources' as const,+ title: 'Where do page views come from?',+ description: 'Page views by referring domain · Last 7 days',+ config: {+ ...EVENTS_CONFIG,+ chartType: ChartDisplayType.ActionsBar,+ rows: [+ {+ id: 'starter:referring_domain',+ name: 'Referring domain',+ expression: 'properties.$referring_domain',+ source: { table: 'events' },+ type: 'string' as const,+ },+ ],+ filters: [{ field: eventReach.rows[0], operator: 'equals' as const, value: '$pageview' }],+ },+ },+]