feat: Support EventMetadata filter for EventsQuery/TrendsQuery#30232
feat: Support EventMetadata filter for EventsQuery/TrendsQuery#30232danielbachhuber merged 46 commits intomasterfrom
Conversation
There was a problem hiding this comment.
PR Summary
This PR adds support for filtering events by metadata properties in EventsQuery and TrendsQuery, enabling direct filtering on fields like distinct_id and timestamp.
Key changes:
- Added new
EventMetadatafilter type infrontend/src/types.tswith corresponding taxonomic group support - Implemented metadata property handling in
posthog/hogql/property.pyfor direct field access - Added test coverage in
test_events_query_runner.pyandtest_trends_query_runner.pyfor metadata filtering - Updated property validation in
posthog/models/property/property.pyto support event_metadata type - Extended taxonomic filter logic with new metadata options and icons in
taxonomicFilterLogic.tsx
12 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile
| options: eventMetadataOptions, | ||
| getIcon: (option: Record<string, string>) => option.icon, | ||
| getName: (option: Record<string, string>) => option.name, | ||
| getValue: (option: Record<string, string>) => option.value, | ||
| getPopoverHeader: () => 'Event metadata', |
There was a problem hiding this comment.
logic: Missing type validation for option.icon - could cause runtime errors if icon is not a valid React element
posthog/hogql/property.py
Outdated
| return _expr_to_compare_op( | ||
| expr=ast.Field(chain=[property.key]), | ||
| value=value, | ||
| operator=operator, | ||
| team=team, | ||
| property=property, | ||
| is_json_field=False, | ||
| ) |
There was a problem hiding this comment.
logic: Missing boolean value handling for event metadata fields
mariusandra
left a comment
There was a problem hiding this comment.
Cool stuff, left some thoughts inline.
| { icon: <IconLogomark />, name: 'Distinct ID', value: 'distinct_id' }, | ||
| { icon: <IconLogomark />, name: 'Timestamp', value: 'timestamp' }, |
There was a problem hiding this comment.
Can you also add event here? Perhaps also person_id if it works well.
There was a problem hiding this comment.
Thinking out loud...
While I think it makes total sense to have a new event metadata TaxonomicFilterGroupType for this new tab/pill... I wonder if this is the right PropertyFilterType to add. It feels a bit restrictive. More specifically, I wonder if EventMetadataPropertyFilter could be merged with HogQLPropertyFilter.
Looking at this list of options here, we effectively have expression operator value, where the expression comes from a predefined list of SQL expressions. There's no backend validation... so it's effectively a generic SQLValueFilter or something like that.
The existing HogQLPropertyFilter does the expression part as its key. We could just add operator and value into it... or if making a new property filter, to make it more abstract in its name.
There was a problem hiding this comment.
Can you also add
eventhere? Perhaps alsoperson_idif it works well.
Added with 94d3026. Both work fine.
The existing
HogQLPropertyFilterdoes theexpressionpart as itskey. We could just addoperatorandvalueinto it... or if making a new property filter, to make it more abstract in its name.
Doesn't that mean we'd need to apply this logic and this logic in the client?
There was a problem hiding this comment.
Doesn't that mean we'd need to apply this logic and this logic in the client?
Hmm... well, no... it'd work exactly like the filter you now added works, just renamed to HogQLPropertyValueFilter or something more abstract.
My worry is that people will want to start filtering for all sorts of other and custom expressions. However I do like that this is scoped to just events, making some things a lot easier than if it'd be totally abstract...
|
Do we want to call this "$group_2" in the customer facing UI? I don't think we use that terminology anywhere and it's quite an implementation detail to expose. "Organization ID" might make more sense? I'm not sure we want to surface that group naming anywhere? |
📸 UI snapshots have been updated1 snapshot changes in total. 0 added, 1 modified, 0 deleted:
Triggered by this commit. |
|
This looks awesome. Much overdue. |
📸 UI snapshots have been updated1 snapshot changes in total. 0 added, 1 modified, 0 deleted:
Triggered by this commit. |
|
Size Change: -194 kB (-1.95%) Total Size: 9.76 MB
|
@aspicer My intent was to ease this transition: Specifically, when the user selects "organization" in the UI and then sees It looks like posthog/frontend/src/lib/taxonomy.tsx Lines 1915 to 1921 in 55db33c We could fetch the group type mapping labels dynamically but I don't love the idea of adding a call to a logic. It seems like too many of our components have random dependencies on logics. I do think Thoughts? |
|
It's definitely better for the user to see a clearly labelled "Organization ID" rather than a cryptic "$group_2", so I think it's worth taking whatever extra steps are needed to make it work. We should have all the group names in memory normally as well. |
📸 UI snapshots have been updated1 snapshot changes in total. 0 added, 1 modified, 0 deleted:
Triggered by this commit. |
@mariusandra I think I fixed this with fcfba12 and 0fda1bc (at least, I don't see the erroneous
I tried this initially with e5f6405 but had to change it in bef7375 because of some bug (don't recall exactly). Is there some alternative you had in mind? |
📸 UI snapshots have been updated1 snapshot changes in total. 0 added, 1 modified, 0 deleted:
Triggered by this commit. |
There was a problem hiding this comment.
The person_id filter dropdown doesn't work:
and I left a few other comments inline.
As for skipping checkOrLoadPropertyDefinition, well, just prepopulate propertyDefinitionStorage with the metadata properties and that should be enough, no? One trick you can use is to rename the propertyDefinitionStorage reducer to rawPropertyDefinitionStorage, and then add a new selector called propertyDefinitionStorage that merges rawPropertyDefinitionStorage and eventMetadataPropertyDefinitions into one object.
@mariusandra Good catch, fixed with fdf4a12
Ah, that's quite nice. Thanks for the suggestion! Updated with 070a82d |
mariusandra
left a comment
There was a problem hiding this comment.
This has been a big undertaking and thanks for putting up with all my comments and remarks. However I think we made it! 🙌
I still left one comment inline about merging the objects in taxonomy.tsx, however the rest LGTM! So merge whenever you're ready! 👍
frontend/src/lib/taxonomy.tsx
Outdated
| event_metadata: { | ||
| person_id: { | ||
| label: 'Person ID', | ||
| description: 'The ID of the person, depending on the person properties mode.', | ||
| examples: ['16ff262c4301e5-0aa346c03894bc-39667c0e-1aeaa0-16ff262c431767'], | ||
| }, | ||
| // Other values are assigned below | ||
| }, |
There was a problem hiding this comment.
Hmm... I'm not too sure, but it looks like it was added in this PR
Looking at the code:
... it does seem like "distinct_id" was explicitly moved out of the "event_properties" tab because it wasn't a property, but it was more like (event) "metadata". So I think the intent is the same. As for where is it used, I could only find the CORE_FILTER_DEFINITIONS_BY_GROUP.event_properties.distinct_id = CORE_FILTER_DEFINITIONS_BY_GROUP.metadata.distinct_id line right now.
I think it's safe to merge the two... unless we get any immediate typescript errors, probably the worst that can happen is some missing descriptions, which can be fixed later... I also think it's worth merging, as otherwise people will lose time wondering the same question 2 years down the line :D.
This reverts commit bd93bec.









See #29881
See https://posthog.slack.com/archives/C0368RPHLQH/p1742417106336409
Changes
Adds support for filtering
EventsQueryorTrendsQueryby a newEventMetadatafilter:CleanShot.2025-03-21.at.10.27.10.mp4
The happy path works but I'm not sure how many gotchas I might be missing.
How did you test this code?
Added backend tests.