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(dashboards): add per-tile test account filtering override (#77361)

Thomas Obermüllerd5074635d156
diff --git a/frontend/src/scenes/dashboard/TileFiltersOverride.tsx b/frontend/src/scenes/dashboard/TileFiltersOverride.tsx
index 88bd475aea33..3b2114fbbde3 100644
--- a/frontend/src/scenes/dashboard/TileFiltersOverride.tsx
+++ b/frontend/src/scenes/dashboard/TileFiltersOverride.tsx
@@ -3,8 +3,8 @@ import './TileFiltersOverride.scss'
import { BindLogic, useActions, useValues } from 'kea'
-import { IconCalendar } from '@posthog/icons'
-import { LemonSelect, LemonSwitch } from '@posthog/lemon-ui'
+import { IconCalendar, IconGear } from '@posthog/icons'
+import { LemonButton, LemonDivider, LemonSegmentedButton, LemonSelect, LemonSwitch } from '@posthog/lemon-ui'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
@@ -12,6 +12,8 @@ import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { getProjectEventExistence } from 'lib/utils/getAppContext'
import { TaxonomicBreakdownFilter } from 'scenes/insights/filters/BreakdownFilter/TaxonomicBreakdownFilter'
import { insightLogic } from 'scenes/insights/insightLogic'
+import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
import { groupsModel } from '~/models/groupsModel'
import { BreakdownFilter, NodeKind } from '~/queries/schema/schema-general'
@@ -20,10 +22,26 @@ import type { DashboardTile, InsightLogicProps, IntervalType, QueryBasedInsightM
import { tileLogic } from './tileLogic'
+type TestAccountFilterChoice = 'inherit' | 'filter-out' | 'include'
+
+const CHOICE_TO_FILTER: Record<TestAccountFilterChoice, boolean | null> = {
+ inherit: null,
+ 'filter-out': true,
+ include: false,
+}
+
+const CHOICE_HINTS: Record<TestAccountFilterChoice, string> = {
+ inherit: "Uses the dashboard's setting, or the insight's own if the dashboard doesn't set one.",
+ 'filter-out': 'Internal and test users are filtered out of this insight.',
+ include: 'Internal and test users are included in this insight.',
+}
+
export function TileFiltersOverride({ tile }: { tile: DashboardTile<QueryBasedInsightModel> }): JSX.Element {
const { overrides } = useValues(tileLogic)
- const { setDates, setProperties, setBreakdown, setInterval, setIgnoreDashboardFilters } = useActions(tileLogic)
+ const { setDates, setProperties, setBreakdown, setInterval, setFilterTestAccounts, setIgnoreDashboardFilters } =
+ useActions(tileLogic)
const { groupsTaxonomicTypes } = useValues(groupsModel)
+ const { currentTeam } = useValues(teamLogic)
const { hasPageview, hasScreen } = getProjectEventExistence()
@@ -32,6 +50,11 @@ export function TileFiltersOverride({ tile }: { tile: DashboardTile<QueryBasedIn
const supportsInterval = isInsightQueryWithSeries(querySource ?? undefined)
const supportsBreakdown = isInsightQueryWithBreakdown(querySource)
+ const filterTestAccounts = overrides.filterTestAccounts ?? null
+ const testAccountChoice: TestAccountFilterChoice =
+ filterTestAccounts === null ? 'inherit' : filterTestAccounts ? 'filter-out' : 'include'
+ const hasTestAccountFilters = (currentTeam?.test_account_filters || []).length > 0
+
// The breakdown picker needs a mounted insightLogic. Bind a throwaway one, like DashboardEditBar,
// keyed per tile so it can't collide with the edit bar's `dashboardItemId: 'new'` binding.
const breakdownInsightProps: InsightLogicProps = {
@@ -51,117 +74,174 @@ export function TileFiltersOverride({ tile }: { tile: DashboardTile<QueryBasedIn
<div>
<p className="text-sm text-muted mb-4">
Set custom filters for this tile. Property filters apply on top of the dashboard's, while the tile's
- date range, interval, and breakdown replace the dashboard's.
+ date range, interval, breakdown, and test account filtering replace the dashboard's.
</p>
</div>
- <div className="space-y-4">
- <div>
- <LemonSwitch
- checked={!!overrides.ignoreDashboardFilters}
- onChange={setIgnoreDashboardFilters}
- label="Ignore dashboard filters"
- bordered
- fullWidth
- data-attr="tile-ignore-dashboard-filters"
- />
- <p className="text-xs text-muted mt-1 mb-0">
- When on, none of the dashboard's filters apply to this insight. The overrides below still do.
- </p>
+ <div>
+ <LemonDivider label="Scope" />
+ <div className="flex flex-col gap-4 pb-4">
+ <div>
+ <LemonSwitch
+ checked={!!overrides.ignoreDashboardFilters}
+ onChange={setIgnoreDashboardFilters}
+ label="Ignore dashboard filters"
+ bordered
+ fullWidth
+ data-attr="tile-ignore-dashboard-filters"
+ />
+ <p className="text-xs text-muted mt-1 mb-0">
+ When on, none of the dashboard's filters apply to this insight. The overrides below still
+ do.
+ </p>
+ </div>
</div>
- <div>
- <label className="text-sm font-medium mb-2 block">Date range</label>
- <DateFilter
- showCustom
- showExplicitDateToggle
- dateFrom={overrides.date_from ?? null}
- dateTo={overrides.date_to ?? null}
- explicitDate={overrides.explicitDate}
- onChange={(from, to, explicitDate) => setDates(from, to, explicitDate)}
- makeLabel={(key) => (
- <>
- <IconCalendar />
- <span className="hide-when-small"> {key}</span>
- </>
- )}
- />
- </div>
+ <LemonDivider label="Time" />
+ <div className="flex flex-col gap-4 pb-4">
+ <div>
+ <label className="text-sm font-medium mb-2 block">Date range</label>
+ <DateFilter
+ showCustom
+ showExplicitDateToggle
+ dateFrom={overrides.date_from ?? null}
+ dateTo={overrides.date_to ?? null}
+ explicitDate={overrides.explicitDate}
+ onChange={(from, to, explicitDate) => setDates(from, to, explicitDate)}
+ makeLabel={(key) => (
+ <>
+ <IconCalendar />
+ <span className="hide-when-small"> {key}</span>
+ </>
+ )}
+ />
+ </div>
- <div>
- <label className="text-sm font-medium mb-2 block">Interval</label>
- <LemonSelect<IntervalType | null>
- size="small"
- value={overrides.interval ?? null}
- dropdownMatchSelectWidth={false}
- disabledReason={
- supportsInterval ? undefined : "This insight type doesn't support an interval override"
- }
- onChange={(interval) => setInterval(interval)}
- options={[
- { value: null, label: 'inherit' },
- { value: 'hour', label: 'hour' },
- { value: 'day', label: 'day' },
- { value: 'week', label: 'week' },
- { value: 'month', label: 'month' },
- ]}
- data-attr="tile-override-interval"
- />
+ <div>
+ <label className="text-sm font-medium mb-2 block">Interval</label>
+ <LemonSelect<IntervalType | null>
+ size="small"
+ value={overrides.interval ?? null}
+ dropdownMatchSelectWidth={false}
+ disabledReason={
+ supportsInterval ? undefined : "This insight type doesn't support an interval override"
+ }
+ onChange={(interval) => setInterval(interval)}
+ options={[
+ { value: null, label: 'inherit' },
+ { value: 'hour', label: 'hour' },
+ { value: 'day', label: 'day' },
+ { value: 'week', label: 'week' },
+ { value: 'month', label: 'month' },
+ ]}
+ data-attr="tile-override-interval"
+ />
+ </div>
</div>
- <div>
- <label className="text-sm font-medium mb-2 block">Properties</label>
- <PropertyFilters
- onChange={(properties) => setProperties(properties)}
- pageKey={`tile_${tile.id}_properties`}
- propertyFilters={overrides.properties ?? []}
- taxonomicGroupTypes={[
- TaxonomicFilterGroupType.EventProperties,
- TaxonomicFilterGroupType.PersonProperties,
- TaxonomicFilterGroupType.EventFeatureFlags,
- TaxonomicFilterGroupType.EventMetadata,
- ...(hasPageview ? [TaxonomicFilterGroupType.PageviewUrls] : []),
- ...(hasScreen ? [TaxonomicFilterGroupType.Screens] : []),
- TaxonomicFilterGroupType.EmailAddresses,
- ...groupsTaxonomicTypes,
- TaxonomicFilterGroupType.Cohorts,
- TaxonomicFilterGroupType.Elements,
- TaxonomicFilterGroupType.SessionProperties,
- TaxonomicFilterGroupType.HogQLExpression,
- TaxonomicFilterGroupType.DataWarehousePersonProperties,
- ]}
- />
- </div>
+ <LemonDivider label="Filters" />
+ <div className="flex flex-col gap-4 pb-4">
+ <div>
+ <label className="text-sm font-medium mb-2 block">Properties</label>
+ <PropertyFilters
+ onChange={(properties) => setProperties(properties)}
+ pageKey={`tile_${tile.id}_properties`}
+ propertyFilters={overrides.properties ?? []}
+ taxonomicGroupTypes={[
+ TaxonomicFilterGroupType.EventProperties,
+ TaxonomicFilterGroupType.PersonProperties,
+ TaxonomicFilterGroupType.EventFeatureFlags,
+ TaxonomicFilterGroupType.EventMetadata,
+ ...(hasPageview ? [TaxonomicFilterGroupType.PageviewUrls] : []),
+ ...(hasScreen ? [TaxonomicFilterGroupType.Screens] : []),
+ TaxonomicFilterGroupType.EmailAddresses,
+ ...groupsTaxonomicTypes,
+ TaxonomicFilterGroupType.Cohorts,
+ TaxonomicFilterGroupType.Elements,
+ TaxonomicFilterGroupType.SessionProperties,
+ TaxonomicFilterGroupType.HogQLExpression,
+ TaxonomicFilterGroupType.DataWarehousePersonProperties,
+ ]}
+ />
+ </div>
- <div>
- <label className="text-sm font-medium mb-2 block">Breakdown</label>
- <BindLogic logic={insightLogic} props={breakdownInsightProps}>
- <TaxonomicBreakdownFilter
- insightProps={breakdownInsightProps}
- breakdownFilter={overrides.breakdown_filter}
- isTrends={false}
- isFunnels={false}
- showLabel={false}
- disabledReason={
- supportsBreakdown ? undefined : "This insight type doesn't support a breakdown override"
- }
- updateBreakdownFilter={(breakdown_filter) => {
- let newBreakdownFilter: BreakdownFilter | null = breakdown_filter
- // taxonomicBreakdownFilterLogic can generate an empty breakdown_filter object
- if (
- breakdown_filter &&
- !breakdown_filter.breakdown_type &&
- !breakdown_filter.breakdowns
- ) {
- newBreakdownFilter = null
- }
- setBreakdown(newBreakdownFilter)
- }}
- updateDisplay={() => {}}
- disablePropertyInfo
+ <div>
+ <div className="flex items-center gap-1 mb-2">
+ <label className="text-sm font-medium">Test account filtering</label>
+ <LemonButton
+ icon={<IconGear />}
+ size="xsmall"
+ noPadding
+ to={urls.settings('project-product-analytics', 'internal-user-filtering')}
+ tooltip="Configure internal and test account filters"
+ />
+ </div>
+ <LemonSegmentedButton<TestAccountFilterChoice>
size="small"
+ value={testAccountChoice}
+ onChange={(next) => setFilterTestAccounts(CHOICE_TO_FILTER[next])}
+ options={[
+ {
+ value: 'inherit',
+ label: 'Inherit',
+ tooltip: "Use the dashboard's setting, or the insight's own",
+ 'data-attr': 'tile-test-account-filter-inherit',
+ },
+ {
+ value: 'filter-out',
+ label: 'Filter out',
+ tooltip: 'Force test account filtering on for this insight',
+ disabledReason: !hasTestAccountFilters
+ ? "You haven't set any internal test filters. Click the gear icon to configure."
+ : undefined,
+ 'data-attr': 'tile-test-account-filter-out',
+ },
+ {
+ value: 'include',
+ label: 'Include',
+ tooltip: 'Force test account filtering off for this insight',
+ 'data-attr': 'tile-test-account-filter-include',
+ },
+ ]}
/>
- </BindLogic>
+ <p className="text-xs text-muted mt-1 mb-0">{CHOICE_HINTS[testAccountChoice]}</p>
+ </div>
+ </div>
+
+ <LemonDivider label="Display" />
+ <div className="flex flex-col gap-4 pb-4">
+ <div>
+ <label className="text-sm font-medium mb-2 block">Breakdown</label>
+ <BindLogic logic={insightLogic} props={breakdownInsightProps}>
+ <TaxonomicBreakdownFilter
+ insightProps={breakdownInsightProps}
+ breakdownFilter={overrides.breakdown_filter}
+ isTrends={false}
+ isFunnels={false}
+ showLabel={false}
+ disabledReason={
+ supportsBreakdown
+ ? undefined
+ : "This insight type doesn't support a breakdown override"
+ }
+ updateBreakdownFilter={(breakdown_filter) => {
+ let newBreakdownFilter: BreakdownFilter | null = breakdown_filter
+ // taxonomicBreakdownFilterLogic can generate an empty breakdown_filter object
+ if (
+ breakdown_filter &&
+ !breakdown_filter.breakdown_type &&
+ !breakdown_filter.breakdowns
+ ) {
+ newBreakdownFilter = null
+ }
+ setBreakdown(newBreakdownFilter)
+ }}
+ updateDisplay={() => {}}
+ disablePropertyInfo
+ size="small"
+ />
+ </BindLogic>
+ </div>
</div>
</div>
</div>
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index e48efe73b8e8..9868d16ddf66 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -4285,6 +4285,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
LemonDialog.openForm({
title: 'Override Tile Filters',
+ maxWidth: '40rem',
initialValues: {},
content: (
<BindLogic logic={tileLogic} props={tileLogicProps}>
diff --git a/frontend/src/scenes/dashboard/tileLogic.test.ts b/frontend/src/scenes/dashboard/tileLogic.test.ts
index 0fb85b6ca231..ce03f5faaa05 100644
--- a/frontend/src/scenes/dashboard/tileLogic.test.ts
+++ b/frontend/src/scenes/dashboard/tileLogic.test.ts
@@ -32,6 +32,17 @@ describe('tileLogic', () => {
expect(logic.values.overrides).toEqual({ date_from: '-7d' })
})
+ it('keeps an explicit false filterTestAccounts override, clearing only on null', () => {
+ logic.actions.setFilterTestAccounts(true)
+ expect(logic.values.overrides).toEqual({ date_from: '-7d', filterTestAccounts: true })
+
+ logic.actions.setFilterTestAccounts(false)
+ expect(logic.values.overrides).toEqual({ date_from: '-7d', filterTestAccounts: false })
+
+ logic.actions.setFilterTestAccounts(null)
+ expect(logic.values.overrides).toEqual({ date_from: '-7d' })
+ })
+
it('resetOverrides wipes every override', () => {
logic.actions.setInterval('day')
logic.actions.setBreakdown({ breakdown: '$browser', breakdown_type: 'event' })
diff --git a/frontend/src/scenes/dashboard/tileLogic.tsx b/frontend/src/scenes/dashboard/tileLogic.tsx
index f6a39ccf2a06..9c635efa1692 100644
--- a/frontend/src/scenes/dashboard/tileLogic.tsx
+++ b/frontend/src/scenes/dashboard/tileLogic.tsx
@@ -31,6 +31,9 @@ export interface tileLogicActions {
date_to: string | null | undefined
explicitDate: boolean | undefined
}
+ setFilterTestAccounts: (filterTestAccounts: boolean | null) => {
+ filterTestAccounts: boolean | null
+ }
setIgnoreDashboardFilters: (ignoreDashboardFilters: boolean) => {
ignoreDashboardFilters: boolean
}
@@ -67,6 +70,7 @@ export const tileLogic = kea<tileLogicType>([
setProperties: (properties: AnyPropertyFilter[] | null | undefined) => ({ properties }),
setBreakdown: (breakdown_filter: BreakdownFilter | null | undefined) => ({ breakdown_filter }),
setInterval: (interval: IntervalType | null | undefined) => ({ interval }),
+ setFilterTestAccounts: (filterTestAccounts: boolean | null) => ({ filterTestAccounts }),
setIgnoreDashboardFilters: (ignoreDashboardFilters: boolean) => ({ ignoreDashboardFilters }),
resetOverrides: true,
})),
@@ -121,6 +125,17 @@ export const tileLogic = kea<tileLogicType>([
}
return newState
},
+ setFilterTestAccounts: (state, { filterTestAccounts }) => {
+ const newState = { ...state }
+ // Tri-state: false means "force test account filtering off", so unlike the other
+ // overrides only null (inherit) clears the key.
+ if (filterTestAccounts === null) {
+ delete newState.filterTestAccounts
+ } else {
+ newState.filterTestAccounts = filterTestAccounts
+ }
+ return newState
+ },
setIgnoreDashboardFilters: (state, { ignoreDashboardFilters }) => {
const newState = { ...state }
if (ignoreDashboardFilters) {
diff --git a/posthog/hogql_queries/test/test_merge_filters_by_priority.py b/posthog/hogql_queries/test/test_merge_filters_by_priority.py
index cf33f261d6a6..a9f0c6545482 100644
--- a/posthog/hogql_queries/test/test_merge_filters_by_priority.py
+++ b/posthog/hogql_queries/test/test_merge_filters_by_priority.py
@@ -26,12 +26,19 @@ class TestMergeFiltersByPriority(SimpleTestCase):
def test_returns_single_layer_when_other_absent(self, _name, dashboard, tile, expected):
assert merge_filters_by_priority(dashboard, tile) == expected
- def test_tile_scalar_fields_win_over_dashboard(self):
+ @parameterized.expand(
+ [
+ ("tile forces test accounts on", False, True),
+ # False is a meaningful override (force off), so it must beat a dashboard True too.
+ ("tile forces test accounts off", True, False),
+ ]
+ )
+ def test_tile_scalar_fields_win_over_dashboard(self, _name, dashboard_fta, tile_fta):
merged = merge_filters_by_priority(
- {"interval": "day", "filterTestAccounts": False},
- {"interval": "week", "filterTestAccounts": True},
+ {"interval": "day", "filterTestAccounts": dashboard_fta},
+ {"interval": "week", "filterTestAccounts": tile_fta},
)
- assert merged == {"interval": "week", "filterTestAccounts": True}
+ assert merged == {"interval": "week", "filterTestAccounts": tile_fta}
def test_dashboard_scalar_kept_when_tile_leaves_it_unset(self):
merged = merge_filters_by_priority(