Plain is headed towards 1.0! Subscribe for development updates →

plain-pageviews

Track pageviews from the client-side.

FAQs

Why not use server-side middleware?

Originally this was the idea. It turns out that tracking from the backend, while powerful, also means you have to identify all kinds of requests not to track (assets, files, API calls, etc.). In the end, a simple client-side tracking script naturally accomplishes what we're looking for in a more straightforward way.

 1from plain.staff.cards import Card
 2from plain.staff.views import (
 3    StaffModelDetailView,
 4    StaffModelListView,
 5    StaffModelViewset,
 6    register_viewset,
 7)
 8
 9from .models import Pageview
10
11
12@register_viewset
13class PageviewStaff(StaffModelViewset):
14    class ListView(StaffModelListView):
15        model = Pageview
16        nav_section = "Pageviews"
17        title = "Pageviews"
18        fields = ["user_id", "url", "timestamp", "session_key"]
19
20    class DetailView(StaffModelDetailView):
21        model = Pageview
22
23
24class UserPageviewsCard(Card):
25    title = "Recent pageviews"
26    template_name = "pageviews/card.html"
27
28    def get_template_context(self):
29        context = super().get_template_context()
30
31        context["pageviews"] = Pageview.objects.filter(
32            user_id=self.view.object.pk
33        ).order_by("-timestamp")[:50]
34
35        return context