📈(backend) capture an event when a document is created and deleted

When a document is created and deleted, we capture an event and we send
it to posthog.
This commit is contained in:
Manuel Raynaud
2026-06-01 17:41:55 +02:00
parent 5eb9c4edd6
commit d495b158c1
6 changed files with 96 additions and 28 deletions
+16
View File
@@ -67,6 +67,7 @@ from core.services.search_indexers import (
get_visited_document_ids_of,
)
from core.tasks.mail import send_ask_for_access_mail
from core.utils.analytics import PosthogEventName, posthog_capture
from core.utils.paths import filter_descendants
from core.utils.treebeard import create_tree_node_with_retry
from core.utils.users import users_sharing_documents_with
@@ -778,10 +779,18 @@ class DocumentViewSet(
role=models.RoleChoices.OWNER,
)
posthog_capture(
PosthogEventName.DOC_CREATED, self.request.user, {}, document=obj
)
def perform_destroy(self, instance):
"""Override to implement a soft delete instead of dumping the record in database."""
instance.soft_delete()
posthog_capture(
PosthogEventName.DOC_DELETED, self.request.user, {}, document=instance
)
def _can_user_edit_document(self, document_id, set_cache=False):
"""Check if the user can edit the document."""
try:
@@ -1112,6 +1121,13 @@ class DocumentViewSet(
# Set the created instance to the serializer
serializer.instance = child_document
posthog_capture(
PosthogEventName.DOC_CREATED,
self.request.user,
{"document_parent": str(document.id)},
document=child_document,
)
headers = self.get_success_headers(serializer.data)
return drf.response.Response(
serializer.data, status=status.HTTP_201_CREATED, headers=headers