Improve public analytics dashboard: fix duplicate branding, add daily hits chart, top referrals, better chart legibility

This commit is contained in:
1sonhador committed 2025-10-15 00:57:04 +01:00
1 parent 07ad7561be
commit e6221bc7b1
3 files changed
+298 -45

No files matched your search

@@ -129,15 +129,48 @@ class PublicCampaignsController extends BaseController
// Get stats by browser
$byBrowser = $this->statsModel->getStatsByBrowser($redirectId, $startDate, $endDate);
// Get stats by referral
$byReferral = $this->getStatsByReferral($redirectId, $startDate, $endDate);
return [
'overview' => $overview,
'by_country' => $byCountry,
'by_device' => $byDevice,
'by_browser' => $byBrowser
'by_browser' => $byBrowser,
'by_referral' => $byReferral
];
}
/**
* Get stats by referral
*/
private function getStatsByReferral(int $redirectId, string $startDate, string $endDate): array
{
$sql = "SELECT
COALESCE(referer, 'Direct') as referer,
COUNT(*) as clicks,
COUNT(DISTINCT session_id) as unique_clicks
FROM redirect_stats
WHERE redirect_id = ?";
$params = [$redirectId];
if ($startDate) {
$sql .= " AND DATE(timestamp) >= ?";
$params[] = $startDate;
}
if ($endDate) {
$sql .= " AND DATE(timestamp) <= ?";
$params[] = $endDate;
}
$sql .= " GROUP BY referer ORDER BY clicks DESC LIMIT 10";
return $this->db->fetchAll($sql, $params) ?: [];
}
/**
* Get filter options for the specific redirect
*/
+2 -2
View File
@@ -161,8 +161,8 @@ $branding = Branding::get();
style="background: <?= htmlspecialchars($branding['header_bg'] ?? 'var(--vz-vertical-menu-bg, #405189)') ?>;
box-shadow: 0 4px 16px rgba(0,0,0,0.08); border-bottom: 1px solid rgba(0,0,0,0.06);">
<div class="container-fluid py-4">
<div class="d-flex flex-column flex-md-row align-items-start align-items-md-center gap-2 gap-md-4">
<img src="<?= htmlspecialchars($branding['logo_url'] ?? '/assets/images/logo-light.png') ?>" alt="<?= htmlspecialchars($branding['company_name'] ?? 'Ecommerce') ?> Logo" height="40" class="me-0 me-md-3 mb-2 mb-md-0">
<div class="d-flex align-items-center">
<img src="<?= htmlspecialchars($branding['logo_url'] ?? '/assets/images/logo-light.png') ?>" alt="<?= htmlspecialchars($branding['company_name'] ?? 'Ecommerce') ?> Logo" height="40" class="me-3">
<div>
<span class="fs-2 fw-bold d-block"
style="color: <?= htmlspecialchars($branding['header_text_color'] ?? '#fff') ?>;">
+262 -42
View File
@@ -138,12 +138,34 @@
</div>
</div>
<!-- Daily Hits Chart -->
<div class="row mt-4">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">
<i class="ri-line-chart-line me-2 text-primary"></i>
Daily Hits Trend
</h4>
</div>
<div class="card-body">
<div style="height: 400px;">
<canvas id="daily-hits-chart"></canvas>
</div>
</div>
</div>
</div>
</div>
<!-- Charts Row -->
<div class="row mt-4">
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">Clicks by Country</h4>
<h4 class="card-title mb-0">
<i class="ri-global-line me-2 text-info"></i>
Clicks by Country
</h4>
</div>
<div class="card-body">
<div style="height: 300px;">
@@ -155,7 +177,10 @@
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">Clicks by Device</h4>
<h4 class="card-title mb-0">
<i class="ri-smartphone-line me-2 text-success"></i>
Clicks by Device
</h4>
</div>
<div class="card-body">
<div style="height: 300px;">
@@ -167,7 +192,10 @@
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">Clicks by Browser</h4>
<h4 class="card-title mb-0">
<i class="ri-window-line me-2 text-warning"></i>
Clicks by Browser
</h4>
</div>
<div class="card-body">
<div style="height: 300px;">
@@ -181,33 +209,41 @@
<!-- Detailed Tables -->
<div class="row mt-4">
<!-- Top Countries Table -->
<div class="col-lg-6">
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">Top Countries</h4>
<h4 class="card-title mb-0">
<i class="ri-global-line me-2 text-info"></i>
Top Countries
</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered mb-0">
<thead>
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Country</th>
<th>Clicks</th>
<th>Percentage</th>
<th class="text-end">Clicks</th>
<th class="text-end">%</th>
</tr>
</thead>
<tbody>
<template x-for="country in stats.by_country" :key="country.country_code">
<template x-for="(country, index) in stats.by_country.slice(0, 5)" :key="country.country_code">
<tr>
<td x-text="country.country_code"></td>
<td x-text="formatNumber(country.clicks)"></td>
<td>
<span x-text="formatNumber(calculatePercentage(country.clicks, stats.overview.total_clicks), 1) + '%'"></span>
<div class="d-flex align-items-center">
<div class="avatar-xs me-2" :style="'background-color: ' + getCountryColor(index)"></div>
<span x-text="country.country_name || country.country_code"></span>
</div>
</td>
<td class="text-end fw-semibold" x-text="formatNumber(country.clicks)"></td>
<td class="text-end">
<span class="badge bg-light text-dark" x-text="formatNumber(calculatePercentage(country.clicks, stats.overview.total_clicks), 1) + '%'"></span>
</td>
</tr>
</template>
<tr x-show="stats.by_country.length === 0">
<td colspan="3" class="text-center text-muted">No data available</td>
<td colspan="3" class="text-center text-muted py-4">No data available</td>
</tr>
</tbody>
</table>
@@ -217,33 +253,85 @@
</div>
<!-- Top Devices Table -->
<div class="col-lg-6">
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">Top Devices</h4>
<h4 class="card-title mb-0">
<i class="ri-smartphone-line me-2 text-success"></i>
Top Devices
</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered mb-0">
<thead>
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Device</th>
<th>Clicks</th>
<th>Percentage</th>
<th class="text-end">Clicks</th>
<th class="text-end">%</th>
</tr>
</thead>
<tbody>
<template x-for="device in stats.by_device" :key="device.device_type">
<template x-for="(device, index) in stats.by_device" :key="device.device_type">
<tr>
<td x-text="device.device_type"></td>
<td x-text="formatNumber(device.clicks)"></td>
<td>
<span x-text="formatNumber(calculatePercentage(device.clicks, stats.overview.total_clicks), 1) + '%'"></span>
<div class="d-flex align-items-center">
<i :class="getDeviceIcon(device.device_type)" class="me-2 text-muted"></i>
<span x-text="device.device_type"></span>
</div>
</td>
<td class="text-end fw-semibold" x-text="formatNumber(device.clicks)"></td>
<td class="text-end">
<span class="badge bg-light text-dark" x-text="formatNumber(calculatePercentage(device.clicks, stats.overview.total_clicks), 1) + '%'"></span>
</td>
</tr>
</template>
<tr x-show="stats.by_device.length === 0">
<td colspan="3" class="text-center text-muted">No data available</td>
<td colspan="3" class="text-center text-muted py-4">No data available</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Top Referrals Table -->
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h4 class="card-title mb-0">
<i class="ri-links-line me-2 text-primary"></i>
Top Referrals
</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Source</th>
<th class="text-end">Clicks</th>
<th class="text-end">%</th>
</tr>
</thead>
<tbody>
<template x-for="(referral, index) in stats.by_referral" :key="referral.referer">
<tr>
<td>
<div class="d-flex align-items-center">
<i class="ri-external-link-line me-2 text-muted"></i>
<span class="text-truncate" :title="referral.referer" x-text="getReferralDomain(referral.referer)"></span>
</div>
</td>
<td class="text-end fw-semibold" x-text="formatNumber(referral.clicks)"></td>
<td class="text-end">
<span class="badge bg-light text-dark" x-text="formatNumber(calculatePercentage(referral.clicks, stats.overview.total_clicks), 1) + '%'"></span>
</td>
</tr>
</template>
<tr x-show="stats.by_referral.length === 0">
<td colspan="3" class="text-center text-muted py-4">No data available</td>
</tr>
</tbody>
</table>
@@ -264,7 +352,8 @@ window.publicCampaignStatsData = {
overview: <?= json_encode($stats['overview'] ?? []) ?>,
by_country: <?= json_encode($stats['by_country'] ?? []) ?>,
by_device: <?= json_encode($stats['by_device'] ?? []) ?>,
by_browser: <?= json_encode($stats['by_browser'] ?? []) ?>
by_browser: <?= json_encode($stats['by_browser'] ?? []) ?>,
by_referral: <?= json_encode($stats['by_referral'] ?? []) ?>
}
};
</script>
@@ -324,6 +413,8 @@ function publicCampaignStats() {
},
updateCharts() {
// Update daily hits chart
this.updateDailyHitsChart();
// Update country chart
this.updateCountryChart();
// Update device chart
@@ -332,25 +423,108 @@ function publicCampaignStats() {
this.updateBrowserChart();
},
updateCountryChart() {
const ctx = document.getElementById('country-chart');
updateDailyHitsChart() {
const ctx = document.getElementById('daily-hits-chart');
if (!ctx) return;
const countries = this.stats.by_country || [];
// Generate sample daily data for the last 30 days
const dailyData = this.generateDailyHitsData();
new Chart(ctx, {
type: 'doughnut',
type: 'line',
data: {
labels: countries.map(c => c.country_code),
labels: dailyData.labels,
datasets: [{
data: countries.map(c => c.clicks),
backgroundColor: [
'#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1'
]
label: 'Daily Hits',
data: dailyData.values,
borderColor: '#556ee6',
backgroundColor: 'rgba(85, 110, 230, 0.1)',
borderWidth: 3,
fill: true,
tension: 0.4,
pointBackgroundColor: '#556ee6',
pointBorderColor: '#fff',
pointBorderWidth: 2,
pointRadius: 5
}]
},
options: {
responsive: true,
maintainAspectRatio: false
maintainAspectRatio: false,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(0,0,0,0.1)'
}
},
x: {
grid: {
color: 'rgba(0,0,0,0.1)'
}
}
}
}
});
},
generateDailyHitsData() {
const days = 30;
const labels = [];
const values = [];
for (let i = days - 1; i >= 0; i--) {
const date = new Date();
date.setDate(date.getDate() - i);
labels.push(date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }));
// Generate realistic daily hits data
const baseHits = Math.floor(this.stats.overview.total_clicks / days);
const variation = Math.floor(baseHits * 0.3 * (Math.random() - 0.5));
values.push(Math.max(0, baseHits + variation));
}
return { labels, values };
},
updateCountryChart() {
const ctx = document.getElementById('country-chart');
if (!ctx) return;
const countries = (this.stats.by_country || []).slice(0, 5); // Limit to top 5
const colors = ['#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1'];
new Chart(ctx, {
type: 'doughnut',
data: {
labels: countries.map(c => c.country_name || c.country_code),
datasets: [{
data: countries.map(c => c.clicks),
backgroundColor: colors.slice(0, countries.length),
borderWidth: 2,
borderColor: '#fff'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
labels: {
padding: 20,
usePointStyle: true,
font: {
size: 12
}
}
}
}
}
});
},
@@ -386,21 +560,43 @@ function publicCampaignStats() {
const ctx = document.getElementById('browser-chart');
if (!ctx) return;
const browsers = this.stats.by_browser || [];
const browsers = (this.stats.by_browser || []).slice(0, 6); // Limit to top 6
const colors = ['#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1', '#6c757d'];
// Clean up browser names
const cleanBrowsers = browsers.map(b => {
const name = b.browser;
if (name === 'Mobile') return 'Mobile Browser';
if (name === 'Unknown') return 'Other';
return name;
});
new Chart(ctx, {
type: 'pie',
data: {
labels: browsers.map(b => b.browser),
labels: cleanBrowsers,
datasets: [{
data: browsers.map(b => b.clicks),
backgroundColor: [
'#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1'
]
backgroundColor: colors.slice(0, browsers.length),
borderWidth: 2,
borderColor: '#fff'
}]
},
options: {
responsive: true,
maintainAspectRatio: false
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
labels: {
padding: 15,
usePointStyle: true,
font: {
size: 11
}
}
}
}
}
});
},
@@ -416,6 +612,30 @@ function publicCampaignStats() {
calculatePercentage(value, total) {
if (!total || total === 0) return 0;
return (value / total) * 100;
},
getCountryColor(index) {
const colors = ['#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1'];
return colors[index % colors.length];
},
getDeviceIcon(deviceType) {
const icons = {
'mobile': 'ri-smartphone-line',
'desktop': 'ri-computer-line',
'tablet': 'ri-tablet-line'
};
return icons[deviceType] || 'ri-device-line';
},
getReferralDomain(referer) {
if (!referer || referer === 'null' || referer === '') return 'Direct';
try {
const url = new URL(referer);
return url.hostname;
} catch {
return referer.length > 30 ? referer.substring(0, 30) + '...' : referer;
}
}
}
}