- Add source maps and license files for compiled assets - Add debug and test files for development - Complete Git repository with all necessary files for MoneyFlix project
220 lines
7.0 KiB
PHP
220 lines
7.0 KiB
PHP
<?php
|
|
namespace App\Modules\Controllers;
|
|
|
|
use App\Core\BaseController;
|
|
use App\Core\View;
|
|
use App\Core\DatabaseManager;
|
|
use App\Core\Branding;
|
|
use App\Modules\Redirects\Models\RedirectStatsModel;
|
|
|
|
class PublicCampaignsController extends BaseController
|
|
{
|
|
private DatabaseManager $db;
|
|
private RedirectStatsModel $statsModel;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->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, $redirect, $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,
|
|
'redirect' => $redirect,
|
|
'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 analytics slug
|
|
*/
|
|
private function getRedirectBySlug(string $slug): ?array
|
|
{
|
|
$sql = "SELECT id, name, cuid, analytics_slug, target_url, status FROM redirects WHERE analytics_slug = :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);
|
|
|
|
// 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,
|
|
'daily_hits' => $dailyHits
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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 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
|
|
*/
|
|
private function getFilterOptions(int $redirectId): array
|
|
{
|
|
return [
|
|
'redirects' => [$redirectId], // Only this redirect
|
|
'countries' => [],
|
|
'browsers' => [],
|
|
'operatingSystems' => []
|
|
];
|
|
}
|
|
}
|