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(insights): track behavioral filter configuration (#90580)

Sam Penningtonb826488bc0fd
diff --git a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx
index 764449c6ef08..7711a60cfa7b 100644
--- a/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx
+++ b/frontend/src/lib/components/PropertyFilters/components/BehavioralPropertyFilterRow.tsx
@@ -1,4 +1,5 @@
import { useValues } from 'kea'
+import posthog from 'posthog-js'
import { useState } from 'react'
import { IconFilter } from '@posthog/icons'
@@ -121,6 +122,7 @@ export function BehavioralPropertyFilterRow({
const countValue = filter.operator_value ?? 1
const setCount = (operator: PropertyOperator, operatorValue: number): void => {
+ posthog.capture('behavioral filter count changed', { operator, operator_value: operatorValue })
onChange(withBehavioralCount(filter, operator, operatorValue))
}
@@ -131,7 +133,12 @@ export function BehavioralPropertyFilterRow({
<LemonSelect
size={size}
value={!!filter.negation}
- onChange={(negation) => onChange(withBehavioralNegation(filter, negation))}
+ onChange={(negation) => {
+ posthog.capture('behavioral filter behavior changed', {
+ behavior: negation ? 'did_not_perform' : 'performed',
+ })
+ onChange(withBehavioralNegation(filter, negation))
+ }}
options={[
{ value: false, label: 'Performed' },
{ value: true, label: 'Did not perform' },
@@ -149,14 +156,14 @@ export function BehavioralPropertyFilterRow({
}
groupTypes={[TaxonomicFilterGroupType.Events, TaxonomicFilterGroupType.Actions]}
value={filter.event_type === 'actions' ? Number(filter.key) : filter.key}
- onChange={(value, groupType) =>
- onChange({
- ...filter,
- key: String(value),
- event_type:
- groupType === TaxonomicFilterGroupType.Actions ? 'actions' : 'events',
+ onChange={(value, groupType) => {
+ const eventType =
+ groupType === TaxonomicFilterGroupType.Actions ? 'actions' : 'events'
+ posthog.capture('behavioral filter event or action changed', {
+ event_type: eventType,
})
- }
+ onChange({ ...filter, key: String(value), event_type: eventType })
+ }}
renderValue={() => <span>{behavioralEntityLabel(filter, actionsById)}</span>}
placeholder="Select an event or action"
fullWidth
@@ -219,13 +226,26 @@ export function BehavioralPropertyFilterRow({
min={1}
className="w-14"
value={filter.time_value ?? 30}
- onChange={(timeValue) => onChange({ ...filter, time_value: positiveOr(timeValue, 30) })}
+ onChange={(timeValue) => {
+ const nextTimeValue = positiveOr(timeValue, 30)
+ posthog.capture('behavioral filter time period changed', {
+ time_value: nextTimeValue,
+ time_interval: filter.time_interval ?? TimeUnitType.Day,
+ })
+ onChange({ ...filter, time_value: nextTimeValue })
+ }}
data-attr="behavioral-filter-time-value"
/>
<LemonSelect
size={size}
value={filter.time_interval ?? TimeUnitType.Day}
- onChange={(timeInterval) => onChange({ ...filter, time_interval: timeInterval })}
+ onChange={(timeInterval) => {
+ posthog.capture('behavioral filter time period changed', {
+ time_value: filter.time_value ?? 30,
+ time_interval: timeInterval,
+ })
+ onChange({ ...filter, time_interval: timeInterval })
+ }}
options={TIME_INTERVAL_OPTIONS}
data-attr="behavioral-filter-time-interval"
/>
diff --git a/frontend/src/lib/utils/eventUsageLogic.test.ts b/frontend/src/lib/utils/eventUsageLogic.test.ts
index 10da1971d982..6c8fc6170f07 100644
--- a/frontend/src/lib/utils/eventUsageLogic.test.ts
+++ b/frontend/src/lib/utils/eventUsageLogic.test.ts
@@ -6,12 +6,13 @@ import type {
ExperimentRatioMetric,
ExperimentRetentionMetric,
ExperimentTrendsQuery,
+ Node,
} from '~/queries/schema/schema-general'
-import { BaseMathType } from '~/types'
+import { BaseMathType, BehavioralEventType, FilterLogicalOperator, PropertyFilterType } from '~/types'
-import { getEventPropertiesForMetric } from './eventUsageLogic'
+import { getEventPropertiesForMetric, sanitizeQuery } from './eventUsageLogic'
-describe('getEventPropertiesForMetric', () => {
+describe('eventUsageLogic', () => {
describe('ExperimentMetric (new format)', () => {
it('extracts funnel metric properties', () => {
const metric: ExperimentFunnelMetric = {
@@ -210,6 +211,49 @@ describe('getEventPropertiesForMetric', () => {
})
})
+ describe('sanitizeQuery', () => {
+ it('counts behavioral filters across global and series filters', () => {
+ const query = {
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [
+ {
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
+ properties: [
+ {
+ type: PropertyFilterType.Behavioral,
+ key: 'signed up',
+ value: BehavioralEventType.PerformEvent,
+ event_type: 'events',
+ },
+ ],
+ },
+ ],
+ properties: {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: PropertyFilterType.Behavioral,
+ key: 'completed onboarding',
+ value: BehavioralEventType.PerformEvent,
+ event_type: 'events',
+ },
+ ],
+ },
+ ],
+ },
+ },
+ } as unknown as Node
+
+ expect(sanitizeQuery(query).behavioral_filter_count).toBe(2)
+ })
+ })
+
describe('legacy formats', () => {
it('extracts ExperimentFunnelsQuery properties', () => {
const metric = {
diff --git a/frontend/src/lib/utils/eventUsageLogic.ts b/frontend/src/lib/utils/eventUsageLogic.ts
index 88be8b30cc05..ad681016e51d 100644
--- a/frontend/src/lib/utils/eventUsageLogic.ts
+++ b/frontend/src/lib/utils/eventUsageLogic.ts
@@ -389,8 +389,23 @@ function sanitizeDashboard(dashboard: DashboardType<QueryBasedInsightModel> | nu
}
}
+function countBehavioralFilters(value: unknown): number {
+ if (Array.isArray(value)) {
+ return value.reduce((count, item) => count + countBehavioralFilters(item), 0)
+ }
+ if (!value || typeof value !== 'object') {
+ return 0
+ }
+
+ const record = value as Record<string, unknown>
+ return (
+ Number(record.type === PropertyFilterType.Behavioral) +
+ Object.values(record).reduce<number>((count, item) => count + countBehavioralFilters(item), 0)
+ )
+}
+
/** Takes a query and returns an object with "useful" properties that don't contain sensitive data. */
-function sanitizeQuery(query: Node | null): Record<string, string | number | boolean | undefined> {
+export function sanitizeQuery(query: Node | null): Record<string, string | number | boolean | undefined> {
const payload: Record<string, string | number | boolean | undefined> = {
query_kind: query?.kind,
query_source_kind: isNodeWithSource(query) ? query.source.kind : undefined,
@@ -418,6 +433,7 @@ function sanitizeQuery(query: Node | null): Record<string, string | number | boo
// properties
payload.has_properties = !!properties
+ payload.behavioral_filter_count = countBehavioralFilters(querySource)
payload.filter_test_accounts = filterTestAccounts
// breakdown
diff --git a/products/product_analytics/backend/models/insight.py b/products/product_analytics/backend/models/insight.py
index 108a5cc29f22..196e887e3963 100644
--- a/products/product_analytics/backend/models/insight.py
+++ b/products/product_analytics/backend/models/insight.py
@@ -37,6 +37,14 @@ _ANALYTICS_INSIGHT_QUERY_KINDS = frozenset(
)
+def _count_behavioral_filters(value: object) -> int:
+ if isinstance(value, list):
+ return sum(_count_behavioral_filters(item) for item in value)
+ if not isinstance(value, dict):
+ return 0
+ return int(value.get("type") == "behavioral") + sum(_count_behavioral_filters(item) for item in value.values())
+
+
if TYPE_CHECKING:
from posthog.schema import NodeKind
@@ -219,6 +227,7 @@ class Insight(RootTeamMixin, FileSystemSyncMixin, models.Model):
1 for s in series if isinstance(s, dict) and s.get("kind") == "DataWarehouseNode"
)
metadata["has_properties"] = bool(source.get("properties"))
+ metadata["behavioral_filter_count"] = _count_behavioral_filters(source)
if "filterTestAccounts" in source:
metadata["filter_test_accounts"] = source.get("filterTestAccounts")
breakdown_filter = source.get("breakdownFilter")
diff --git a/products/product_analytics/backend/models/test/test_insight_model.py b/products/product_analytics/backend/models/test/test_insight_model.py
index 7770f9221f53..f50d4b1e405e 100644
--- a/products/product_analytics/backend/models/test/test_insight_model.py
+++ b/products/product_analytics/backend/models/test/test_insight_model.py
@@ -321,9 +321,36 @@ class TestInsightModel(BaseTest):
"source": {
"kind": "TrendsQuery",
"series": [
- {"kind": "EventsNode", "event": "$pageview"},
+ {
+ "kind": "EventsNode",
+ "event": "$pageview",
+ "properties": [
+ {
+ "type": "behavioral",
+ "key": "signed up",
+ "value": "performed_event",
+ "event_type": "events",
+ }
+ ],
+ },
{"kind": "ActionsNode", "id": 1},
],
+ "properties": {
+ "type": "AND",
+ "values": [
+ {
+ "type": "AND",
+ "values": [
+ {
+ "type": "behavioral",
+ "key": "completed onboarding",
+ "value": "performed_event",
+ "event_type": "events",
+ }
+ ],
+ }
+ ],
+ },
"dateRange": {"date_from": "-7d"},
"filterTestAccounts": True,
"breakdownFilter": {"breakdown_type": "event"},
@@ -336,7 +363,8 @@ class TestInsightModel(BaseTest):
"event_entity_count": 1,
"action_entity_count": 1,
"data_warehouse_entity_count": 0,
- "has_properties": False,
+ "has_properties": True,
+ "behavioral_filter_count": 2,
"filter_test_accounts": True,
"breakdown_type": "event",
"has_formula": True,