- Added standard Laravel directory structure and configuration. - Included Svelte and Tailwind configuration for the admin interface. - Added core PHPUnit and testing scripts.
56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\PageView;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Service to handle Analytics-related operations.
|
|
*/
|
|
class AnalyticsService
|
|
{
|
|
/**
|
|
* Get aggregate analytics data for the dashboard.
|
|
*
|
|
* @return array Statistics array including total views, views by date, top pages, referrers, and browsers.
|
|
*/
|
|
public function getDashboardStats(): array
|
|
{
|
|
return [
|
|
'totalViews' => PageView::count(),
|
|
'viewsByDate' => PageView::select('view_date', DB::raw('count(*) as count'))
|
|
->groupBy('view_date')
|
|
->orderBy('view_date', 'desc')
|
|
->limit(30)
|
|
->get(),
|
|
'topPages' => PageView::select('path', DB::raw('count(*) as count'))
|
|
->groupBy('path')
|
|
->orderBy('count', 'desc')
|
|
->limit(10)
|
|
->get(),
|
|
'topReferrers' => PageView::select('referrer', DB::raw('count(*) as count'))
|
|
->whereNotNull('referrer')
|
|
->groupBy('referrer')
|
|
->orderBy('count', 'desc')
|
|
->limit(10)
|
|
->get(),
|
|
'browsers' => PageView::select('browser', DB::raw('count(*) as count'))
|
|
->groupBy('browser')
|
|
->orderBy('count', 'desc')
|
|
->get(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Record a new pageview in the database.
|
|
*
|
|
* @param array $data Data for the pageview (path, referrer, browser, etc.).
|
|
* @return PageView The recorded PageView model instance.
|
|
*/
|
|
public function recordPageview(array $data): PageView
|
|
{
|
|
return PageView::create($data);
|
|
}
|
|
}
|