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: add group properties to create event (#57793)

Alex Vf4acfe4b890a
diff --git a/products/conversations/backend/api/tests/test_events.py b/products/conversations/backend/api/tests/test_events.py
index 9736a7673ef7..774cadcc58fa 100644
--- a/products/conversations/backend/api/tests/test_events.py
+++ b/products/conversations/backend/api/tests/test_events.py
@@ -212,3 +212,60 @@ class TestConversationEvents(BaseTest):
# Tokens must be different (proves isolation)
assert first_call["token"] != second_call["token"]
+
+ @parameterized.expand(
+ [
+ ("identified_person", True, True, True),
+ ("anonymous_person", True, False, False),
+ ("no_person", False, False, False),
+ ("empty_distinct_id", False, False, False),
+ ]
+ )
+ @patch("products.conversations.backend.events.capture_internal")
+ @patch("products.conversations.backend.events.get_persons_by_distinct_ids")
+ def test_capture_ticket_created_person_processing(
+ self, _name, has_person, is_identified, expect_groups, mock_get_persons, mock_capture
+ ):
+ from posthog.models.person.person import Person
+
+ is_empty_distinct_id = _name == "empty_distinct_id"
+ ticket = self.ticket
+ if is_empty_distinct_id:
+ ticket = Ticket.objects.create_with_number(
+ team=self.team,
+ widget_session_id=str(uuid.uuid4()),
+ distinct_id="",
+ channel_source="github",
+ )
+
+ if has_person:
+ mock_get_persons.return_value = [Person(team_id=self.team.id, is_identified=is_identified)]
+ else:
+ mock_get_persons.return_value = []
+
+ capture_ticket_created(ticket)
+
+ if is_empty_distinct_id:
+ mock_get_persons.assert_not_called()
+ else:
+ mock_get_persons.assert_called_once_with(self.team.id, [ticket.distinct_id])
+
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["process_person_profile"] is expect_groups
+ if expect_groups:
+ groups = call_kwargs["properties"]["$groups"]
+ assert groups["organization"] == str(self.team.organization_id)
+ assert groups["project"] == str(self.team.uuid)
+ assert "instance" in groups
+ else:
+ assert "$groups" not in call_kwargs["properties"]
+
+ @patch("products.conversations.backend.events.capture_internal")
+ @patch("products.conversations.backend.events.get_persons_by_distinct_ids", side_effect=Exception("db timeout"))
+ def test_capture_ticket_created_still_fires_on_person_lookup_failure(self, mock_get_persons, mock_capture):
+ capture_ticket_created(self.ticket)
+
+ mock_capture.assert_called_once()
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["process_person_profile"] is False
+ assert "$groups" not in call_kwargs["properties"]
diff --git a/products/conversations/backend/events.py b/products/conversations/backend/events.py
index 14e214df2e46..95062c117600 100644
--- a/products/conversations/backend/events.py
+++ b/products/conversations/backend/events.py
@@ -6,10 +6,16 @@ All events use the ticket's distinct_id so they're tied to the customer person.
Events are sent to the customer's PostHog project via their team's API token.
"""
+import structlog
+
from posthog.api.capture import capture_internal
+from posthog.event_usage import groups as build_groups
+from posthog.models.person.util import get_persons_by_distinct_ids
from products.conversations.backend.models import Ticket
+logger = structlog.get_logger(__name__)
+
EVENT_SOURCE = "conversations_events"
@@ -30,13 +36,26 @@ def capture_ticket_created(ticket: Ticket) -> None:
properties["customer_name"] = traits.get("name", "")
properties["customer_email"] = traits.get("email", "")
+ team = ticket.team
+ team_id = team.id
+ process_person = False
+ if ticket.distinct_id:
+ try:
+ persons = get_persons_by_distinct_ids(team_id, [ticket.distinct_id])
+ if any(p.is_identified for p in persons):
+ process_person = True
+ properties["$groups"] = build_groups(team.organization, team)
+ except Exception:
+ logger.exception("ticket_created_person_lookup_failed", team_id=team_id, ticket_id=str(ticket.id))
+
capture_internal(
- token=ticket.team.api_token,
+ token=team.api_token,
event_name="$conversation_ticket_created",
event_source=EVENT_SOURCE,
distinct_id=ticket.distinct_id or ticket.channel_source or "unknown",
timestamp=None,
properties=properties,
+ process_person_profile=process_person,
)