276 lines
13 KiB
PHP
276 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* BackOffice — Analytics avancé
|
|
* Portfolio Tony AVERTY
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Security.php';
|
|
Security::bootstrap(true, ['no_store' => true, 'allow_analytics_cdn' => true]);
|
|
Security::requireAdmin();
|
|
|
|
require_once __DIR__ . '/../config/database.php';
|
|
$db = getDatabase();
|
|
|
|
$period = (int) ($_GET['period'] ?? 30);
|
|
$period = in_array($period, [7, 30, 90]) ? $period : 30;
|
|
|
|
// ── Stats globales ────────────────────────────────────────
|
|
$totalVisits = $db->prepare("SELECT COUNT(*) FROM visits WHERE created_at >= NOW() - INTERVAL :d DAY");
|
|
$totalVisits->execute(['d' => $period]);
|
|
$totalVisits = $totalVisits->fetchColumn();
|
|
|
|
$uniqueSessions = $db->prepare("SELECT COUNT(DISTINCT session_id) FROM visits WHERE created_at >= NOW() - INTERVAL :d DAY");
|
|
$uniqueSessions->execute(['d' => $period]);
|
|
$uniqueSessions = $uniqueSessions->fetchColumn();
|
|
|
|
$avgDuration = $db->prepare("SELECT ROUND(AVG(duration_s)) FROM visits WHERE duration_s IS NOT NULL AND duration_s < 3600 AND created_at >= NOW() - INTERVAL :d DAY");
|
|
$avgDuration->execute(['d' => $period]);
|
|
$avgDuration = (int) $avgDuration->fetchColumn();
|
|
|
|
// ── Visites par jour ──────────────────────────────────────
|
|
$visitsByDay = $db->prepare(
|
|
"SELECT DATE(created_at) as d, COUNT(*) as c
|
|
FROM visits WHERE created_at >= NOW() - INTERVAL :p DAY
|
|
GROUP BY DATE(created_at) ORDER BY d ASC"
|
|
);
|
|
$visitsByDay->execute(['p' => $period]);
|
|
$visitsByDay = $visitsByDay->fetchAll();
|
|
|
|
$chartDays = [];
|
|
$chartVisits = [];
|
|
for ($i = $period - 1; $i >= 0; $i--) {
|
|
$date = date('Y-m-d', strtotime("-{$i} days"));
|
|
$chartDays[] = date('d/m', strtotime($date));
|
|
$chartVisits[] = 0;
|
|
}
|
|
foreach ($visitsByDay as $row) {
|
|
$idx = array_search(date('d/m', strtotime($row['d'])), $chartDays);
|
|
if ($idx !== false) $chartVisits[$idx] = (int) $row['c'];
|
|
}
|
|
|
|
// ── Répartition devices ───────────────────────────────────
|
|
$deviceRows = $db->prepare("SELECT device_type, COUNT(*) as c FROM visits WHERE created_at >= NOW() - INTERVAL :d DAY GROUP BY device_type");
|
|
$deviceRows->execute(['d' => $period]);
|
|
$deviceData = ['desktop' => 0, 'mobile' => 0, 'tablet' => 0];
|
|
foreach ($deviceRows->fetchAll() as $r) { $deviceData[$r['device_type']] = (int) $r['c']; }
|
|
|
|
// ── Pages les plus visitées ───────────────────────────────
|
|
$topPages = $db->prepare(
|
|
"SELECT page, COUNT(*) as c FROM visits
|
|
WHERE created_at >= NOW() - INTERVAL :d DAY
|
|
GROUP BY page ORDER BY c DESC LIMIT 10"
|
|
);
|
|
$topPages->execute(['d' => $period]);
|
|
$topPages = $topPages->fetchAll();
|
|
|
|
// ── Referrers ─────────────────────────────────────────────
|
|
$topReferrers = $db->prepare(
|
|
"SELECT referrer, COUNT(*) as c FROM visits
|
|
WHERE referrer IS NOT NULL AND referrer != ''
|
|
AND created_at >= NOW() - INTERVAL :d DAY
|
|
GROUP BY referrer ORDER BY c DESC LIMIT 10"
|
|
);
|
|
$topReferrers->execute(['d' => $period]);
|
|
$topReferrers = $topReferrers->fetchAll();
|
|
|
|
// ── Projets cliqués ───────────────────────────────────────
|
|
$topProjects = $db->prepare(
|
|
"SELECT p.title, COUNT(pc.id) as clicks
|
|
FROM project_clicks pc
|
|
JOIN projects p ON p.id = pc.project_id
|
|
WHERE pc.created_at >= NOW() - INTERVAL :d DAY
|
|
GROUP BY pc.project_id ORDER BY clicks DESC LIMIT 10"
|
|
);
|
|
$topProjects->execute(['d' => $period]);
|
|
$topProjects = $topProjects->fetchAll();
|
|
|
|
// ── Heures de pointe ──────────────────────────────────────
|
|
$byHour = $db->prepare(
|
|
"SELECT HOUR(created_at) as h, COUNT(*) as c FROM visits
|
|
WHERE created_at >= NOW() - INTERVAL :d DAY
|
|
GROUP BY HOUR(created_at) ORDER BY h ASC"
|
|
);
|
|
$byHour->execute(['d' => $period]);
|
|
$hourData = array_fill(0, 24, 0);
|
|
foreach ($byHour->fetchAll() as $r) { $hourData[(int)$r['h']] = (int)$r['c']; }
|
|
|
|
function fmtDuration(int $s): string {
|
|
if ($s < 60) return $s . 's';
|
|
return floor($s / 60) . 'min ' . ($s % 60) . 's';
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="robots" content="noindex, nofollow">
|
|
<title>Analytics — BackOffice Tony AVERTY</title>
|
|
<link rel="stylesheet" href="assets/admin.css">
|
|
</head>
|
|
<body>
|
|
<div class="admin-page">
|
|
<?php require __DIR__ . '/includes/nav.php'; ?>
|
|
|
|
<main class="admin-content">
|
|
<div class="admin-topbar">
|
|
<h1 class="admin-page-title">Analytics</h1>
|
|
<div style="display:flex;gap:0.5rem;">
|
|
<?php foreach ([7 => '7j', 30 => '30j', 90 => '90j'] as $d => $label): ?>
|
|
<a href="?period=<?= $d ?>"
|
|
class="admin-btn admin-btn--sm <?= $period === $d ? 'admin-btn--primary' : 'admin-btn--outline' ?>">
|
|
<?= $label ?>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Stats -->
|
|
<div class="stats-grid">
|
|
<div class="stat-card">
|
|
<div class="stat-card__icon stat-card__icon--blue">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
|
|
</div>
|
|
<div class="stat-card__body">
|
|
<span class="stat-card__value"><?= number_format((int)$totalVisits) ?></span>
|
|
<span class="stat-card__label">Visites totales (<?= $period ?>j)</span>
|
|
</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="stat-card__icon stat-card__icon--purple">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 8v4l3 3"/></svg>
|
|
</div>
|
|
<div class="stat-card__body">
|
|
<span class="stat-card__value"><?= number_format((int)$uniqueSessions) ?></span>
|
|
<span class="stat-card__label">Sessions uniques</span>
|
|
</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="stat-card__icon stat-card__icon--green">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
|
|
</div>
|
|
<div class="stat-card__body">
|
|
<span class="stat-card__value"><?= $avgDuration ? fmtDuration($avgDuration) : '—' ?></span>
|
|
<span class="stat-card__label">Durée moyenne</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Chart visites -->
|
|
<div class="charts-grid">
|
|
<div class="chart-card chart-card--wide">
|
|
<h2 class="chart-card__title">Visites / jour (<?= $period ?>j)</h2>
|
|
<div class="chart-card__body"><canvas id="chartVisits" height="90"></canvas></div>
|
|
</div>
|
|
<div class="chart-card">
|
|
<h2 class="chart-card__title">Appareils</h2>
|
|
<div class="chart-card__body chart-card__body--center"><canvas id="chartDevices" height="200"></canvas></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Heures de pointe -->
|
|
<div class="charts-grid" style="margin-top:1.5rem;">
|
|
<div class="chart-card chart-card--wide">
|
|
<h2 class="chart-card__title">Heures de pointe</h2>
|
|
<div class="chart-card__body"><canvas id="chartHours" height="80"></canvas></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tables -->
|
|
<div class="analytics-tables">
|
|
<!-- Pages -->
|
|
<div class="analytics-table-card">
|
|
<h2 class="chart-card__title">Pages les plus visitées</h2>
|
|
<?php if (empty($topPages)): ?>
|
|
<p style="color:var(--admin-text-secondary);padding:1rem 0;">Aucune donnée.</p>
|
|
<?php else: ?>
|
|
<table class="admin-table">
|
|
<thead><tr><th>Page</th><th>Visites</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($topPages as $p): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($p['page'], ENT_QUOTES, 'UTF-8') ?></td>
|
|
<td><strong><?= (int)$p['c'] ?></strong></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Referrers -->
|
|
<div class="analytics-table-card">
|
|
<h2 class="chart-card__title">Sources de trafic</h2>
|
|
<?php if (empty($topReferrers)): ?>
|
|
<p style="color:var(--admin-text-secondary);padding:1rem 0;">Aucune donnée.</p>
|
|
<?php else: ?>
|
|
<table class="admin-table">
|
|
<thead><tr><th>Referrer</th><th>Visites</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($topReferrers as $r): ?>
|
|
<tr>
|
|
<td style="word-break:break-all;max-width:280px;"><?= htmlspecialchars($r['referrer'], ENT_QUOTES, 'UTF-8') ?></td>
|
|
<td><strong><?= (int)$r['c'] ?></strong></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if (!empty($topProjects)): ?>
|
|
<!-- Projets -->
|
|
<div class="analytics-table-card">
|
|
<h2 class="chart-card__title">Projets les plus cliqués</h2>
|
|
<table class="admin-table">
|
|
<thead><tr><th>Projet</th><th>Clics</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($topProjects as $p): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($p['title'], ENT_QUOTES, 'UTF-8') ?></td>
|
|
<td><strong><?= (int)$p['clicks'] ?></strong></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.2/dist/chart.umd.min.js" nonce="<?= Security::cspNonce() ?>"></script>
|
|
<script nonce="<?= Security::cspNonce() ?>">
|
|
Chart.defaults.color = '#8b8b9e';
|
|
Chart.defaults.borderColor = 'rgba(255,255,255,0.06)';
|
|
Chart.defaults.font.family = 'Inter, sans-serif';
|
|
|
|
new Chart(document.getElementById('chartVisits'), {
|
|
type: 'line',
|
|
data: {
|
|
labels: <?= json_encode($chartDays) ?>,
|
|
datasets: [{ label: 'Visites', data: <?= json_encode($chartVisits) ?>, borderColor: '#6366f1', backgroundColor: 'rgba(99,102,241,0.08)', fill: true, tension: 0.4, pointRadius: 2, borderWidth: 2 }]
|
|
},
|
|
options: { responsive: true, plugins: { legend: { display: false } }, scales: { x: { grid: { color: 'rgba(255,255,255,0.04)' } }, y: { grid: { color: 'rgba(255,255,255,0.04)' }, beginAtZero: true, ticks: { precision: 0 } } } }
|
|
});
|
|
|
|
new Chart(document.getElementById('chartDevices'), {
|
|
type: 'doughnut',
|
|
data: {
|
|
labels: ['Desktop', 'Mobile', 'Tablette'],
|
|
datasets: [{ data: [<?= $deviceData['desktop'] ?>, <?= $deviceData['mobile'] ?>, <?= $deviceData['tablet'] ?>], backgroundColor: ['#6366f1', '#22c55e', '#f59e0b'], borderWidth: 0, hoverOffset: 8 }]
|
|
},
|
|
options: { responsive: true, cutout: '65%', plugins: { legend: { position: 'bottom', labels: { padding: 16, usePointStyle: true } } } }
|
|
});
|
|
|
|
new Chart(document.getElementById('chartHours'), {
|
|
type: 'bar',
|
|
data: {
|
|
labels: <?= json_encode(range(0, 23)) ?>,
|
|
datasets: [{ label: 'Visites', data: <?= json_encode($hourData) ?>, backgroundColor: 'rgba(99,102,241,0.6)', borderRadius: 4, borderSkipped: false }]
|
|
},
|
|
options: { responsive: true, plugins: { legend: { display: false } }, scales: { x: { grid: { display: false } }, y: { grid: { color: 'rgba(255,255,255,0.04)' }, beginAtZero: true, ticks: { precision: 0 } } } }
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|