Files
lasuite-docs/src/backend/core/utils/analytics.py
T
Manuel Raynaud 5eb9c4edd6 📈(backend) create a utils to capture event with posthog
We want to capture event with posthog. We created a utils for this
reposible to send the event or not if posthog is configured.
2026-06-01 17:41:55 +02:00

48 lines
1.0 KiB
Python

"""
PostHog utilities
"""
from enum import StrEnum
from django.conf import settings
import posthog
from core.models import User
class PosthogEventName(StrEnum):
"""Posthog event name enum"""
DOC_CREATED = "doc_created"
DOC_DELETED = "doc_deleted"
def posthog_capture(
event_name: PosthogEventName,
user: User | None,
properties: dict | None = None,
**kwargs,
):
"""Capture an event with PostHog."""
if settings.POSTHOG_KEY:
if properties is None:
properties = {}
properties = properties.copy()
document = kwargs.get("document")
if document:
properties.update(
{
"document_id": str(document.id),
"document_title": document.title,
"document_depth": document.depth,
"document_path": document.path,
}
)
posthog.capture(
event_name,
distinct_id=getattr(user, "email", None),
properties=properties,
)