Ajout du suivi des visites : modèle Visit, middleware de tracking, mises à jour des vues et du tableau de bord statistiques.
This commit is contained in:
parent
bec74976ba
commit
6e8a2bc287
7 changed files with 286 additions and 5 deletions
|
|
@ -2,6 +2,7 @@ from django.shortcuts import render, get_object_or_404
|
|||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.utils import timezone
|
||||
from django.db.models import Count
|
||||
from core.models import Visit
|
||||
from django.views.decorators.cache import cache_page
|
||||
from django.contrib.auth.models import User
|
||||
from courses.models import Course, Lesson
|
||||
|
|
@ -98,6 +99,39 @@ def stats_dashboard(request):
|
|||
revenus_disponibles = False
|
||||
technique_disponible = False
|
||||
|
||||
# Visites / Trafic
|
||||
period_start_date = start_dt.date()
|
||||
period_end_date = now.date()
|
||||
period_visits = Visit.objects.filter(date__gte=period_start_date, date__lte=period_end_date)
|
||||
|
||||
unique_visitors = period_visits.values('visitor_id').distinct().count()
|
||||
|
||||
earlier_visitors_qs = Visit.objects.filter(date__lt=period_start_date).values('visitor_id').distinct()
|
||||
returning_visitors = period_visits.filter(visitor_id__in=earlier_visitors_qs).values('visitor_id').distinct().count()
|
||||
|
||||
converted_visitors = (
|
||||
period_visits
|
||||
.filter(became_user_at__isnull=False, became_user_at__date__gte=period_start_date, became_user_at__date__lte=period_end_date)
|
||||
.values('visitor_id').distinct().count()
|
||||
)
|
||||
|
||||
top_sources_qs = (
|
||||
period_visits
|
||||
.values('source')
|
||||
.annotate(c=Count('visitor_id', distinct=True))
|
||||
.order_by('-c')
|
||||
)
|
||||
top_countries_qs = (
|
||||
period_visits
|
||||
.exclude(country='')
|
||||
.values('country')
|
||||
.annotate(c=Count('visitor_id', distinct=True))
|
||||
.order_by('-c')
|
||||
)
|
||||
|
||||
top_sources_table = [(row['source'] or 'Direct/Unknown', row['c']) for row in top_sources_qs[:10]]
|
||||
top_countries_table = [(row['country'], row['c']) for row in top_countries_qs[:10]]
|
||||
|
||||
# Helper pour avoir toutes les dates de la période et remplir les trous
|
||||
def build_series_dict(qs, date_key='day', count_key='c'):
|
||||
counts = {str(item[date_key]): item[count_key] for item in qs}
|
||||
|
|
@ -131,6 +165,9 @@ def stats_dashboard(request):
|
|||
'total_users': total_users,
|
||||
'new_users_period': sum(values_new_users),
|
||||
'active_users_period': active_users_count,
|
||||
'unique_visitors': unique_visitors,
|
||||
'returning_visitors': returning_visitors,
|
||||
'converted_visitors': converted_visitors,
|
||||
'total_courses': total_courses,
|
||||
'courses_enabled': total_courses_enabled,
|
||||
'total_lessons': total_lessons,
|
||||
|
|
@ -153,6 +190,8 @@ def stats_dashboard(request):
|
|||
# Tables
|
||||
'new_users_table': new_users_table,
|
||||
'new_courses_table': new_courses_table,
|
||||
'top_sources_table': top_sources_table,
|
||||
'top_countries_table': top_countries_table,
|
||||
}
|
||||
|
||||
# Sérialisation JSON pour Chart.js
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue