: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(product-analytics): show the daily share for unique counts (#124399)
Sam Penningtond4515d38b543
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.tsindex 182b6d7a6f3f..d6e6f573ab0f 100644--- a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts@@ -2,6 +2,7 @@ import { expectLogic } from 'kea-test-utils'import { insightDataLogic } from 'scenes/insights/insightDataLogic'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'+import { BREAKDOWN_OTHER_STRING_LABEL } from 'scenes/insights/utils'import { useMocks } from '~/mocks/jest'import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'@@ -463,6 +464,95 @@ describe('insightsTableDataLogic', () => {expect(logic.values.getPercentOfDayTotal(logic.values.indexedResults[0], 0)).toBeNull()})+ it.each([+ [+ 'load',+ 200,+ [+ [null, 1],+ [null, 0.5],+ [null, null],+ ],+ ],+ [+ 'fail',+ 500,+ [+ [null, null],+ [null, null],+ [null, null],+ ],+ ],+ ] as [string, number, (number | null)[][]][])(+ 'unique users divide by the daily totals without the breakdown when they %s',+ async (_name, totalsStatus, expectedUniqueShares) => {+ const sentQueries: TrendsQuery[] = []+ useMocks({+ post: {+ '/api/environments/:team_id/query/:query_kind/': async ({ request }) => {+ sentQueries.push(((await request.json()) as { query: TrendsQuery }).query)+ return [+ totalsStatus,+ {+ results: [+ makeCompareResult({+ order: 0,+ days: ['2023-01-02', '2023-01-03'],+ data: [2, 9],+ }),+ ],+ },+ ]+ },+ },+ })+ const query: TrendsQuery = {+ kind: NodeKind.TrendsQuery,+ series: [+ { kind: NodeKind.EventsNode, event: '$pageview' },+ { kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.UniqueUsers },+ ],+ breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },+ }+ const insight: Partial<InsightModel> = {+ result: [+ makeCompareResult({ order: 0, breakdown_value: 'Chrome', data: [4, 4] }),+ makeCompareResult({ order: 0, breakdown_value: 'Firefox', data: [4, 0] }),+ makeCompareResult({ order: 1, breakdown_value: 'Chrome', data: [1, 2] }),+ makeCompareResult({ order: 1, breakdown_value: 'Firefox', data: [1, 1] }),+ makeCompareResult({ order: 1, breakdown_value: BREAKDOWN_OTHER_STRING_LABEL, data: [1, 1] }),+ ],+ }++ await expectLogic(logic, () => {+ insightVizDataLogic.findMounted(props)?.actions.updateQuerySource(query)+ builtDataNodeLogic.actions.loadDataSuccess(insight)+ }).toFinishAllListeners()++ expect(sentQueries.map((q) => [q.series, q.breakdownFilter])).toEqual([[[query.series[1]], undefined]])+ const shares = (): (number | null)[][] =>+ logic.values.indexedResults.map((r) => [+ logic.values.getPercentOfDayTotal(r, 0),+ logic.values.getPercentOfDayTotal(r, 1),+ ])+ expect(shares()).toEqual([[0.5, 1], [0.5, 0], ...expectedUniqueShares])++ insightVizDataLogic+ .findMounted(props)+ ?.actions.updateQuerySource({ trendsFilter: { display: ChartDisplayType.ActionsBar } })+ await expectLogic(logic).toFinishAllListeners()+ expect(shares()).toEqual([[0.5, 1], [0.5, 0], ...expectedUniqueShares])++ insightVizDataLogic.findMounted(props)?.actions.updateQuerySource({ dateRange: { date_from: '-30d' } })+ await expectLogic(logic).toFinishAllListeners()+ expect(shares().slice(2)).toEqual([+ [null, null],+ [null, null],+ [null, null],+ ])+ }+ )+it('percent of total divides by the total of the same series and period', async () => {const row = (order: number,diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.tsindex e5ed372a2490..f4cf1177570b 100644--- a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts@@ -1,10 +1,17 @@-import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'+import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'+import { loaders } from 'kea-loaders'+import { uuid } from 'lib/utils/dom'import { average, median } from 'lib/utils/numbers'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'-import { getResultOrder, hasFormulaResults } from 'scenes/insights/utils/resultSeries'+import { isOtherBreakdown } from 'scenes/insights/utils'+import { compareDataNodeQuery } from 'scenes/insights/utils/queryUtils'+import { ResultSeriesRow, getResultOrder, hasFormulaResults } from 'scenes/insights/utils/resultSeries'+import { teamLogic } from 'scenes/teamLogic'+import { cancelQueryOnServer, concurrencyController } from '~/queries/nodes/DataNode/dataNodeLogic'+import { performQuery } from '~/queries/query'import type {AnyDataWarehouseNode,AnyEntityNode,@@ -18,6 +25,7 @@ import type {LifecycleQuery,PathsQuery,PathsV2Query,+ RefreshType,RetentionQuery,StickinessQuery,TrendsQuery,@@ -25,7 +33,7 @@ import type {WebStatsTableQuery,} from '~/queries/schema/schema-general'import { hasBreakdownFilter } from '~/queries/utils'-import { ChartDisplayType, CompareLabelType, InsightLogicProps } from '~/types'+import { ChartDisplayType, CompareLabelType, InsightLogicProps, TrendResult } from '~/types'import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'@@ -86,16 +94,35 @@ function isSessionPropertySum(entity: EntityNode): boolean {)}-function percentOfTotalGroupKey(item: IndexedTrendResult): string {+// One person can count under several breakdown values, so these need their own distinct daily totals.+const UNIQUE_COUNT_MATHS = new Set<string>(['dau', 'weekly_active', 'monthly_active', 'unique_session', 'unique_group'])++function percentOfTotalGroupKey(item: ResultSeriesRow & Pick<TrendResult, 'compare_label'>): string {return JSON.stringify([getResultOrder(item), item.compare_label ?? ''])}+function isOtherBreakdownValue(breakdownValue: IndexedTrendResult['breakdown_value']): boolean {+ return Array.isArray(breakdownValue) ? breakdownValue.some(isOtherBreakdown) : isOtherBreakdown(breakdownValue)+}++export interface UniqueDayTotals {+ query: TrendsQuery+ // `order` is the series' position in `query`, not in the insight.+ rows: { order: number; compare_label: TrendResult['compare_label']; days: string[]; data: number[] }[]+}++function isSameTotalsQuery(a: TrendsQuery | null | undefined, b: TrendsQuery | null): boolean {+ return !!a && !!b && compareDataNodeQuery(a, b, { ignoreVisualizationOnlyChanges: true })+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface insightsTableDataLogicValues {display: ChartDisplayType | null | undefined // insightVizDataLogic+ insightDataLoading: boolean // insightVizDataLogicisTrends: boolean // insightVizDataLogicpersistedAggregationType: AggregationType | undefined // insightVizDataLogicseries: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined // insightVizDataLogic+ currentTeamId: number | null // teamLogicbreakdownFilter: BreakdownFilter | null | undefined // trendsDataLogiccompareFilter: CompareFilter | null | undefined // trendsDataLogicindexedResults: IndexedTrendResult[] // trendsDataLogic@@ -113,6 +140,8 @@ export interface insightsTableDataLogicValues {| null // trendsDataLogicaggregation: AggregationTypeallowAggregation: boolean+ currentUniqueDayTotals: Map<string, Map<string, number>> | null+ dayShareFits: booleandisplayResults: IndexedTrendResult[]getPercentOfDayTotal: (item: IndexedTrendResult, index: number) => number | nullgetPercentOfTotal: (item: IndexedTrendResult) => number | null@@ -123,13 +152,91 @@ export interface insightsTableDataLogicValues {previousResultMap: Map<string, IndexedTrendResult>showPercentOfDayTotal: booleanshowPercentOfTotal: boolean+ uniqueCountSeriesOrders: number[]+ uniqueDayTotals: UniqueDayTotals | null+ uniqueDayTotalsLoading: boolean+ uniqueDayTotalsQuery: TrendsQuery | null}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface insightsTableDataLogicActions {+ loadData: (+ refresh?: RefreshType | undefined,+ alreadyRunningQueryId?: string | undefined,+ overrideQuery?: import('~/queries/schema').DataNode<Record<string, any>> | undefined+ ) => {+ overrideQuery: import('~/queries/schema').DataNode<Record<string, any>> | undefined+ pollOnly: boolean+ queryId: string+ refresh: RefreshType | undefined+ } // insightVizDataLogic+ loadDataSuccess: (+ response:+ | Record<string, any>+ | null+ | import('~/queries/schema').ErrorTrackingQueryResponse+ | import('~/queries/schema').HogQLAutocompleteResponse+ | import('~/queries/schema').HogQLMetadataResponse+ | import('~/queries/schema').HogQLQueryResponse<any[]>+ | import('~/queries/schema').HogQueryResponse+ | import('~/queries/schema').LogAttributesQueryResponse+ | import('~/queries/schema').LogValuesQueryResponse+ | import('~/queries/schema').MetricsQueryResponse+ | import('~/queries/schema').SessionsQueryResponse+ | import('~/queries/schema').TraceSpansAggregationQueryResponse+ | import('~/queries/schema').TraceSpansAttributeBreakdownQueryResponse+ | import('~/queries/schema').TraceSpansQueryResponse+ | import('~/queries/schema').TraceSpansTreeQueryResponse,+ payload?:+ | {+ overrideQuery: import('~/queries/schema').DataNode<Record<string, any>> | undefined+ pollOnly: boolean+ queryId: string+ refresh: RefreshType | undefined+ }+ | undefined+ ) => {+ payload?: {+ overrideQuery: import('~/queries/schema').DataNode<Record<string, any>> | undefined+ pollOnly: boolean+ queryId: string+ refresh: RefreshType | undefined+ }+ response:+ | Record<string, any>+ | null+ | import('~/queries/schema').ErrorTrackingQueryResponse+ | import('~/queries/schema').HogQLAutocompleteResponse+ | import('~/queries/schema').HogQLMetadataResponse+ | import('~/queries/schema').HogQLQueryResponse<any[]>+ | import('~/queries/schema').HogQueryResponse+ | import('~/queries/schema').LogAttributesQueryResponse+ | import('~/queries/schema').LogValuesQueryResponse+ | import('~/queries/schema').MetricsQueryResponse+ | import('~/queries/schema').SessionsQueryResponse+ | import('~/queries/schema').TraceSpansAggregationQueryResponse+ | import('~/queries/schema').TraceSpansAttributeBreakdownQueryResponse+ | import('~/queries/schema').TraceSpansQueryResponse+ | import('~/queries/schema').TraceSpansTreeQueryResponse+ } // insightVizDataLogicsetDetailedResultsAggregationType: (detailedResultsAggregationType: AggregationType) => {detailedResultsAggregationType: AggregationType} // insightVizDataLogic+ loadUniqueDayTotals: (refresh: RefreshType) => RefreshType+ loadUniqueDayTotalsFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ loadUniqueDayTotalsSuccess: (+ uniqueDayTotals: UniqueDayTotals | null,+ payload?: RefreshType+ ) => {+ uniqueDayTotals: UniqueDayTotals | null+ payload?: RefreshType+ }toggleColumnPin: (columnKey: string) => {columnKey: string}@@ -184,7 +291,7 @@ export interface insightsTableDataLogicMeta {indexedResults: IndexedTrendResult[],isNonTimeSeriesDisplay: boolean) => (item: IndexedTrendResult) => number | null- showPercentOfDayTotal: (+ dayShareFits: (isTrends: boolean,isNonTimeSeriesDisplay: boolean,series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,@@ -201,9 +308,40 @@ export interface insightsTableDataLogicMeta {| WebStatsTableQuery| null) => boolean+ uniqueCountSeriesOrders: (+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined+ ) => number[]+ uniqueDayTotalsQuery: (+ dayShareFits: boolean,+ querySource:+ | FunnelsQuery+ | LifecycleQuery+ | PathsQuery+ | PathsV2Query+ | RetentionQuery+ | StickinessQuery+ | TrendsQuery+ | WebOverviewQuery+ | WebStatsTableQuery+ | null,+ uniqueCountSeriesOrders: number[]+ ) => TrendsQuery | null+ currentUniqueDayTotals: (+ uniqueDayTotals: UniqueDayTotals | null,+ uniqueDayTotalsQuery: TrendsQuery | null,+ uniqueCountSeriesOrders: number[]+ ) => Map<string, Map<string, number>> | null+ showPercentOfDayTotal: (+ dayShareFits: boolean,+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,+ uniqueCountSeriesOrders: number[],+ currentUniqueDayTotals: Map<string, Map<string, number>> | null+ ) => booleangetPercentOfDayTotal: (indexedResults: IndexedTrendResult[],- showPercentOfDayTotal: boolean+ showPercentOfDayTotal: boolean,+ uniqueCountSeriesOrders: number[],+ currentUniqueDayTotals: Map<string, Map<string, number>> | null) => (item: IndexedTrendResult, index: number) => number | null}}@@ -223,17 +361,91 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([connect((props: InsightLogicProps) => ({values: [insightVizDataLogic(props),- ['isTrends', 'display', 'series', 'detailedResultsAggregationType as persistedAggregationType'],+ [+ 'isTrends',+ 'display',+ 'series',+ 'detailedResultsAggregationType as persistedAggregationType',+ 'insightDataLoading',+ ],trendsDataLogic(props),['indexedResults', 'compareFilter', 'breakdownFilter', 'isNonTimeSeriesDisplay', 'querySource'],+ teamLogic,+ ['currentTeamId'],],- actions: [insightVizDataLogic(props), ['setDetailedResultsAggregationType']],+ actions: [insightVizDataLogic(props), ['setDetailedResultsAggregationType', 'loadData', 'loadDataSuccess']],})),actions({toggleColumnPin: (columnKey: string) => ({ columnKey }),}),+ loaders(({ values, props, cache }) => ({+ uniqueDayTotals: [+ null as UniqueDayTotals | null,+ {+ loadUniqueDayTotals: async (refresh: RefreshType, breakpoint) => {+ const query = values.uniqueDayTotalsQuery+ if (!query || props.doNotLoad || !values.currentTeamId) {+ return null+ }+ if (refresh !== 'force_blocking' && isSameTotalsQuery(values.uniqueDayTotals?.query, query)) {+ return values.uniqueDayTotals+ }+ const teamId = values.currentTeamId+ const queryId = uuid()+ const abortController = new AbortController()+ let finished = false+ cache.disposables.add(+ () => () => {+ if (!finished) {+ abortController.abort()+ void cancelQueryOnServer(teamId, queryId)+ }+ },+ 'uniqueDayTotalsQuery',+ { pauseOnPageHidden: false }+ )+ let results: TrendResult[]+ try {+ const response = await concurrencyController.run({+ debugTag: 'uniqueDayTotals',+ abortController,+ fn: () =>+ performQuery(+ query,+ { signal: abortController.signal },+ refresh,+ queryId,+ undefined,+ // A dashboard breakdown override would split the totals again.+ props.filtersOverride && { ...props.filtersOverride, breakdown_filter: undefined },+ props.variablesOverride+ ),+ })+ results = response.results as TrendResult[]+ } catch {+ breakpoint()+ // Without the totals the table leaves out the share, so a failure needs no error.+ return null+ } finally {+ finished = true+ }+ breakpoint()+ return {+ query,+ rows: results.map((result) => ({+ order: getResultOrder(result),+ compare_label: result.compare_label,+ days: result.days,+ data: result.data,+ })),+ }+ },+ },+ ],+ })),+reducers({pinnedBreakdownColumns: [[] as string[],@@ -394,7 +606,7 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([}},],- showPercentOfDayTotal: [+ dayShareFits: [(s) => [s.isTrends, s.isNonTimeSeriesDisplay, s.series, s.breakdownFilter, s.querySource],(isTrends: boolean,@@ -406,19 +618,88 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([isTrends &&!isNonTimeSeriesDisplay &&!!series?.every(- (entity) => (!entity.math || DAY_ADDITIVE_MATHS.has(entity.math)) && !isSessionPropertySum(entity)+ (entity) =>+ (!entity.math || DAY_ADDITIVE_MATHS.has(entity.math) || UNIQUE_COUNT_MATHS.has(entity.math)) &&+ !isSessionPropertySum(entity)) &&breakdownSplitsAWhole(breakdownFilter) &&!hasFormulaResults(querySource),],+ uniqueCountSeriesOrders: [+ (s) => [s.series],+ (series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined) =>+ (series ?? []).flatMap((entity, index) =>+ entity.math && UNIQUE_COUNT_MATHS.has(entity.math) ? [index] : []+ ),+ ],+ // The same unique-count series without the breakdown, so each day counts a person once.+ uniqueDayTotalsQuery: [+ (s) => [s.dayShareFits, s.querySource, s.uniqueCountSeriesOrders],+ (+ dayShareFits: boolean,+ querySource: InsightQueryNode | null,+ uniqueCountSeriesOrders: number[]+ ): TrendsQuery | null => {+ if (!dayShareFits || uniqueCountSeriesOrders.length === 0) {+ return null+ }+ const trendsQuery = querySource as TrendsQuery+ return {+ ...trendsQuery,+ series: uniqueCountSeriesOrders.map((index) => trendsQuery.series[index]),+ breakdownFilter: undefined,+ }+ },+ ],+ // Keyed like the table rows, so a series moved in the query still gets its own totals.+ currentUniqueDayTotals: [+ (s) => [s.uniqueDayTotals, s.uniqueDayTotalsQuery, s.uniqueCountSeriesOrders],+ (+ uniqueDayTotals: UniqueDayTotals | null,+ uniqueDayTotalsQuery: TrendsQuery | null,+ uniqueCountSeriesOrders: number[]+ ): Map<string, Map<string, number>> | null => {+ if (!uniqueDayTotals || !isSameTotalsQuery(uniqueDayTotals.query, uniqueDayTotalsQuery)) {+ return null+ }+ return new Map(+ uniqueDayTotals.rows.map((row) => [+ percentOfTotalGroupKey({+ order: uniqueCountSeriesOrders[row.order],+ compare_label: row.compare_label,+ }),+ new Map(row.days.map((day, index) => [day, row.data[index]])),+ ])+ )+ },+ ],+ showPercentOfDayTotal: [+ (s) => [s.dayShareFits, s.series, s.uniqueCountSeriesOrders, s.currentUniqueDayTotals],+ (+ dayShareFits: boolean,+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,+ uniqueCountSeriesOrders: number[],+ currentUniqueDayTotals: Map<string, Map<string, number>> | null+ ): boolean =>+ dayShareFits && (uniqueCountSeriesOrders.length < (series?.length ?? 0) || !!currentUniqueDayTotals),+ ],getPercentOfDayTotal: [- (s) => [s.indexedResults, s.showPercentOfDayTotal],- (indexedResults: IndexedTrendResult[], showPercentOfDayTotal: boolean) => {+ (s) => [s.indexedResults, s.showPercentOfDayTotal, s.uniqueCountSeriesOrders, s.currentUniqueDayTotals],+ (+ indexedResults: IndexedTrendResult[],+ showPercentOfDayTotal: boolean,+ uniqueCountSeriesOrders: number[],+ currentUniqueDayTotals: Map<string, Map<string, number>> | null+ ) => {if (!showPercentOfDayTotal) {return (): number | null => null}+ const uniqueOrders = new Set(uniqueCountSeriesOrders)const totals = new Map<string, number[]>()for (const result of indexedResults) {+ if (uniqueOrders.has(getResultOrder(result))) {+ continue+ }const key = percentOfTotalGroupKey(result)const dayTotals = totals.get(key) ?? []result.data?.forEach((value, index) => {@@ -427,11 +708,39 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([totals.set(key, dayTotals)}return (item: IndexedTrendResult, index: number): number | null => {- const total = totals.get(percentOfTotalGroupKey(item))?.[index]+ const key = percentOfTotalGroupKey(item)const value = item.data?.[index]+ let total: number | undefined+ if (uniqueOrders.has(getResultOrder(item))) {+ // "Other" adds up the distinct counts of the values it groups, so it has no share of the day.+ if (isOtherBreakdownValue(item.breakdown_value)) {+ return null+ }+ // With "All time" the totals can start on a later day, so match them by date.+ const day = item.days?.[index]+ total = day === undefined ? undefined : currentUniqueDayTotals?.get(key)?.get(day)+ } else {+ total = totals.get(key)?.[index]+ }return total && value != null ? value / total : null}},],}),++ listeners(({ actions, cache }) => ({+ loadData: ({ refresh }) => {+ cache.forceUniqueDayTotals = refresh === 'force_blocking' || refresh === 'force_async'+ },+ loadDataSuccess: () => {+ actions.loadUniqueDayTotals(cache.forceUniqueDayTotals ? 'force_blocking' : 'blocking')+ cache.forceUniqueDayTotals = false+ },+ })),++ afterMount(({ actions, values }) => {+ if (!values.insightDataLoading) {+ actions.loadUniqueDayTotals('blocking')+ }+ }),])