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(conversations): attribute ticket events to the actor (#62441)

Alex V5dadddb543fb
diff --git a/products/conversations/backend/api/tests/test_events.py b/products/conversations/backend/api/tests/test_events.py
index 0dbe9da53a1b..fa6a64ff8b2e 100644
--- a/products/conversations/backend/api/tests/test_events.py
+++ b/products/conversations/backend/api/tests/test_events.py
@@ -50,57 +50,68 @@ class TestConversationEvents(BaseTest):
@patch("products.conversations.backend.events.capture_internal_routed")
def test_capture_ticket_status_changed_uses_team_token(self, mock_capture):
- capture_ticket_status_changed(self.ticket, "new", "pending")
+ capture_ticket_status_changed(self.ticket, "new", "pending", actor=self.user, actor_type="user")
mock_capture.assert_called_once()
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["token"] == self.team.api_token
assert call_kwargs["event_name"] == "$conversation_ticket_status_changed"
assert call_kwargs["event_source"] == EVENT_SOURCE
- assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["distinct_id"] == self.user.distinct_id
assert call_kwargs["properties"]["old_status"] == "new"
assert call_kwargs["properties"]["new_status"] == "pending"
+ assert call_kwargs["properties"]["actor_type"] == "user"
+ assert call_kwargs["properties"]["actor_id"] == self.user.id
+ assert call_kwargs["properties"]["actor_email"] == self.user.email
@patch("products.conversations.backend.events.capture_internal_routed")
def test_capture_ticket_priority_changed_uses_team_token(self, mock_capture):
- capture_ticket_priority_changed(self.ticket, None, "high")
+ capture_ticket_priority_changed(self.ticket, None, "high", actor=self.user, actor_type="user")
mock_capture.assert_called_once()
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["token"] == self.team.api_token
assert call_kwargs["event_name"] == "$conversation_ticket_priority_changed"
assert call_kwargs["event_source"] == EVENT_SOURCE
- assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["distinct_id"] == self.user.distinct_id
assert call_kwargs["properties"]["old_priority"] is None
assert call_kwargs["properties"]["new_priority"] == "high"
+ assert call_kwargs["properties"]["actor_type"] == "user"
+ assert call_kwargs["properties"]["actor_id"] == self.user.id
+ assert call_kwargs["properties"]["actor_email"] == self.user.email
@patch("products.conversations.backend.events.capture_internal_routed")
def test_capture_ticket_assigned_uses_team_token(self, mock_capture):
- capture_ticket_assigned(self.ticket, "user", "123")
+ capture_ticket_assigned(self.ticket, "user", "123", actor=self.user, actor_type="user")
mock_capture.assert_called_once()
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["token"] == self.team.api_token
assert call_kwargs["event_name"] == "$conversation_ticket_assigned"
assert call_kwargs["event_source"] == EVENT_SOURCE
- assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["distinct_id"] == self.user.distinct_id
assert call_kwargs["properties"]["assignee_type"] == "user"
assert call_kwargs["properties"]["assignee_id"] == "123"
+ assert call_kwargs["properties"]["actor_type"] == "user"
+ assert call_kwargs["properties"]["actor_id"] == self.user.id
+ assert call_kwargs["properties"]["actor_email"] == self.user.email
@patch("products.conversations.backend.events.capture_internal_routed")
def test_capture_message_sent_uses_team_token(self, mock_capture):
- capture_message_sent(self.ticket, "msg-123", "Hello customer", 42)
+ capture_message_sent(self.ticket, "msg-123", "Hello customer", author=self.user)
mock_capture.assert_called_once()
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["token"] == self.team.api_token
assert call_kwargs["event_name"] == "$conversation_message_sent"
assert call_kwargs["event_source"] == EVENT_SOURCE
- assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["distinct_id"] == self.user.distinct_id
assert call_kwargs["properties"]["message_id"] == "msg-123"
assert call_kwargs["properties"]["message_content"] == "Hello customer"
assert call_kwargs["properties"]["author_type"] == "team"
- assert call_kwargs["properties"]["author_id"] == 42
+ assert call_kwargs["properties"]["actor_type"] == "user"
+ assert call_kwargs["properties"]["actor_id"] == self.user.id
+ assert call_kwargs["properties"]["actor_email"] == self.user.email
@patch("products.conversations.backend.events.capture_internal_routed")
def test_capture_message_received_uses_team_token(self, mock_capture):
@@ -134,7 +145,7 @@ class TestConversationEvents(BaseTest):
[None, "high"],
),
("capture_ticket_assigned", capture_ticket_assigned, "$conversation_ticket_assigned", ["user", "123"]),
- ("capture_message_sent", capture_message_sent, "$conversation_message_sent", ["msg-id", "content", 1]),
+ ("capture_message_sent", capture_message_sent, "$conversation_message_sent", ["msg-id", "content"]),
(
"capture_message_received",
capture_message_received,
@@ -160,7 +171,7 @@ class TestConversationEvents(BaseTest):
@patch("products.conversations.backend.events.capture_internal_routed")
def test_message_content_truncated_to_1000_chars(self, mock_capture):
long_content = "x" * 1500
- capture_message_sent(self.ticket, "msg-id", long_content, 1)
+ capture_message_sent(self.ticket, "msg-id", long_content, author=self.user)
call_kwargs = mock_capture.call_args.kwargs
assert len(call_kwargs["properties"]["message_content"]) == 1000
@@ -531,3 +542,27 @@ class TestConversationEvents(BaseTest):
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_routed")
+ def test_capture_ticket_status_changed_system_actor_uses_customer_distinct_id(self, mock_capture):
+ """System actions (e.g. snooze wake) use the customer's distinct_id, not an actor's."""
+ capture_ticket_status_changed(self.ticket, "on_hold", "open", actor_type="system")
+
+ mock_capture.assert_called_once()
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["properties"]["actor_type"] == "system"
+ assert call_kwargs["properties"]["actor_id"] is None
+ assert call_kwargs["properties"]["actor_email"] is None
+
+ @patch("products.conversations.backend.events.capture_internal_routed")
+ def test_capture_ticket_status_changed_external_actor_uses_customer_distinct_id(self, mock_capture):
+ """External actions (e.g. GitHub sync) use the customer's distinct_id."""
+ capture_ticket_status_changed(self.ticket, "open", "resolved", actor_type="external")
+
+ mock_capture.assert_called_once()
+ call_kwargs = mock_capture.call_args.kwargs
+ assert call_kwargs["distinct_id"] == self.ticket.distinct_id
+ assert call_kwargs["properties"]["actor_type"] == "external"
+ assert call_kwargs["properties"]["actor_id"] is None
+ assert call_kwargs["properties"]["actor_email"] is None
diff --git a/products/conversations/backend/api/tests/test_snooze.py b/products/conversations/backend/api/tests/test_snooze.py
index 6586c13fe2dd..d7a5e7fbbaa3 100644
--- a/products/conversations/backend/api/tests/test_snooze.py
+++ b/products/conversations/backend/api/tests/test_snooze.py
@@ -300,7 +300,7 @@ class TestWakeSnoozedTickets(BaseTest):
ticket.refresh_from_db()
self.assertEqual(ticket.status, Status.OPEN)
self.assertIsNone(ticket.snoozed_until)
- mock_capture.assert_called_once_with(ticket, Status.ON_HOLD, Status.OPEN)
+ mock_capture.assert_called_once_with(ticket, Status.ON_HOLD, Status.OPEN, actor_type="system")
@patch("products.conversations.backend.tasks.capture_ticket_status_changed")
def test_clears_snooze_but_preserves_resolved_status(self, mock_capture):
diff --git a/products/conversations/backend/api/tickets.py b/products/conversations/backend/api/tickets.py
index 955a49b9edc4..e014d69593e9 100644
--- a/products/conversations/backend/api/tickets.py
+++ b/products/conversations/backend/api/tickets.py
@@ -679,10 +679,12 @@ class TicketViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, viewsets.Mod
try:
if status_changed:
- capture_ticket_status_changed(instance, old_status, new_status)
+ capture_ticket_status_changed(instance, old_status, new_status, actor=request.user, actor_type="user")
if priority_changed:
- capture_ticket_priority_changed(instance, old_priority, new_priority)
+ capture_ticket_priority_changed(
+ instance, old_priority, new_priority, actor=request.user, actor_type="user"
+ )
except Exception as e:
capture_exception(e, {"ticket_id": str(instance.id)})
@@ -775,7 +777,7 @@ class TicketViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, viewsets.Mod
event-tracking logic in one place.
"""
try:
- capture_ticket_status_changed(ticket, old_status, new_status)
+ capture_ticket_status_changed(ticket, old_status, new_status, actor=request.user, actor_type="user")
except Exception as e:
capture_exception(e, {"ticket_id": str(ticket.id)})
@@ -1141,6 +1143,6 @@ def assign_ticket(ticket: Ticket, assignee, organization, user, team_id, was_imp
else:
assignee_type = None
assignee_id = None
- capture_ticket_assigned(ticket, assignee_type, assignee_id)
+ capture_ticket_assigned(ticket, assignee_type, assignee_id, actor=user, actor_type="user")
except Exception as e:
capture_exception(e, {"ticket_id": str(ticket.id)})
diff --git a/products/conversations/backend/events.py b/products/conversations/backend/events.py
index 8cc1c4a855ea..4825293b52a3 100644
--- a/products/conversations/backend/events.py
+++ b/products/conversations/backend/events.py
@@ -2,10 +2,12 @@
Emit PostHog analytics events for ticket and message state changes.
These events power workflow triggers (e.g. "if ticket pending for X days, resolve it").
-All events use the ticket's distinct_id so they're tied to the customer person.
+Staff/system actions use the actor's distinct_id; customer actions use the ticket's distinct_id.
Events are sent to the customer's PostHog project via their team's API token.
"""
+from typing import Literal
+
import structlog
from posthog.api.capture_dispatch import capture_internal_routed
@@ -13,6 +15,7 @@ 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 posthog.models.team import Team
+from posthog.models.user import User
from products.conversations.backend.models import Ticket
from products.conversations.backend.models.constants import Channel
@@ -22,6 +25,33 @@ logger = structlog.get_logger(__name__)
EVENT_SOURCE = "conversations_events"
+ActorType = Literal["user", "system", "external", "customer"]
+
+
+def _get_actor_properties(actor: User | None, actor_type: ActorType) -> dict:
+ """Build actor-related properties for an event."""
+ return {
+ "actor_type": actor_type,
+ "actor_id": actor.id if actor else None,
+ "actor_email": actor.email if actor else None,
+ }
+
+
+def _get_actor_distinct_id(
+ ticket: Ticket,
+ actor: User | None,
+ actor_type: ActorType,
+) -> str:
+ """Determine the distinct_id based on who performed the action.
+
+ User actions are attributed to the staff member.
+ System/external/customer actions are attributed to the customer.
+ """
+ if actor_type == "user" and actor and actor.distinct_id:
+ return actor.distinct_id
+ return ticket.distinct_id or ticket.channel_source or "unknown"
+
+
# Channels whose customer email is tied to a provider-verified identity and is therefore safe
# to use for organization attribution.
_EMAIL_FALLBACK_CHANNELS = frozenset({Channel.EMAIL.value, Channel.SLACK.value, Channel.TEAMS.value})
@@ -112,64 +142,90 @@ def capture_ticket_created(ticket: Ticket) -> None:
)
-def capture_ticket_status_changed(ticket: Ticket, old_status: str, new_status: str) -> None:
+def capture_ticket_status_changed(
+ ticket: Ticket,
+ old_status: str,
+ new_status: str,
+ actor: User | None = None,
+ actor_type: ActorType = "system",
+) -> None:
properties = _get_ticket_base_properties(ticket)
properties["old_status"] = old_status
properties["new_status"] = new_status
+ properties.update(_get_actor_properties(actor, actor_type))
capture_internal_routed(
token=ticket.team.api_token,
event_name="$conversation_ticket_status_changed",
event_source=EVENT_SOURCE,
- distinct_id=ticket.distinct_id or ticket.channel_source or "unknown",
+ distinct_id=_get_actor_distinct_id(ticket, actor, actor_type),
timestamp=None,
properties=properties,
)
-def capture_ticket_priority_changed(ticket: Ticket, old_priority: str | None, new_priority: str | None) -> None:
+def capture_ticket_priority_changed(
+ ticket: Ticket,
+ old_priority: str | None,
+ new_priority: str | None,
+ actor: User | None = None,
+ actor_type: ActorType = "system",
+) -> None:
properties = _get_ticket_base_properties(ticket)
properties["old_priority"] = old_priority
properties["new_priority"] = new_priority
+ properties.update(_get_actor_properties(actor, actor_type))
capture_internal_routed(
token=ticket.team.api_token,
event_name="$conversation_ticket_priority_changed",
event_source=EVENT_SOURCE,
- distinct_id=ticket.distinct_id or ticket.channel_source or "unknown",
+ distinct_id=_get_actor_distinct_id(ticket, actor, actor_type),
timestamp=None,
properties=properties,
)
-def capture_ticket_assigned(ticket: Ticket, assignee_type: str | None, assignee_id: str | None) -> None:
+def capture_ticket_assigned(
+ ticket: Ticket,
+ assignee_type: str | None,
+ assignee_id: str | None,
+ actor: User | None = None,
+ actor_type: ActorType = "system",
+) -> None:
properties = _get_ticket_base_properties(ticket)
properties["assignee_type"] = assignee_type
properties["assignee_id"] = assignee_id
+ properties.update(_get_actor_properties(actor, actor_type))
capture_internal_routed(
token=ticket.team.api_token,
event_name="$conversation_ticket_assigned",
event_source=EVENT_SOURCE,
- distinct_id=ticket.distinct_id or ticket.channel_source or "unknown",
+ distinct_id=_get_actor_distinct_id(ticket, actor, actor_type),
timestamp=None,
properties=properties,
)
-def capture_message_sent(ticket: Ticket, message_id: str, message_content: str, author_id: int | None) -> None:
+def capture_message_sent(
+ ticket: Ticket,
+ message_id: str,
+ message_content: str,
+ author: User | None = None,
+) -> None:
"""Team member sent a message on a ticket."""
properties = _get_ticket_base_properties(ticket)
properties["message_id"] = message_id
properties["message_content"] = (message_content or "")[:1000]
properties["author_type"] = "team"
- properties["author_id"] = author_id
+ properties.update(_get_actor_properties(author, "user"))
capture_internal_routed(
token=ticket.team.api_token,
event_name="$conversation_message_sent",
event_source=EVENT_SOURCE,
- distinct_id=ticket.distinct_id or ticket.channel_source or "unknown",
+ distinct_id=_get_actor_distinct_id(ticket, author, "user"),
timestamp=None,
properties=properties,
)
diff --git a/products/conversations/backend/signals.py b/products/conversations/backend/signals.py
index 1969b76f91c3..e735896f522b 100644
--- a/products/conversations/backend/signals.py
+++ b/products/conversations/backend/signals.py
@@ -127,16 +127,19 @@ def update_ticket_on_message(sender, instance: Comment, created: bool, **kwargs)
# Customer-facing analytics (to customer's project)
if is_team_message:
- capture_message_sent(ticket, comment_id, content or "", created_by_id)
+ author = User.objects.filter(id=created_by_id).first() if created_by_id else None
+ capture_message_sent(ticket, comment_id, content or "", author=author)
else:
+ author = None
capture_message_received(ticket, comment_id, content or "")
# Internal analytics (PostHog tracking its own usage)
props = {"channel_source": ticket.channel_source}
- if is_team_message and created_by_id:
- user = User.objects.filter(id=created_by_id).first()
- if user:
- report_user_action(user, "support message sent", props, team=ticket.team)
+ if is_team_message:
+ if author:
+ report_user_action(author, "support message sent", props, team=ticket.team)
+ else:
+ report_team_action(ticket.team, "support message sent", props)
else:
report_team_action(ticket.team, "support message received", props)
# Send email notification on first customer message (i.e. new ticket)
diff --git a/products/conversations/backend/tasks.py b/products/conversations/backend/tasks.py
index 75de1fb8be35..f95a645d1dc3 100644
--- a/products/conversations/backend/tasks.py
+++ b/products/conversations/backend/tasks.py
@@ -826,7 +826,7 @@ def wake_snoozed_tickets() -> None:
ticket.status = Status.OPEN
ticket.save(update_fields=["status", "snoozed_until", "updated_at"])
try:
- capture_ticket_status_changed(ticket, old_status, Status.OPEN)
+ capture_ticket_status_changed(ticket, old_status, Status.OPEN, actor_type="system")
except Exception:
logger.exception("wake_snoozed_ticket_event_failed", ticket_id=str(ticket.id))
else:
@@ -1024,7 +1024,7 @@ def _handle_github_issue_event(team: Team, repo: str, action: str, payload: dict
existing.status = new_status
existing.save(update_fields=["status", "updated_at"])
try:
- capture_ticket_status_changed(existing, old_status, new_status)
+ capture_ticket_status_changed(existing, old_status, new_status, actor_type="external")
except Exception:
logger.exception("github_event_status_change_event_failed", ticket_id=str(existing.id))