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.

feat(product-analytics): show percent of total in the insights table (#124099)

Sam Penningtonc0b58a1acebe
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 373fcb1b15a3..4f3108d7213d 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -4065,33 +4065,33 @@ snapshots:
insights-insightstable--aggregation--light:
hash: v1.k794b7964.7db9a62e008e70801ec3b22495ada93d6729189b91d9f8c7c69ed75ecce373d1.6kCZQqFF_pSP08WFMu2HEaakUnOmnu3QGERn3qFPsj0
insights-insightstable--compare-previous--dark:
- hash: v1.k794b7964.41fb69aabc8a918c58f693db54ee71abf53115782820dd01a1a26f6929dbe714.ACZOSkxt8vlxz5dr0DPoe7koXbe7aUxWeHcU4zMhv84
+ hash: v1.k794b7964.22f529caf9a60f583b8fa21ed1c9713c1b3eb2f4347973203fcfba8e064c7ca8._-HE4IVTh1yYMqouKRQA9HUpUJMPqNdHEFHIC2suVBY
insights-insightstable--compare-previous--light:
- hash: v1.k794b7964.28fa002817466cdc38906e70bc568a02c9d4080d4e696ec4013a2a538907b56d.yFhGtvxHTdLkBaBTudbBvH9CQbI3uANS3b2ycEIeKRY
+ hash: v1.k794b7964.9ca1fcb3e04c307a8136f5dc633c2155163b69b5ed73d61cbf30775330e47be8.fNVPi4SpIc31Wd201P1QfLdILy7YFTFKSI9lZV9L7AU
insights-insightstable--compare-previous-detailed-results--dark:
- hash: v1.k794b7964.c99318f81cdc4a1e9d7dae8dba5d0727a84aa19c41036114fe8a395922db110b.tjGLu_ipVC3nNz-pebdemSE04ce-00kEzGbs7lRfKSo
+ hash: v1.k794b7964.8d4cdfb3c983c49b52916ded5cf36f7f115ce96e4a35c4a84547b54a48d92124.iZ3dj1oHtKDEbldyNcJrTASv3UBXO2Gi8RJFQI_orA0
insights-insightstable--compare-previous-detailed-results--light:
- hash: v1.k794b7964.3981b9428cb05f4f8eab902d95a0900cb9a724309b124db04091a84ae2c5e90a.cL_ZiS6mBEh0i59RySIuPM6oT0zETFIc1u4glGzvzjI
+ hash: v1.k794b7964.9f1156e326bde31b89853985e4d87cb12cd742a9e7c46a1e3ca6bc449528364b.1FNWJd0P4VXz67tlIWqB-9n3DpFvFFRXS1DkHkIECUU
insights-insightstable--default--dark:
- hash: v1.k794b7964.585aa3b95a71f83db72c958f68b5b9b28c05047625904238a18d840fd4abe38b.kbYrG-uIZ1A_25STyJcDiz6Ph7-N7rkj2HWE6g6eoy8
+ hash: v1.k794b7964.4c66edc788aa1066cefdc0b5120fc9ffdbb7052101c64cbb1b8c23381c1bf116.K8HtXC_o0YhaWGH3gZ2A7h043xsldiCkwKZ20Tj0HvY
insights-insightstable--default--light:
- hash: v1.k794b7964.c66c9a6962e38158249a400d2ae4720c486fba618e6667dfc3349d29fe6e1a16.KXTd_9veaErcx9P8AjQZntJUmnMbLJ3dJ25DsCEaUmM
+ hash: v1.k794b7964.cadd5102474fe995333120c12b7f9d3e31990c91d6944f38d75a3c01c5f045e0.7U9SvhM3YBib-mF1SSFnL2debkaxeDb9MpeO3XZ-iS0
insights-insightstable--embedded--dark:
- hash: v1.k794b7964.7cb21e010bcdae794bbc2d2459f8cfdaf22769d3285eb427579a443f75929721.N_f__JXKdstDMOj7auPECMx7KY1aJHvGkU_DDhx3hLs
+ hash: v1.k794b7964.c081867f39408550a0c6c5a9ce0e5e33a87559f43451e99e926b5c4db997b7b9.tMJ_33N19rYkqoHs27726aOyqbFgToOR69Kp3qndVWo
insights-insightstable--embedded--light:
- hash: v1.k794b7964.95560d073460ef933a4d97c323e908f60717fe457e48348eb77b848482e68345.a8i8j2KLmaAM0a2291p9Fk88Scx4sUCiKa92QfLo3cs
+ hash: v1.k794b7964.25644e4b7148a779a0702b27c3b43b1ad0cd2cbfcd371fad948b690518e0816b.8suBO83qrRI-4hcdEVGltXafPGhbuumasmjYxTLGGUE
insights-insightstable--hourly--dark:
- hash: v1.k794b7964.55eae8e120222588bf182dc25a16a055fbf7fc66edb30207e231230aa4c2ef15.g9XKrGQkwvs3FtldMGVq5q0Kmh2ONP7aSA8cbL7o2k0
+ hash: v1.k794b7964.fa7a6e958ec02a1402b717714a605f01093379c592aa2013b71e4fdf1635b7af.Dk8_MHUBydZbuFFuuuJS4RSvgDhWbzbvm_3isEjkm4k
insights-insightstable--hourly--light:
- hash: v1.k794b7964.946b7f2f2b0a5676da3d36ebb24a7a6149165604d4e26f9008c43d7316eda576.L95EbdtD_LaCexLALTqDHlf5itNSHYsswomU7vtRMtA
+ hash: v1.k794b7964.d9eba5186d7ee5a4f34c2c1120af028b0f02cfb5923a3eac285eba7ab99b75e8.TITGlya_VGsdMrx1FnRgiUwp8Iky2NqLQXwmSbxWWHY
insights-insightstable--is-legend--dark:
- hash: v1.k794b7964.8149c232d7b3c8ee1abb048003a5c9434dba8186bc728da5da4b9af2dca16cd0.NQh23-oNGsHd14_4QI2tUt0IRbdnHKUpmgDBuo-1x1A
+ hash: v1.k794b7964.1b1d2db63f64e218f68e42a70b36f93694c31e2ca9b39ca8b818043b40524107.rHTluxZvxNQvT79LsNPpi43dqTlFpQZg1wpObOXa5dQ
insights-insightstable--is-legend--light:
- hash: v1.k794b7964.d8663c41468bfaac7bff46419f17906bb57a3ea4a92036d3f2901f2bd5a645ef.mj8iZ-ckvyoJVUPIB6QGatB5oqSb2S5aLPbYuCi9nb0
+ hash: v1.k794b7964.b4f4923f10416a763d90bf9eea7bfe956b24bfd5776920202690e745187170c2.owPjJ3WhwlByINmkVGNzDx7efDTyMswRp35c5j8jLjI
insights-insightstable--sql-breakdown--dark:
- hash: v1.k794b7964.cc64ca6ab10ef0f936dd479fb6a76716fd5935b3dee59833377c03cb3e36569a.kVka0mzCaVBMto0uHpEx5cTwdVDK0wlESIquSCaBrjk
+ hash: v1.k794b7964.9c7d0f05746b63c3fd129cc80ef95b2fadf08ae15f7819ad93e7233beaf55021.QpiqP_LMSIW8EWoJO0mPzkFGTQ8bK6xkRn32UEwhnVk
insights-insightstable--sql-breakdown--light:
- hash: v1.k794b7964.710a5fa22da7eff24e97e754426977214d9cdb9fd0252415534110bc7d9bbf95.EmqhUt-YOAWJV1nbFZ2BA-Let9QcaeDgslu9bcPR_mE
+ hash: v1.k794b7964.19c090e0257cddc72331dfe9907cbe21ce11a3018884b4d2c0ce998b48fe5b7d.D_8cVVy21pUHLayENWZPgnU7Z_cX7fvXmKIsfUzMuJg
insights-journeys-journeygrid--anchored--dark:
hash: v1.k794b7964.d960a6965fe0b701f77dc4cf3f1c668aa6c19f5369e5e4aaa5423a7158474deb.DVpy9fBOuJdH6MLVhfwTvQ1o2va4L7JaUwIpTqcGkRA
insights-journeys-journeygrid--anchored--light:
@@ -13085,9 +13085,9 @@ snapshots:
scenes-app-insights-loading-messages--loading-messages--light:
hash: v1.k794b7964.8127887a8cefedcd7ed93385885d6847022c7fa290f01ba60c42deb1216aa9b8.7DnuefE-HifUz5vzz32GzJAmV6eG8Or-yMF3OpaDuw8
scenes-app-insights-mcp-missing-capability--mcp-missing-capability--dark:
- hash: v1.k794b7964.dfd3cfa4c22429f905144ad610de438cd4685b2f160970762ee53a0c57b51f7c.36ljh4-ECCoIoGsxSqYp-Bl_q4ndSo-sORGvmxkm75A
+ hash: v1.k794b7964.9f5eda4c107498216eb3decaca2c56625c3494f3db8d925fa42d39eefbfb4745._x5perRns6amXlURnxpF1S8HqyJOrpcNgMqPOPBcQB0
scenes-app-insights-mcp-missing-capability--mcp-missing-capability--light:
- hash: v1.k794b7964.1b1a18bc74d3513095cfeb8a172f426cbda4d1b9b5df5ba59479d8ceb2399535.1TyzFZmRmUYsqc9Q5dnyZQtCF7gppER5cdCkVWMsNdc
+ hash: v1.k794b7964.6e5be6f46d2fd0433e596d454c118213944f1f14a63b0cb151c9151639908b9e.Y9KbfoiPi_QedePTdXM2ilVy-trbGRQTN6wlmX6JHHU
scenes-app-insights-posthog-ai-suggestion--default--dark:
hash: v1.k794b7964.9b9cc1445c7ce703d3b0dbb3d4a92be579932829994f414f1de80f8016ea1154.jb54m6G-4sIokLRkpEnOPhJu-hxlxtfJcQ7ri7sI3WY
scenes-app-insights-posthog-ai-suggestion--default--light:
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/InsightsTable.tsx b/products/product_analytics/frontend/insights/shared/InsightsTable/InsightsTable.tsx
index 1a0b22d02427..0be5e5976361 100644
--- a/products/product_analytics/frontend/insights/shared/InsightsTable/InsightsTable.tsx
+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/InsightsTable.tsx
@@ -7,6 +7,7 @@ import { ReactNode, useCallback, useMemo, useState } from 'react'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonTable, LemonTableColumn } from 'lib/lemon-ui/LemonTable'
import { COUNTRY_CODE_TO_LONG_NAME } from 'lib/utils/country'
+import { percentage } from 'lib/utils/numbers'
import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'
import { entityFilterLogic } from 'scenes/insights/filters/ActionFilter/entityFilterLogic'
import { insightLogic } from 'scenes/insights/insightLogic'
@@ -24,19 +25,14 @@ import { getResultSeriesName } from 'products/product_analytics/frontend/insight
import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'
import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'
-import {
- AggregationColumnItem,
- AggregationColumnTitle,
- CALC_COLUMN_LABELS,
- getAggregatedValue,
-} from './columns/AggregationColumn'
+import { AggregationColumnItem, AggregationColumnTitle, CALC_COLUMN_LABELS } from './columns/AggregationColumn'
import { BreakdownColumnItem, BreakdownColumnTitle, MultipleBreakdownColumnTitle } from './columns/BreakdownColumn'
import { ColorCustomizationColumnItem, ColorCustomizationColumnTitle } from './columns/ColorCustomizationColumn'
import { SeriesCheckColumnItem, SeriesCheckColumnTitle } from './columns/SeriesCheckColumn'
import { SeriesColumnItem, formatCompareLabel } from './columns/SeriesColumn'
import { ValueColumnItem, ValueColumnTitle } from './columns/ValueColumn'
import { WorldMapColumnItem, WorldMapColumnTitle } from './columns/WorldMapColumn'
-import { AggregationType, insightsTableDataLogic } from './insightsTableDataLogic'
+import { AggregationType, getAggregatedValue, insightsTableDataLogic } from './insightsTableDataLogic'
export type CalcColumnState = 'total' | 'average' | 'median'
@@ -104,8 +100,18 @@ export function InsightsTable({
querySource,
} = useValues(trendsDataLogic(insightProps))
const { toggleResultHidden, toggleAllResultsHidden } = useActions(trendsDataLogic(insightProps))
- const { aggregation, allowAggregation, pinnedColumns, isColumnPinned, getPreviousResult, displayResults } =
- useValues(insightsTableDataLogic(insightProps))
+ const {
+ aggregation,
+ allowAggregation,
+ pinnedColumns,
+ isColumnPinned,
+ getPreviousResult,
+ displayResults,
+ showPercentOfTotal,
+ getPercentOfTotal,
+ showPercentOfDayTotal,
+ getPercentOfDayTotal,
+ } = useValues(insightsTableDataLogic(insightProps))
const { setDetailedResultsAggregationType, toggleColumnPin } = useActions(insightsTableDataLogic(insightProps))
const { weekStartDay, timezone, baseCurrency } = useValues(teamLogic)
const [maxVisibleColumns, setMaxVisibleColumns] = useState(MAX_VALUE_COLUMNS)
@@ -377,6 +383,19 @@ export function InsightsTable({
align: 'right',
})
+ if (showPercentOfTotal) {
+ columns.push({
+ title: <span>% of total</span>,
+ render: (_: any, item: IndexedTrendResult) => {
+ const share = getPercentOfTotal(item)
+ return <span>{share === null ? '—' : percentage(share, 1)}</span>
+ },
+ sorter: (a, b) => (getPercentOfTotal(a) ?? 0) - (getPercentOfTotal(b) ?? 0),
+ key: 'percent-of-total',
+ align: 'right',
+ })
+ }
+
if (isCompareTable) {
columns.push({
title: <span>Previous {CALC_COLUMN_LABELS[aggregation]}</span>,
@@ -469,6 +488,7 @@ export function InsightsTable({
isStickiness={isStickiness}
renderCount={renderCount}
formatPropertyValueForDisplay={formatPropertyValueForDisplay}
+ percentOfDayTotal={showPercentOfDayTotal ? getPercentOfDayTotal(item, index) : null}
/>
),
key: `data-${index}`,
@@ -484,6 +504,8 @@ export function InsightsTable({
compareFilter?.compare,
interval,
maxVisibleColumns,
+ showPercentOfDayTotal,
+ getPercentOfDayTotal,
]) // oxlint-disable-line react-hooks/exhaustive-deps
const totalValueColumns = indexedResults?.[0]?.data?.length ?? 0
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/columns/AggregationColumn.tsx b/products/product_analytics/frontend/insights/shared/InsightsTable/columns/AggregationColumn.tsx
index 23e436e87995..19d57b4b2ae1 100644
--- a/products/product_analytics/frontend/insights/shared/InsightsTable/columns/AggregationColumn.tsx
+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/columns/AggregationColumn.tsx
@@ -7,7 +7,6 @@ import { IconChevronDown } from '@posthog/icons'
import { LemonMenu, LemonMenuItem } from '@posthog/lemon-ui'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
-import { average, median } from 'lib/utils/numbers'
import { formatAggregationAxisValue } from 'scenes/insights/aggregationAxisFormat'
import { formatAggregationValue } from 'scenes/insights/utils'
import { teamLogic } from 'scenes/teamLogic'
@@ -19,6 +18,7 @@ import { TrendsFilterType } from '~/types'
import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'
import { CalcColumnState } from '../InsightsTable'
+import { getAggregatedValue } from '../insightsTableDataLogic'
export const CALC_COLUMN_LABELS: Record<CalcColumnState, string> = {
total: 'Total Sum',
@@ -74,25 +74,6 @@ type AggregationColumnItemProps = {
trendsFilter: TrendsFilter | null | undefined
}
-export function getAggregatedValue(
- item: IndexedTrendResult,
- aggregation: CalcColumnState,
- isNonTimeSeriesDisplay: boolean
-): number | undefined {
- if (aggregation === 'total' || isNonTimeSeriesDisplay) {
- let value = item.count ?? item.aggregated_value
- if (item.aggregated_value > item.count || (item.aggregated_value < 0 && item.aggregated_value < item.count)) {
- value = item.aggregated_value
- }
- return value
- } else if (aggregation === 'average') {
- return average(item.data)
- } else if (aggregation === 'median') {
- return median(item.data)
- }
- return undefined
-}
-
export function AggregationColumnItem({
item,
mathProperty,
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/columns/ValueColumn.tsx b/products/product_analytics/frontend/insights/shared/InsightsTable/columns/ValueColumn.tsx
index df5afb5d7292..267bb17b1a5a 100644
--- a/products/product_analytics/frontend/insights/shared/InsightsTable/columns/ValueColumn.tsx
+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/columns/ValueColumn.tsx
@@ -1,6 +1,7 @@
import { ReactNode, memo } from 'react'
import { DateDisplay } from 'lib/components/DateDisplay'
+import { percentage } from 'lib/utils/numbers'
import { formatAggregationValue } from 'scenes/insights/utils'
import { FormatPropertyValueForDisplayFunction } from '~/models/propertyDefinitionsModel'
@@ -50,6 +51,7 @@ type ValueColumnItemProps = {
isStickiness: boolean
renderCount: (value: number) => ReactNode
formatPropertyValueForDisplay: FormatPropertyValueForDisplayFunction
+ percentOfDayTotal?: number | null
}
export const ValueColumnItem = memo(function ValueColumnItem({
@@ -59,6 +61,7 @@ export const ValueColumnItem = memo(function ValueColumnItem({
isStickiness,
renderCount,
formatPropertyValueForDisplay,
+ percentOfDayTotal,
}: ValueColumnItemProps): JSX.Element {
const formattedValue = formatAggregationValue(
mathProperty,
@@ -66,6 +69,14 @@ export const ValueColumnItem = memo(function ValueColumnItem({
renderCount,
formatPropertyValueForDisplay
)
+ if (percentOfDayTotal != null) {
+ return (
+ <span className="whitespace-nowrap">
+ <span>{formattedValue}</span>
+ <span className="text-secondary ml-1">({percentage(percentOfDayTotal, 1)})</span>
+ </span>
+ )
+ }
return (
<span>
{isStickiness ? (
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts
index c40c54523eaa..182b6d7a6f3f 100644
--- a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts
+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.test.ts
@@ -95,6 +95,99 @@ describe('insightsTableDataLogic', () => {
})
})
+ it.each([
+ ['a breakdown of total counts', {}, true, true],
+ ['no breakdown', { breakdownFilter: undefined }, false, false],
+ [
+ 'a cohort breakdown',
+ { breakdownFilter: { breakdown: [1, 'all'], breakdown_type: 'cohort' } },
+ false,
+ false,
+ ],
+ [
+ 'the "Other" bucket hidden',
+ {
+ breakdownFilter: {
+ breakdown: '$browser',
+ breakdown_type: 'event',
+ breakdown_hide_other_aggregation: true,
+ },
+ },
+ false,
+ false,
+ ],
+ [
+ 'unique users',
+ { series: [{ kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.UniqueUsers }] },
+ false,
+ false,
+ ],
+ [
+ 'an average of a property',
+ {
+ series: [
+ {
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
+ math: PropertyMathType.Average,
+ math_property: '$screen_width',
+ },
+ ],
+ },
+ false,
+ false,
+ ],
+ [
+ 'a sum of session duration',
+ {
+ series: [
+ {
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
+ math: PropertyMathType.Sum,
+ math_property: '$session_duration',
+ },
+ ],
+ },
+ false,
+ false,
+ ],
+ [
+ 'a sum of a session property',
+ {
+ series: [
+ {
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
+ math: PropertyMathType.Sum,
+ math_property: '$pageview_count',
+ math_property_type: 'session_properties',
+ },
+ ],
+ },
+ false,
+ false,
+ ],
+ ['a formula', { trendsFilter: { formulaNodes: [{ formula: 'A * 2' }] } }, false, false],
+ ] as [string, Partial<TrendsQuery>, boolean, boolean][])(
+ 'percent of total columns with %s',
+ async (_name, overrides, expectedTotal, expectedDay) => {
+ const query: TrendsQuery = {
+ kind: NodeKind.TrendsQuery,
+ series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
+ breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },
+ ...overrides,
+ }
+
+ insightVizDataLogic(props).actions.updateQuerySource(query)
+
+ await expectLogic(logic).toMatchValues({
+ showPercentOfTotal: expectedTotal,
+ showPercentOfDayTotal: expectedDay,
+ })
+ }
+ )
+
describe('aggregation', () => {
it('by default averages for insights with unique math entity', async () => {
const query: TrendsQuery = {
@@ -357,5 +450,69 @@ describe('insightsTableDataLogic', () => {
expect(previousItem!.compare_label).toBe('previous')
expect(previousItem!.count).toBe(80)
})
+
+ it('getPercentOfDayTotal handles results without daily data', async () => {
+ const insight: Partial<InsightModel> = {
+ result: [makeCompareResult({ data: null as any })],
+ }
+
+ await expectLogic(logic, () => {
+ builtDataNodeLogic.actions.loadDataSuccess(insight)
+ }).toFinishAllListeners()
+
+ expect(logic.values.getPercentOfDayTotal(logic.values.indexedResults[0], 0)).toBeNull()
+ })
+
+ it('percent of total divides by the total of the same series and period', async () => {
+ const row = (
+ order: number,
+ compare_label: CompareLabelType,
+ breakdown_value: string,
+ data: number[]
+ ): Partial<IndexedTrendResult> =>
+ makeCompareResult({
+ action: { order, id: '$pageview', name: '$pageview' } as any,
+ order,
+ compare: true,
+ compare_label,
+ breakdown_value,
+ data,
+ count: data.reduce((a, b) => a + b, 0),
+ })
+ const insight: Partial<InsightModel> = {
+ result: [
+ row(0, CompareLabelType.Current, 'Chrome', [10, 20]),
+ row(0, CompareLabelType.Current, 'Firefox', [10, 0]),
+ row(0, CompareLabelType.Previous, 'Chrome', [5, 0]),
+ row(0, CompareLabelType.Previous, 'Firefox', [5, 10]),
+ row(1, CompareLabelType.Current, 'Chrome', [50, 0]),
+ ],
+ }
+
+ const query: TrendsQuery = {
+ kind: NodeKind.TrendsQuery,
+ series: [
+ { kind: NodeKind.EventsNode, event: '$pageview' },
+ { kind: NodeKind.EventsNode, event: '$pageview' },
+ ],
+ breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },
+ compareFilter: { compare: true },
+ }
+
+ await expectLogic(logic, () => {
+ insightVizDataLogic.findMounted(props)?.actions.updateQuerySource(query)
+ builtDataNodeLogic.actions.loadDataSuccess(insight)
+ }).toFinishAllListeners()
+
+ const { indexedResults, getPercentOfTotal, getPercentOfDayTotal } = logic.values
+ expect(indexedResults.map(getPercentOfTotal)).toEqual([0.75, 0.25, 0.25, 0.75, 1])
+ expect(indexedResults.map((r) => [getPercentOfDayTotal(r, 0), getPercentOfDayTotal(r, 1)])).toEqual([
+ [0.5, 1],
+ [0.5, 0],
+ [0.5, 0],
+ [0.5, 1],
+ [1, null],
+ ])
+ })
})
})
diff --git a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts
index 7755fa5ca16c..e5ed372a2490 100644
--- a/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts
+++ b/products/product_analytics/frontend/insights/shared/InsightsTable/insightsTableDataLogic.ts
@@ -1,16 +1,30 @@
import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
+import { average, median } from 'lib/utils/numbers'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
-import { getResultOrder } from 'scenes/insights/utils/resultSeries'
+import { getResultOrder, hasFormulaResults } from 'scenes/insights/utils/resultSeries'
import type {
AnyDataWarehouseNode,
AnyEntityNode,
+ BreakdownFilter,
CompareFilter,
DataWarehouseNode,
+ EntityNode,
+ FunnelsQuery,
GroupNode,
+ InsightQueryNode,
+ LifecycleQuery,
+ PathsQuery,
+ PathsV2Query,
+ RetentionQuery,
+ StickinessQuery,
+ TrendsQuery,
+ WebOverviewQuery,
+ WebStatsTableQuery,
} from '~/queries/schema/schema-general'
+import { hasBreakdownFilter } from '~/queries/utils'
import { ChartDisplayType, CompareLabelType, InsightLogicProps } from '~/types'
import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'
@@ -22,26 +36,93 @@ export enum AggregationType {
Median = 'median',
}
+export function getAggregatedValue(
+ item: IndexedTrendResult,
+ aggregation: `${AggregationType}`,
+ isNonTimeSeriesDisplay: boolean
+): number | undefined {
+ if (aggregation === 'total' || isNonTimeSeriesDisplay) {
+ let value = item.count ?? item.aggregated_value
+ if (item.aggregated_value > item.count || (item.aggregated_value < 0 && item.aggregated_value < item.count)) {
+ value = item.aggregated_value
+ }
+ return value
+ } else if (aggregation === 'average') {
+ return average(item.data)
+ } else if (aggregation === 'median') {
+ return median(item.data)
+ }
+ return undefined
+}
+
export function compareResultKey(item: IndexedTrendResult): string {
return JSON.stringify([getResultOrder(item), item.label ?? '', item.breakdown_value ?? ''])
}
+// Each event lands in exactly one breakdown value. Unique counts don't: one person can be in several rows.
+const DAY_ADDITIVE_MATHS = new Set<string>([
+ 'total',
+ 'sum',
+ 'first_time_for_user',
+ 'first_matching_event_for_user',
+ 'first_time_for_group',
+ 'first_matching_event_for_group',
+])
+
+// Cohorts overlap, and a hidden "Other" bucket drops part of the whole from the results.
+function breakdownSplitsAWhole(breakdownFilter: BreakdownFilter | null | undefined): boolean {
+ return (
+ hasBreakdownFilter(breakdownFilter) &&
+ breakdownFilter.breakdown_type !== 'cohort' &&
+ !breakdownFilter.breakdown_hide_other_aggregation
+ )
+}
+
+// The query sums once per session and breakdown value, so a session lands in every value it touched.
+function isSessionPropertySum(entity: EntityNode): boolean {
+ return (
+ entity.math === 'sum' &&
+ (entity.math_property === '$session_duration' || entity.math_property_type === 'session_properties')
+ )
+}
+
+function percentOfTotalGroupKey(item: IndexedTrendResult): string {
+ return JSON.stringify([getResultOrder(item), item.compare_label ?? ''])
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightsTableDataLogicValues {
display: ChartDisplayType | null | undefined // insightVizDataLogic
isTrends: boolean // insightVizDataLogic
persistedAggregationType: AggregationType | undefined // insightVizDataLogic
series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined // insightVizDataLogic
+ breakdownFilter: BreakdownFilter | null | undefined // trendsDataLogic
compareFilter: CompareFilter | null | undefined // trendsDataLogic
indexedResults: IndexedTrendResult[] // trendsDataLogic
+ isNonTimeSeriesDisplay: boolean // trendsDataLogic
+ querySource:
+ | FunnelsQuery
+ | LifecycleQuery
+ | PathsQuery
+ | PathsV2Query
+ | RetentionQuery
+ | StickinessQuery
+ | TrendsQuery
+ | WebOverviewQuery
+ | WebStatsTableQuery
+ | null // trendsDataLogic
aggregation: AggregationType
allowAggregation: boolean
displayResults: IndexedTrendResult[]
+ getPercentOfDayTotal: (item: IndexedTrendResult, index: number) => number | null
+ getPercentOfTotal: (item: IndexedTrendResult) => number | null
getPreviousResult: (item: IndexedTrendResult) => IndexedTrendResult | undefined
isColumnPinned: (columnKey: string) => boolean
pinnedBreakdownColumns: string[]
pinnedColumns: string[]
previousResultMap: Map<string, IndexedTrendResult>
+ showPercentOfDayTotal: boolean
+ showPercentOfTotal: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -81,6 +162,49 @@ export interface insightsTableDataLogicMeta {
compareFilter: CompareFilter | null | undefined,
indexedResults: IndexedTrendResult[]
) => IndexedTrendResult[]
+ showPercentOfTotal: (
+ allowAggregation: boolean,
+ aggregation: AggregationType,
+ isNonTimeSeriesDisplay: boolean,
+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,
+ breakdownFilter: BreakdownFilter | null | undefined,
+ querySource:
+ | FunnelsQuery
+ | LifecycleQuery
+ | PathsQuery
+ | PathsV2Query
+ | RetentionQuery
+ | StickinessQuery
+ | TrendsQuery
+ | WebOverviewQuery
+ | WebStatsTableQuery
+ | null
+ ) => boolean
+ getPercentOfTotal: (
+ indexedResults: IndexedTrendResult[],
+ isNonTimeSeriesDisplay: boolean
+ ) => (item: IndexedTrendResult) => number | null
+ showPercentOfDayTotal: (
+ isTrends: boolean,
+ isNonTimeSeriesDisplay: boolean,
+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,
+ breakdownFilter: BreakdownFilter | null | undefined,
+ querySource:
+ | FunnelsQuery
+ | LifecycleQuery
+ | PathsQuery
+ | PathsV2Query
+ | RetentionQuery
+ | StickinessQuery
+ | TrendsQuery
+ | WebOverviewQuery
+ | WebStatsTableQuery
+ | null
+ ) => boolean
+ getPercentOfDayTotal: (
+ indexedResults: IndexedTrendResult[],
+ showPercentOfDayTotal: boolean
+ ) => (item: IndexedTrendResult, index: number) => number | null
}
}
@@ -101,7 +225,7 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([
insightVizDataLogic(props),
['isTrends', 'display', 'series', 'detailedResultsAggregationType as persistedAggregationType'],
trendsDataLogic(props),
- ['indexedResults', 'compareFilter'],
+ ['indexedResults', 'compareFilter', 'breakdownFilter', 'isNonTimeSeriesDisplay', 'querySource'],
],
actions: [insightVizDataLogic(props), ['setDetailedResultsAggregationType']],
})),
@@ -225,5 +349,89 @@ export const insightsTableDataLogic = kea<insightsTableDataLogicType>([
return indexedResults
},
],
+ // A share needs rows that sum to a whole: an additive math split by a breakdown, and no formula.
+ showPercentOfTotal: [
+ (s) => [
+ s.allowAggregation,
+ s.aggregation,
+ s.isNonTimeSeriesDisplay,
+ s.series,
+ s.breakdownFilter,
+ s.querySource,
+ ],
+ (
+ allowAggregation: boolean,
+ aggregation: AggregationType,
+ isNonTimeSeriesDisplay: boolean,
+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,
+ breakdownFilter: BreakdownFilter | null | undefined,
+ querySource: InsightQueryNode | null
+ ): boolean =>
+ allowAggregation &&
+ (isNonTimeSeriesDisplay || aggregation === AggregationType.Total) &&
+ !!series?.every(
+ (entity) =>
+ (!entity.math || entity.math === 'total' || entity.math === 'sum') &&
+ !isSessionPropertySum(entity)
+ ) &&
+ breakdownSplitsAWhole(breakdownFilter) &&
+ !hasFormulaResults(querySource),
+ ],
+ // Totals include hidden rows and "Other", so hiding one doesn't change the others' share.
+ getPercentOfTotal: [
+ (s) => [s.indexedResults, s.isNonTimeSeriesDisplay],
+ (indexedResults: IndexedTrendResult[], isNonTimeSeriesDisplay: boolean) => {
+ const totals = new Map<string, number>()
+ for (const result of indexedResults) {
+ const key = percentOfTotalGroupKey(result)
+ const value = getAggregatedValue(result, AggregationType.Total, isNonTimeSeriesDisplay) ?? 0
+ totals.set(key, (totals.get(key) ?? 0) + value)
+ }
+ return (item: IndexedTrendResult): number | null => {
+ const total = totals.get(percentOfTotalGroupKey(item))
+ const value = getAggregatedValue(item, AggregationType.Total, isNonTimeSeriesDisplay)
+ return total && value !== undefined ? value / total : null
+ }
+ },
+ ],
+ showPercentOfDayTotal: [
+ (s) => [s.isTrends, s.isNonTimeSeriesDisplay, s.series, s.breakdownFilter, s.querySource],
+ (
+ isTrends: boolean,
+ isNonTimeSeriesDisplay: boolean,
+ series: (AnyEntityNode<AnyDataWarehouseNode> | GroupNode<DataWarehouseNode>)[] | null | undefined,
+ breakdownFilter: BreakdownFilter | null | undefined,
+ querySource: InsightQueryNode | null
+ ): boolean =>
+ isTrends &&
+ !isNonTimeSeriesDisplay &&
+ !!series?.every(
+ (entity) => (!entity.math || DAY_ADDITIVE_MATHS.has(entity.math)) && !isSessionPropertySum(entity)
+ ) &&
+ breakdownSplitsAWhole(breakdownFilter) &&
+ !hasFormulaResults(querySource),
+ ],
+ getPercentOfDayTotal: [
+ (s) => [s.indexedResults, s.showPercentOfDayTotal],
+ (indexedResults: IndexedTrendResult[], showPercentOfDayTotal: boolean) => {
+ if (!showPercentOfDayTotal) {
+ return (): number | null => null
+ }
+ const totals = new Map<string, number[]>()
+ for (const result of indexedResults) {
+ const key = percentOfTotalGroupKey(result)
+ const dayTotals = totals.get(key) ?? []
+ result.data?.forEach((value, index) => {
+ dayTotals[index] = (dayTotals[index] ?? 0) + (value ?? 0)
+ })
+ totals.set(key, dayTotals)
+ }
+ return (item: IndexedTrendResult, index: number): number | null => {
+ const total = totals.get(percentOfTotalGroupKey(item))?.[index]
+ const value = item.data?.[index]
+ return total && value != null ? value / total : null
+ }
+ },
+ ],
}),
])