db = DatabaseManager::getInstance(); $this->statsModel = new RedirectStatsModel(); } /** * Renders the public campaign analytics page - simplified to use existing redirects stats */ public function analytics(string $slug): void { if (empty($slug)) { http_response_code(404); echo "Campaign slug is required."; return; } // Get redirect by slug $redirect = $this->getRedirectBySlug($slug); if (!$redirect) { http_response_code(404); echo "Redirect not found."; return; } $branding = Branding::get(); $page_title = ($branding['company_name'] ?? 'Analytics') . ' - ' . $redirect['name']; // Get date range from query params $startDate = $_GET['startDate'] ?? date('Y-m-d', strtotime('-30 days')); $endDate = $_GET['endDate'] ?? date('Y-m-d'); // Get real stats data for this specific redirect $stats = $this->getRedirectStats($redirect['id'], $startDate, $endDate); $filterOptions = $this->getFilterOptions($redirect['id']); View::render('layouts/public-blank.php', [ 'title' => $page_title, 'page_name' => 'public-campaign-analytics', 'branding' => $branding, 'slug' => $slug, 'page_content' => function() use ($branding, $slug, $stats, $filterOptions, $startDate, $endDate) { View::render('layouts/page-title.php', [ 'pagetitle' => 'Campaign Analytics', 'title' => 'Campaign Performance' ]); View::render('pages/public-campaign-analytics', [ 'branding' => $branding, 'slug' => $slug, 'stats' => $stats, 'filterOptions' => $filterOptions, 'startDate' => $startDate, 'endDate' => $endDate ]); } ]); } /** * API endpoint: Get redirect stats by slug */ public function getCampaignStats(string $slug): void { header('Content-Type: application/json'); if (empty($slug)) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Missing campaign slug']); return; } $startDate = $_GET['startDate'] ?? date('Y-m-d', strtotime('-30 days')); $endDate = $_GET['endDate'] ?? date('Y-m-d'); try { $redirect = $this->getRedirectBySlug($slug); if (!$redirect) { http_response_code(404); echo json_encode(['success' => false, 'message' => 'Redirect not found']); return; } $stats = $this->getRedirectStats($redirect['id'], $startDate, $endDate); echo json_encode(['success' => true, 'data' => $stats]); } catch (\Exception $e) { error_log("API error: " . $e->getMessage()); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Error fetching redirect data']); } } /** * Get redirect by slug (cuid) */ private function getRedirectBySlug(string $slug): ?array { $sql = "SELECT id, name, cuid, target_url, status FROM redirects WHERE cuid = :slug AND status >= 0"; return $this->db->fetch($sql, [':slug' => $slug]); } /** * Get real redirect stats data */ private function getRedirectStats(int $redirectId, string $startDate, string $endDate): array { // Get overview stats $overview = $this->statsModel->getStatsByRedirect($redirectId, $startDate, $endDate); // Get stats by country $byCountry = $this->statsModel->getStatsByCountry($redirectId, $startDate, $endDate); // Get stats by device $byDevice = $this->statsModel->getStatsByDevice($redirectId, $startDate, $endDate); // 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_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 */ private function getFilterOptions(int $redirectId): array { return [ 'redirects' => [$redirectId], // Only this redirect 'countries' => [], 'browsers' => [], 'operatingSystems' => [] ]; } }