Self-hostable website analytics on Django: a straightforward collector API, dashboards, a world map, and PDF reports.
analyticsdjangodockerhandcodedpythonself-hostedsqliteviteweb-analytics
1import uuid
2
3from django.db import models
4from django.db.models import Count, Q
5from django.contrib.auth.models import AbstractUser
6
7
8class User(AbstractUser):
9 id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
10
11 def __str__(self):
12 return self.username
13
14 def _event_totals(self):
15 # One query rolls up everything the profile page displays.
16 if not hasattr(self, "_cached_event_totals"):
17 self._cached_event_totals = self.properties.aggregate(
18 total_properties=Count("id", distinct=True),
19 total_events=Count("events"),
20 total_page_views=Count("events", filter=Q(events__event="page_view")),
21 total_session_starts=Count("events", filter=Q(events__event="session_start")),
22 )
23 return self._cached_event_totals
24
25 @property
26 def total_properties(self):
27 return self._event_totals()["total_properties"]
28
29 @property
30 def total_events(self):
31 return self._event_totals()["total_events"]
32
33 @property
34 def total_page_views(self):
35 return self._event_totals()["total_page_views"]
36
37 @property
38 def total_session_starts(self):
39 return self._event_totals()["total_session_starts"]