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.

fix: group properties on create event (#59054)

Alex V641cc8e0f9f8
diff --git a/products/conversations/backend/api/tests/test_events.py b/products/conversations/backend/api/tests/test_events.py
index 774cadcc58fa..0e3dabe91ff0 100644
--- a/products/conversations/backend/api/tests/test_events.py
+++ b/products/conversations/backend/api/tests/test_events.py
@@ -5,6 +5,9 @@ from unittest.mock import patch
from parameterized import parameterized
+from posthog.models.organization import Organization, OrganizationMembership
+from posthog.models.user import User
+
from products.conversations.backend.events import (
EVENT_SOURCE,
capture_message_received,
@@ -215,7 +218,6 @@ class TestConversationEvents(BaseTest):
@parameterized.expand(
[
- ("identified_person", True, True, True),
("anonymous_person", True, False, False),
("no_person", False, False, False),
("empty_distinct_id", False, False, False),
@@ -252,13 +254,51 @@ class TestConversationEvents(BaseTest):
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"]
+ assert "$groups" not in call_kwargs["properties"]
+
+ @patch("products.conversations.backend.events.capture_internal")
+ @patch("products.conversations.backend.events.get_persons_by_distinct_ids")
+ def test_capture_ticket_created_groups_from_person_org(self, mock_get_persons, mock_capture):
+ from posthog.models.person.person import Person
+
+ person_org = Organization.objects.create(name="Person Org")
+ person_user = User.objects.create(email="[email protected]", distinct_id="customer-123")
+ OrganizationMembership.objects.create(user=person_user, organization=person_org)
+
+ mock_get_persons.return_value = [Person(team_id=self.team.id, is_identified=True)]
+
+ capture_ticket_created(self.ticket)
+
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["process_person_profile"] is True
+ groups = call_kwargs["properties"]["$groups"]
+ assert groups["organization"] == str(person_org.id)
+ assert groups["project"] == str(self.team.uuid)
+ assert "instance" in groups
+
+ @parameterized.expand(
+ [
+ ("user_not_found", False, False),
+ ("user_has_no_membership", True, False),
+ ]
+ )
+ @patch("products.conversations.backend.events.capture_internal")
+ @patch("products.conversations.backend.events.get_persons_by_distinct_ids")
+ def test_capture_ticket_created_no_groups_when(
+ self, _name, create_user, create_membership, mock_get_persons, mock_capture
+ ):
+ from posthog.models.person.person import Person
+
+ if create_user:
+ User.objects.create(email="[email protected]", distinct_id="customer-123")
+
+ mock_get_persons.return_value = [Person(team_id=self.team.id, is_identified=True)]
+
+ capture_ticket_created(self.ticket)
+
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["process_person_profile"] is False
+ 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"))
diff --git a/products/conversations/backend/events.py b/products/conversations/backend/events.py
index 95062c117600..b9a401870836 100644
--- a/products/conversations/backend/events.py
+++ b/products/conversations/backend/events.py
@@ -10,6 +10,7 @@ import structlog
from posthog.api.capture import capture_internal
from posthog.event_usage import groups as build_groups
+from posthog.models.organization import OrganizationMembership
from posthog.models.person.util import get_persons_by_distinct_ids
from products.conversations.backend.models import Ticket
@@ -43,8 +44,14 @@ def capture_ticket_created(ticket: Ticket) -> None:
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)
+ membership = (
+ OrganizationMembership.objects.select_related("organization")
+ .filter(user__distinct_id=ticket.distinct_id)
+ .first()
+ )
+ if membership:
+ process_person = True
+ properties["$groups"] = build_groups(membership.organization, team)
except Exception:
logger.exception("ticket_created_person_lookup_failed", team_id=team_id, ticket_id=str(ticket.id))