diff --git a/totem/onboard/views.py b/totem/onboard/views.py index a6109ffc..c07a9263 100644 --- a/totem/onboard/views.py +++ b/totem/onboard/views.py @@ -5,6 +5,7 @@ from django.http import HttpRequest from django.shortcuts import redirect, render +from totem.users import analytics from totem.users.models import User from totem.utils.slack import notify_slack from totem.utils.utils import full_url @@ -36,6 +37,7 @@ def save(self, user: User, onboard: OnboardModel): setattr(onboard, key, value) onboard.onboarded = True onboard.save() + analytics.user_onboarded(user) _notify_slack(user.name, full_url(user.get_admin_url())) diff --git a/totem/users/analytics.py b/totem/users/analytics.py index cc87b122..4d14c21e 100644 --- a/totem/users/analytics.py +++ b/totem/users/analytics.py @@ -1,24 +1,40 @@ +from typing import TYPE_CHECKING + from django.conf import settings from posthog import Posthog +if TYPE_CHECKING: + from totem.users.models import User + _posthog = None -def _get_posthog(): +def _get_posthog() -> Posthog | None: + if settings.DEBUG: + return + if not settings.POSTHOG_API_KEY: + return global _posthog _posthog = Posthog(project_api_key=settings.POSTHOG_API_KEY, host="https://app.posthog.com") return _posthog -def identify_user(user): - if settings.DEBUG: - return - if not settings.POSTHOG_API_KEY: - return - _get_posthog().identify( - user.analytics_id(), - { - "environment": settings.ENVIRONMENT_NAME.lower(), - "staff": user.is_staff, - }, - ) +def identify_user(user: "User"): + if p := _get_posthog(): + p.identify( + user.analytics_id(), + { + "environment": settings.ENVIRONMENT_NAME.lower(), + "staff": user.is_staff, + }, + ) + + +def user_signed_up(user: "User"): + if p := _get_posthog(): + p.capture(user.analytics_id(), "user_signed_up") + + +def user_onboarded(user: "User"): + if p := _get_posthog(): + p.capture(user.analytics_id(), "user_onboarded") diff --git a/totem/users/views.py b/totem/users/views.py index 67416971..533ce2a5 100644 --- a/totem/users/views.py +++ b/totem/users/views.py @@ -13,6 +13,7 @@ from totem.circles.filters import upcoming_events_user_can_attend from totem.email import emails +from . import analytics from .forms import LoginForm from .models import User @@ -66,7 +67,7 @@ def _message(self, email: str): def get(self, request: HttpRequest, *args, **kwargs): response = super().get(request, *args, **kwargs) # Make sure htmx redirects to the login page with a full refresh - response.headers["HX-Redirect"] = request.get_full_path() + response.headers["HX-Redirect"] = request.get_full_path() # type: ignore return response def form_valid(self, form): @@ -99,6 +100,7 @@ def login(email: str, request, after_login_url: str | None = None, mobile: bool django_login(request, user, backend="django.contrib.auth.backends.ModelBackend") # TODO make new welcome email. This one expires after 30min and doesn't make sense for a new user. # emails.send_new_login_email(user.email, url) + analytics.user_signed_up(user) else: emails.send_returning_login_email(user.email, url)