feat: add posthog signup and signin events, enable backend posthog events for oss version (#249)

This commit is contained in:
Sabiha Khan 2026-04-24 12:02:52 +05:30 committed by GitHub
parent 9298116887
commit f7c1f63e1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 82 additions and 35 deletions

View file

@ -8,9 +8,11 @@ from pydantic import ValidationError
from api.constants import AUTH_PROVIDER, DOGRAH_MPS_SECRET_KEY, MPS_API_URL
from api.db import db_client
from api.db.models import UserModel
from api.enums import PostHogEvent
from api.schemas.user_configuration import UserConfiguration
from api.services.auth.stack_auth import stackauth
from api.services.configuration.registry import ServiceProviders
from api.services.posthog_client import capture_event
from api.utils.auth import decode_jwt_token
@ -54,7 +56,7 @@ async def get_user(
# ------------------------------------------------------------------
try:
user_model = await db_client.get_or_create_user_by_provider_id(stack_user["id"])
user_model, user_was_created = await db_client.get_or_create_user_by_provider_id(stack_user["id"])
# Sync email from Stack Auth if available and not already set
stack_email = stack_user.get("primary_email_verified") and stack_user.get(
@ -63,6 +65,15 @@ async def get_user(
if stack_email and user_model.email != stack_email:
await db_client.update_user_email(user_model.id, stack_email)
user_model.email = stack_email
if user_was_created:
capture_event(
distinct_id=str(stack_user["id"]),
event=PostHogEvent.SIGNED_UP,
properties={
"auth_provider": "stack",
},
)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error while creating user from database {e}"

View file

@ -1,7 +1,7 @@
from loguru import logger
from posthog import Posthog
from api.constants import POSTHOG_API_KEY, POSTHOG_HOST
from api.constants import ENABLE_TELEMETRY, POSTHOG_API_KEY, POSTHOG_HOST
_posthog_client: Posthog | None = None
@ -9,7 +9,7 @@ _posthog_client: Posthog | None = None
def get_posthog() -> Posthog | None:
"""Return the lazily-initialised PostHog client, or None if not configured."""
global _posthog_client
if _posthog_client is None and POSTHOG_API_KEY:
if _posthog_client is None and POSTHOG_API_KEY and ENABLE_TELEMETRY:
_posthog_client = Posthog(POSTHOG_API_KEY, host=POSTHOG_HOST)
return _posthog_client