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.

chore(events): adds include_person qp to event GET endpoint (#60169)

Nick Best49cdd3237db2
diff --git a/posthog/api/event.py b/posthog/api/event.py
index 438f06dddc8c..1f88fadc6d4e 100644
--- a/posthog/api/event.py
+++ b/posthog/api/event.py
@@ -260,6 +260,11 @@ class EventViewSet(
deprecated=True,
),
PropertiesSerializer(required=False),
+ OpenApiParameter(
+ "include_person",
+ OpenApiTypes.BOOL,
+ description="Include person details for each event. Default: false.",
+ ),
],
)
def list(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:
@@ -388,13 +393,14 @@ class EventViewSet(
action_id=request.GET.get("action_id"),
)
+ context = {**restricted_context}
+ if request.query_params.get("include_person", "").lower() in ("true", "1"):
+ context["people"] = self._get_people(query_result, team)
+
result = ClickhouseEventSerializer(
query_result[0:limit],
many=True,
- context={
- "people": self._get_people(query_result, team),
- **restricted_context,
- },
+ context=context,
).data
next_url: Optional[str] = None
@@ -421,7 +427,14 @@ class EventViewSet(
return get_persons_mapped_by_distinct_id(team.pk, distinct_ids)
@extend_schema(
- parameters=[OpenApiParameter("id", OpenApiTypes.STR, OpenApiParameter.PATH)],
+ parameters=[
+ OpenApiParameter("id", OpenApiTypes.STR, OpenApiParameter.PATH),
+ OpenApiParameter(
+ "include_person",
+ OpenApiTypes.BOOL,
+ description="Include person details for the event. Default: false.",
+ ),
+ ],
responses={200: OpenApiTypes.OBJECT},
)
def retrieve(
@@ -450,7 +463,7 @@ class EventViewSet(
raise NotFound(detail=f"No events exist for event UUID {pk}")
query_context = {**self._get_restricted_properties_context(request, self.team)}
- if request.query_params.get("include_person", False):
+ if request.query_params.get("include_person", "").lower() in ("true", "1"):
query_context["people"] = self._get_people(query_result, self.team)
res = ClickhouseEventSerializer(query_result[0], many=False, context=query_context).data
diff --git a/posthog/api/test/test_event.py b/posthog/api/test/test_event.py
index 24efa35c0d98..44097ff01490 100644
--- a/posthog/api/test/test_event.py
+++ b/posthog/api/test/test_event.py
@@ -67,6 +67,9 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
flush_persons_and_events()
response = self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=2").json()
+ assert response["results"][0]["person"] is None
+
+ response = self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=2&include_person=true").json()
assert response["results"][0]["person"] == {
"distinct_ids": ["2"],
"is_identified": True,
@@ -98,8 +101,8 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
flush_persons_and_events()
# Django session, PostHog user, PostHog team, PostHog org membership,
- # instance setting check, person and distinct id
- with self.assertNumQueries(10):
+ # instance setting check
+ with self.assertNumQueries(9):
response = self.client.get(f"/api/projects/{self.team.id}/events/?event=event_name").json()
assert response["results"][0]["event"] == "event_name"
@@ -125,8 +128,8 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
flush_persons_and_events()
# Django session, PostHog user, PostHog team, PostHog org membership,
- # access control, instance settings (poe, rate limit), person and distinct id
- expected_queries = 11
+ # access control, instance settings (poe, rate limit)
+ expected_queries = 10
with self.assertNumQueries(expected_queries):
response = self.client.get(
@@ -866,6 +869,7 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
is_identified=True,
)
event_id = _create_event(team=self.team, event="event", distinct_id="1", timestamp=timezone.now())
+ flush_persons_and_events()
response = self.client.get(f"/api/projects/{self.team.id}/events/{event_id}")
assert response.status_code == status.HTTP_200_OK
@@ -932,6 +936,24 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
response = self.client.get(f"/api/projects/{self.team.id}/events/?limit=50000").json()
assert len(response["results"]) == 2
+ @patch("posthog.api.event.get_persons_mapped_by_distinct_id")
+ def test_list_without_include_person_skips_person_lookup(self, mock_get_persons):
+ _create_person(team=self.team, distinct_ids=["1"], is_identified=True)
+ _create_event(event="$pageview", team=self.team, distinct_id="1", properties={"$ip": "8.8.8.8"})
+
+ response = self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=1").json()
+ assert response["results"][0]["person"] is None
+ mock_get_persons.assert_not_called()
+
+ @patch("posthog.api.event.get_persons_mapped_by_distinct_id")
+ def test_list_with_include_person_calls_person_lookup(self, mock_get_persons):
+ _create_person(team=self.team, distinct_ids=["1"], is_identified=True)
+ _create_event(event="$pageview", team=self.team, distinct_id="1", properties={"$ip": "8.8.8.8"})
+ mock_get_persons.return_value = {}
+
+ self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=1&include_person=true")
+ mock_get_persons.assert_called_once()
+
def test_get_events_with_specified_token(self):
_, _, user2 = User.objects.bootstrap("Test", "[email protected]", None)
assert user2.team is not None
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 53b26bb3ea3b..12ae1dfb745c 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -38813,6 +38813,10 @@ export namespace Schemas {
*/
event?: string;
format?: EnvironmentsEventsListFormat;
+ /**
+ * Include person details for each event. Default: false.
+ */
+ include_person?: boolean;
/**
* The maximum number of results to return
*/
@@ -38849,6 +38853,10 @@ export namespace Schemas {
export type EnvironmentsEventsRetrieveParams = {
format?: EnvironmentsEventsRetrieveFormat;
+ /**
+ * Include person details for the event. Default: false.
+ */
+ include_person?: boolean;
};
export type EnvironmentsEventsRetrieveFormat = typeof EnvironmentsEventsRetrieveFormat[keyof typeof EnvironmentsEventsRetrieveFormat];
@@ -43891,6 +43899,10 @@ export namespace Schemas {
*/
event?: string;
format?: EventsListFormat;
+ /**
+ * Include person details for each event. Default: false.
+ */
+ include_person?: boolean;
/**
* The maximum number of results to return
*/
@@ -43927,6 +43939,10 @@ export namespace Schemas {
export type EventsRetrieveParams = {
format?: EventsRetrieveFormat;
+ /**
+ * Include person details for the event. Default: false.
+ */
+ include_person?: boolean;
};
export type EventsRetrieveFormat = typeof EventsRetrieveFormat[keyof typeof EventsRetrieveFormat];