67 lines
2.0 KiB
PHP
67 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Pages\Analytics;
|
|
|
|
use App\Domains\Analytics\Models\Click;
|
|
use App\Domains\Workspace\Models\Workspace;
|
|
use Illuminate\View\View;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
|
|
class Index extends Component
|
|
{
|
|
/** @var array<int, array<string, mixed>> */
|
|
public array $recentClicks = [];
|
|
|
|
public int $totalToday = 0;
|
|
|
|
public int $workspaceId = 0;
|
|
|
|
public function mount(): void
|
|
{
|
|
/** @var Workspace $workspace */
|
|
$workspace = app('current_workspace');
|
|
|
|
$this->workspaceId = $workspace->id;
|
|
$this->totalToday = Click::where('workspace_id', $workspace->id)
|
|
->whereDate('clicked_at', today())
|
|
->count();
|
|
}
|
|
|
|
/** @param array<string, mixed> $data */
|
|
#[On('echo:workspace.{workspaceId}.analytics,click.recorded')]
|
|
public function handleNewClick(array $data): void
|
|
{
|
|
array_unshift($this->recentClicks, $data);
|
|
$this->recentClicks = array_slice($this->recentClicks, 0, 10);
|
|
$this->totalToday++;
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
/** @var Workspace $workspace */
|
|
$workspace = app('current_workspace');
|
|
|
|
$totalClicks = Click::where('workspace_id', $workspace->id)->count();
|
|
$last30 = Click::where('workspace_id', $workspace->id)
|
|
->where('clicked_at', '>=', now()->subDays(30))->count();
|
|
|
|
$byCountry = Click::where('workspace_id', $workspace->id)
|
|
->whereNotNull('country')
|
|
->selectRaw('country, COUNT(*) as total')
|
|
->groupBy('country')
|
|
->orderByDesc('total')
|
|
->limit(5)
|
|
->get();
|
|
|
|
$byDevice = Click::where('workspace_id', $workspace->id)
|
|
->selectRaw('device, COUNT(*) as total')
|
|
->groupBy('device')
|
|
->orderByDesc('total')
|
|
->get();
|
|
|
|
return view('livewire.pages.analytics.index', compact('totalClicks', 'last30', 'byCountry', 'byDevice'))
|
|
->layout('layouts.nimuli-app', ['title' => 'Analytics']);
|
|
}
|
|
}
|