diff --git a/lacommunaute/forum_stats/models.py b/lacommunaute/forum_stats/models.py index 496ac70c7..ea5db7041 100644 --- a/lacommunaute/forum_stats/models.py +++ b/lacommunaute/forum_stats/models.py @@ -19,3 +19,9 @@ class Meta: def __str__(self): return f"{self.name} - {self.date} - {self.period}" + + def get_month_stats(self): + qs = Stat.objects.filter(date__year=self.date.year, date__month=self.date.month, period=Period.MONTH).values( + "name", "value" + ) + return {stat["name"]: stat["value"] for stat in qs} diff --git a/lacommunaute/pages/views.py b/lacommunaute/pages/views.py index 2f66ecc06..7b7aac7f1 100644 --- a/lacommunaute/pages/views.py +++ b/lacommunaute/pages/views.py @@ -13,6 +13,7 @@ from lacommunaute.forum_conversation.models import Topic from lacommunaute.forum_stats.models import Stat from lacommunaute.utils.json import extract_values_in_list +from lacommunaute.utils.math import percent logger = logging.getLogger(__name__) @@ -25,6 +26,21 @@ def contact(request): class StatistiquesPageView(TemplateView): template_name = "pages/statistiques.html" + def get_funnel_data(self): + current_period = Stat.objects.filter(period="month").order_by("-date").first() + current_stats = Stat.get_month_stats(current_period) + current_stats["nb_uniq_visitors"] = current_stats.get("nb_uniq_visitors", 0) + current_stats["nb_uniq_active_visitors"] = current_stats.get("nb_uniq_active_visitors", 0) + current_stats["nb_uniq_engaged_visitors"] = current_stats.get("nb_uniq_engaged_visitors", 0) + current_stats["activation_percent"] = percent( + current_stats["nb_uniq_active_visitors"], current_stats["nb_uniq_visitors"] + ) + current_stats["engagement_percent"] = percent( + current_stats["nb_uniq_engaged_visitors"], current_stats["nb_uniq_visitors"] + ) + current_stats["period"] = current_period.date.strftime("%B %Y") + return current_stats + def get_context_data(self, **kwargs): indicator_names = [ "nb_uniq_engaged_visitors", @@ -40,6 +56,7 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["stats"] = extract_values_in_list(datas, indicator_names) + context = {**context, **self.get_funnel_data()} return context diff --git a/lacommunaute/templates/pages/statistiques.html b/lacommunaute/templates/pages/statistiques.html index 3158a82c6..80e6beb4d 100644 --- a/lacommunaute/templates/pages/statistiques.html +++ b/lacommunaute/templates/pages/statistiques.html @@ -30,12 +30,12 @@

Acquisition

Entonnoir d'aquisition des utilisateurs - 3192 - -16%/mois précédent - 3067 - 96% des Utilisateurs
+1,4%/mois précédent
- 1539 - 50.2% des Util. Actifs
+3%/mois préc.
+ {{ nb_uniq_visitors }} + -XXX%/mois précédent + {{ nb_uniq_active_visitors }} + {{ activation_percent }}% des Utilisateurs
XXX%/mois précédent
+ {{nb_uniq_engaged_visitors }} + {{ engagement_percent }}% des Util. Actifs
+XXX%/mois préc.
@@ -48,7 +48,7 @@

Utilisateurs

visiteurs uniques ayant au moins accédé à l
diff --git a/lacommunaute/utils/math.py b/lacommunaute/utils/math.py new file mode 100644 index 000000000..3550d5b60 --- /dev/null +++ b/lacommunaute/utils/math.py @@ -0,0 +1,4 @@ +def percent(a, b): + if b == 0: + return 0 + return round((a / b) * 100, 2)