From 0df2d753c4556fc25294fa9e1e1787a6765411db Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jun 2026 16:15:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=88(backend)=20capture=20an=20event=20?= =?UTF-8?q?when=20a=20comment=20is=20created?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a new comment is created, we capture an event related to this comment creation. --- src/backend/core/api/viewsets.py | 9 ++++- .../documents/test_api_documents_comments.py | 36 ++++++++++++++----- src/backend/core/utils/analytics.py | 3 ++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 30d7b7551..e6eab55ef 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -3206,7 +3206,14 @@ class CommentViewSet( def perform_create(self, serializer): """Attach the request user as the comment author.""" user = self.request.user if self.request.user.is_authenticated else None - serializer.save(user=user) + comment = serializer.save(user=user) + + posthog_capture( + PosthogEventName.COMMENT_CREATED, + user, + {"comment_id": str(comment.id), "thread_id": str(comment.thread_id)}, + document=self.get_document_or_404(), + ) @drf.decorators.action( detail=True, diff --git a/src/backend/core/tests/documents/test_api_documents_comments.py b/src/backend/core/tests/documents/test_api_documents_comments.py index 6ae2c72f8..ec681f1ed 100644 --- a/src/backend/core/tests/documents/test_api_documents_comments.py +++ b/src/backend/core/tests/documents/test_api_documents_comments.py @@ -1,6 +1,7 @@ """Test API for comments on documents.""" import random +from unittest import mock from django.contrib.auth.models import AnonymousUser @@ -8,6 +9,7 @@ import pytest from rest_framework.test import APIClient from core import factories, models +from core.utils.analytics import PosthogEventName pytestmark = pytest.mark.django_db @@ -184,12 +186,21 @@ def test_create_comment_anonymous_user_public_document(): ) thread = factories.ThreadFactory(document=document) client = APIClient() - response = client.post( - f"/api/v1.0/documents/{document.id!s}/threads/{thread.id!s}/comments/", - {"body": "test"}, - ) + with mock.patch("core.api.viewsets.posthog_capture") as mock_capture: + response = client.post( + f"/api/v1.0/documents/{document.id!s}/threads/{thread.id!s}/comments/", + {"body": "test"}, + ) assert response.status_code == 201 + # The comment creation should be tracked in PostHog + mock_capture.assert_called_once_with( + PosthogEventName.COMMENT_CREATED, + None, + {"comment_id": response.json()["id"], "thread_id": str(thread.id)}, + document=document, + ) + assert response.json() == { "id": str(response.json()["id"]), "body": "test", @@ -231,12 +242,21 @@ def test_create_comment_authenticated_user_accessible_document(): thread = factories.ThreadFactory(document=document) client = APIClient() client.force_login(user) - response = client.post( - f"/api/v1.0/documents/{document.id!s}/threads/{thread.id!s}/comments/", - {"body": "test"}, - ) + with mock.patch("core.api.viewsets.posthog_capture") as mock_capture: + response = client.post( + f"/api/v1.0/documents/{document.id!s}/threads/{thread.id!s}/comments/", + {"body": "test"}, + ) assert response.status_code == 201 + # The comment creation should be tracked in PostHog + mock_capture.assert_called_once_with( + PosthogEventName.COMMENT_CREATED, + user, + {"comment_id": response.json()["id"], "thread_id": str(thread.id)}, + document=document, + ) + assert response.json() == { "id": str(response.json()["id"]), "body": "test", diff --git a/src/backend/core/utils/analytics.py b/src/backend/core/utils/analytics.py index a867d8dd1..335ac267b 100644 --- a/src/backend/core/utils/analytics.py +++ b/src/backend/core/utils/analytics.py @@ -29,6 +29,9 @@ class PosthogEventName(StrEnum): # Thread THREAD_CREATED = "thread_created" + # Comment + COMMENT_CREATED = "comment_created" + # User USER_LOGIN = "user_login"