Skip to content

Commit

Permalink
Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Oct 24, 2023
1 parent 143cd2c commit ec4bfc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 2 additions & 0 deletions totem/onboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))


Expand Down
42 changes: 29 additions & 13 deletions totem/users/analytics.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 3 additions & 1 deletion totem/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ec4bfc1

Please sign in to comment.