: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(customer-analytics): filter usage metrics by person property (#103513)
posthog[bot]c36aad65031a
diff --git a/frontend/src/scenes/settings/environment/UsageMetricsConfig.tsx b/frontend/src/scenes/settings/environment/UsageMetricsConfig.tsxindex 1c9e946641e5..d1e8555cd0c8 100644--- a/frontend/src/scenes/settings/environment/UsageMetricsConfig.tsx+++ b/frontend/src/scenes/settings/environment/UsageMetricsConfig.tsx@@ -150,6 +150,7 @@ function UsageMetricsForm(): JSX.Element {const { setUsageMetricValue } = useActions(usageMetricsConfigLogic)const taxonomicGroupTypes = [TaxonomicFilterGroupType.EventProperties,+ TaxonomicFilterGroupType.PersonProperties,TaxonomicFilterGroupType.EventMetadata,TaxonomicFilterGroupType.HogQLExpression,]diff --git a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.ambr b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.ambrindex 322f83545ded..0960c90c76e1 100644--- a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.ambr+++ b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.ambr@@ -165,6 +165,29 @@use_hive_partitioning=0'''# ---+# name: TestUsageMetricsQueryRunner.test_person_property_filter_splits_a_group_metric_by_user_type+ '''+ SELECT toStartOfDay(toTimeZone(toTimeZone(events.timestamp, 'UTC'), 'UTC')) AS day,+ accurateCastOrNull(countIf(and(and(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'user_type'), ''), 'null'), '^"|"$', ''), 'admin'), 0), equals(events.event, 'metric_event')), and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-10-02 12:11:00.000000', 6, 'UTC')), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC'))))), 'Float64') AS m0_value,+ accurateCastOrNull(countIf(and(and(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'user_type'), ''), 'null'), '^"|"$', ''), 'admin'), 0), equals(events.event, 'metric_event')), and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-09-25 12:11:00.000000', 6, 'UTC')), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-10-02 12:11:00.000000', 6, 'UTC'))))), 'Float64') AS m0_previous+ FROM events+ WHERE and(equals(events.team_id, 99999), equals(events.`$group_0`, 'test_group'), greaterOrEquals(events.timestamp, toDateTime64('2025-09-25 12:11:00.000000', 6, 'UTC')), lessOrEquals(events.timestamp, toDateTime64('today', 6, 'UTC')))+ GROUP BY day+ ORDER BY day ASC+ LIMIT 100 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---# name: TestUsageMetricsQueryRunner.test_sum_math_aggregation'''SELECT toStartOfDay(toTimeZone(toTimeZone(events.timestamp, 'UTC'), 'UTC')) AS day,diff --git a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.new_events_schema.ambr b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.new_events_schema.ambrindex 3c80465f2107..1629987fb79e 100644--- a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.new_events_schema.ambr+++ b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_usage_metrics_query_runner.new_events_schema.ambr@@ -165,6 +165,29 @@use_hive_partitioning=0'''# ---+# name: TestUsageMetricsQueryRunner.test_person_property_filter_splits_a_group_metric_by_user_type[new_events_schema]+ '''+ SELECT toStartOfDay(toTimeZone(toTimeZone(events.timestamp, 'UTC'), 'UTC')) AS day,+ accurateCastOrNull(countIf(and(and(ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^user_type)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^user_type)), if(isNull(nullIf(toString(events.person_properties.user_type), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.user_type, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.user_type), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.user_type), '[]'), '{}'), toString(events.person_properties.user_type))))), 'admin'), 0), equals(events.event, 'metric_event')), and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-10-02 12:11:00.000000', 6, 'UTC')), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC'))))), 'Float64') AS m0_value,+ accurateCastOrNull(countIf(and(and(ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^user_type)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^user_type)), if(isNull(nullIf(toString(events.person_properties.user_type), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.user_type, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.user_type), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.user_type, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.user_type), '[]'), '{}'), toString(events.person_properties.user_type))))), 'admin'), 0), equals(events.event, 'metric_event')), and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-09-25 12:11:00.000000', 6, 'UTC')), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2025-10-02 12:11:00.000000', 6, 'UTC'))))), 'Float64') AS m0_previous+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), equals(events.properties.`$group_0`, 'test_group'), greaterOrEquals(events.timestamp, toDateTime64('2025-09-25 12:11:00.000000', 6, 'UTC')), lessOrEquals(events.timestamp, toDateTime64('today', 6, 'UTC')))+ GROUP BY day+ ORDER BY day ASC+ LIMIT 100 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---# name: TestUsageMetricsQueryRunner.test_sum_math_aggregation[new_events_schema]'''SELECT toStartOfDay(toTimeZone(toTimeZone(events.timestamp, 'UTC'), 'UTC')) AS day,diff --git a/products/customer_analytics/backend/hogql_queries/test/test_usage_metrics_query_runner.py b/products/customer_analytics/backend/hogql_queries/test/test_usage_metrics_query_runner.pyindex 6439898a0d05..222da558c26d 100644--- a/products/customer_analytics/backend/hogql_queries/test/test_usage_metrics_query_runner.py+++ b/products/customer_analytics/backend/hogql_queries/test/test_usage_metrics_query_runner.py@@ -407,6 +407,58 @@ class TestUsageMetricsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertEqual(results[1]["id"], str(another_metric.id))self.assertEqual(results[1]["value"], 5.0)+ @time_machine.travel("2025-10-09T12:11:00", tick=False)+ @snapshot_clickhouse_queries+ def test_person_property_filter_splits_a_group_metric_by_user_type(self):+ group_key = "test_group"+ self._create_group(group_key=group_key, group_type_index=0)+ metric = GroupUsageMetric.objects.create(+ id=self.test_metric_id,+ team=self.team,+ group_type_index=0,+ name="Admin events",+ format=GroupUsageMetric.Format.NUMERIC,+ interval=7,+ display=GroupUsageMetric.Display.NUMBER,+ filters={+ "events": [{"id": "metric_event", "type": "events", "order": 0}],+ "properties": [{"key": "user_type", "type": "person", "value": ["admin"], "operator": "exact"}],+ },+ )+ for user_type, person_uuid, current_count, previous_count in [+ ("admin", "2c6b2f4a-9f3e-4a1c-8f5d-2b7a1c4e9d30", 3, 2),+ ("viewer", "7e1d5c83-2a44-4f7b-9c10-6d8e3b5a2f41", 5, 4),+ ]:+ distinct_id = f"{user_type}-distinct-id"+ _create_person(+ distinct_ids=[distinct_id],+ uuid=person_uuid,+ team=self.team,+ properties={"user_type": user_type},+ )+ for count, moment in [+ (current_count, timezone.now()),+ (previous_count, timezone.now() - timedelta(days=8)),+ ]:+ with time_machine.travel(moment, tick=False):+ for _ in range(count):+ _create_event(+ event="metric_event",+ team=self.team,+ person_id=person_uuid,+ distinct_id=distinct_id,+ properties={"$group_0": group_key},+ )+ flush_persons_and_events()++ query_result = self._calculate(group_type_index=0, group_key=group_key)++ results = query_result["results"]+ self.assertEqual(len(results), 1)+ self.assertEqual(results[0]["id"], str(metric.id))+ self.assertEqual(results[0]["value"], 3.0)+ self.assertEqual(results[0]["previous"], 2.0)+@time_machine.travel("2025-10-09T12:11:00", tick=False)@snapshot_clickhouse_queriesdef test_sum_math_aggregation(self):