diff --git a/crm/Modules/Controllers/PublicCampaignsController.php b/crm/Modules/Controllers/PublicCampaignsController.php index 10e2775..b7f268f 100644 --- a/crm/Modules/Controllers/PublicCampaignsController.php +++ b/crm/Modules/Controllers/PublicCampaignsController.php @@ -132,13 +132,17 @@ class PublicCampaignsController extends BaseController // Get stats by referral $byReferral = $this->getStatsByReferral($redirectId, $startDate, $endDate); + + // Get daily hits data + $dailyHits = $this->getDailyHitsData($redirectId, $startDate, $endDate); return [ 'overview' => $overview, 'by_country' => $byCountry, 'by_device' => $byDevice, 'by_browser' => $byBrowser, - 'by_referral' => $byReferral + 'by_referral' => $byReferral, + 'daily_hits' => $dailyHits ]; } @@ -171,6 +175,34 @@ class PublicCampaignsController extends BaseController return $this->db->fetchAll($sql, $params) ?: []; } + /** + * Get daily hits data for the chart + */ + private function getDailyHitsData(int $redirectId, string $startDate, string $endDate): array + { + $sql = "SELECT + DATE(timestamp) as date, + COUNT(*) as hits + 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 DATE(timestamp) ORDER BY date ASC"; + + return $this->db->fetchAll($sql, $params) ?: []; + } + /** * Get filter options for the specific redirect */ diff --git a/crm/Views/pages/public-campaign-analytics.php b/crm/Views/pages/public-campaign-analytics.php index 5cc8050..10ffee3 100644 --- a/crm/Views/pages/public-campaign-analytics.php +++ b/crm/Views/pages/public-campaign-analytics.php @@ -353,7 +353,8 @@ window.publicCampaignStatsData = { by_country: , by_device: , by_browser: , - by_referral: + by_referral: , + daily_hits: } }; @@ -427,16 +428,32 @@ function publicCampaignStats() { const ctx = document.getElementById('daily-hits-chart'); if (!ctx) return; - // Generate sample daily data for the last 30 days - const dailyData = this.generateDailyHitsData(); + // Use real daily hits data from database + const dailyData = this.stats.daily_hits || []; + + if (dailyData.length === 0) { + // Show empty state if no data + ctx.getContext('2d').clearRect(0, 0, ctx.width, ctx.height); + ctx.getContext('2d').fillStyle = '#666'; + ctx.getContext('2d').font = '16px Arial'; + ctx.getContext('2d').textAlign = 'center'; + ctx.getContext('2d').fillText('No data available for the selected period', ctx.width / 2, ctx.height / 2); + return; + } + + const labels = dailyData.map(d => { + const date = new Date(d.date); + return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); + }); + const values = dailyData.map(d => parseInt(d.hits)); new Chart(ctx, { type: 'line', data: { - labels: dailyData.labels, + labels: labels, datasets: [{ label: 'Daily Hits', - data: dailyData.values, + data: values, borderColor: '#556ee6', backgroundColor: 'rgba(85, 110, 230, 0.1)', borderWidth: 3, @@ -473,25 +490,6 @@ function publicCampaignStats() { }); }, - 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;