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(conversations): stamp SLA state on message sent events (#69345)

Abigailb088bf697c15
diff --git a/products/conversations/backend/api/tests/test_events.py b/products/conversations/backend/api/tests/test_events.py
index 38006cb51add..0583d046e0a3 100644
--- a/products/conversations/backend/api/tests/test_events.py
+++ b/products/conversations/backend/api/tests/test_events.py
@@ -1,5 +1,7 @@
import uuid
+from datetime import datetime
+from freezegun import freeze_time
from posthog.test.base import APIBaseTest, BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
from unittest.mock import patch
@@ -131,6 +133,45 @@ class TestConversationEvents(BaseTest):
assert call_kwargs["properties"]["customer_email"] == "[email protected]"
assert call_kwargs["properties"]["customer_distinct_id"] == self.ticket.distinct_id
+ @parameterized.expand(
+ [
+ (
+ "no_sla",
+ None,
+ {"sla_due_at": None, "sla_active": False, "sla_breached": False, "sla_delta_seconds": None},
+ ),
+ (
+ "on_track",
+ "2026-01-01T13:00:00+00:00",
+ {
+ "sla_due_at": "2026-01-01T13:00:00+00:00",
+ "sla_active": True,
+ "sla_breached": False,
+ "sla_delta_seconds": -3600,
+ },
+ ),
+ (
+ "breached",
+ "2026-01-01T11:00:00+00:00",
+ {
+ "sla_due_at": "2026-01-01T11:00:00+00:00",
+ "sla_active": True,
+ "sla_breached": True,
+ "sla_delta_seconds": 3600,
+ },
+ ),
+ ]
+ )
+ @patch("products.conversations.backend.events.capture_internal")
+ def test_capture_message_sent_stamps_sla_state(self, _name, sla_due_at, expected, mock_capture):
+ self.ticket.sla_due_at = datetime.fromisoformat(sla_due_at) if sla_due_at else None
+
+ with freeze_time("2026-01-01T12:00:00Z"):
+ capture_message_sent(self.ticket, "msg-123", "Hello customer", author=self.user)
+
+ properties = mock_capture.call_args.kwargs["properties"]
+ assert {key: properties[key] for key in expected} == expected
+
@parameterized.expand(
[
("capture_ticket_created", capture_ticket_created, []),
diff --git a/products/conversations/backend/events.py b/products/conversations/backend/events.py
index a25a6501f355..a628f60859ca 100644
--- a/products/conversations/backend/events.py
+++ b/products/conversations/backend/events.py
@@ -6,8 +6,11 @@ Staff/system actions use the actor's distinct_id; customer actions use the ticke
Events are sent to the customer's PostHog project via their team's API token.
"""
+from datetime import datetime
from typing import Literal
+from django.utils import timezone
+
import structlog
from posthog.api.capture import capture_internal
@@ -240,6 +243,24 @@ def _get_customer_properties(ticket: Ticket, *, include_distinct_id: bool = Fals
return properties
+def _get_sla_properties(ticket: Ticket, now: datetime) -> dict:
+ """SLA state at the moment of the event.
+
+ Stamped on events (rather than derived later) so attainment metrics reflect the
+ deadline that was in force at the time, even if the SLA is reset afterwards.
+ `sla_delta_seconds` is positive when past due, negative when time remains.
+ """
+ if ticket.sla_due_at is None:
+ return {"sla_due_at": None, "sla_active": False, "sla_breached": False, "sla_delta_seconds": None}
+ delta_seconds_float = (now - ticket.sla_due_at).total_seconds()
+ return {
+ "sla_due_at": ticket.sla_due_at.isoformat(),
+ "sla_active": True,
+ "sla_breached": delta_seconds_float > 0,
+ "sla_delta_seconds": int(delta_seconds_float),
+ }
+
+
def capture_ticket_created(ticket: Ticket) -> None:
properties = _get_ticket_base_properties(ticket)
properties.update(_get_customer_properties(ticket))
@@ -351,6 +372,7 @@ def capture_message_sent(
properties["author_type"] = "team"
properties.update(_get_actor_properties(author, "user"))
properties.update(_get_customer_properties(ticket, include_distinct_id=True))
+ properties.update(_get_sla_properties(ticket, timezone.now()))
capture_internal(
token=ticket.team.api_token,