This commit is contained in:
Stefanescu Mihai
2026-03-18 16:59:39 +02:00
parent 274f6a378e
commit 9fb575ae70
17 changed files with 800 additions and 8 deletions
+61
View File
@@ -432,6 +432,67 @@ class AnalyticsQueryService
->get();
}
// ---- Conversions ----
public function conversionsReport(): Collection
{
$goals = \App\Models\Goal::where('website_id', $this->website->id)->get();
$totalSessions = $this->totalSessions();
return $goals->map(function ($goal) use ($totalSessions) {
$completions = \App\Models\GoalConversion::where('goal_id', $goal->id)
->whereBetween('converted_at', [$this->from, $this->to])
->count();
$totalValue = \App\Models\GoalConversion::where('goal_id', $goal->id)
->whereBetween('converted_at', [$this->from, $this->to])
->sum('value');
return (object) [
'id' => $goal->id,
'name' => $goal->name,
'type' => $goal->type,
'is_active' => $goal->is_active,
'completions' => $completions,
'conversion_rate' => $totalSessions > 0 ? round(($completions / $totalSessions) * 100, 2) : 0,
'total_value' => $totalValue,
];
});
}
public function goalCompletionsOverTime(): Collection
{
$days = $this->from->diffInDays($this->to);
$groupFormat = $days <= 2 ? '%Y-%m-%d %H:00' : '%Y-%m-%d';
$carbonFormat = $days <= 2 ? 'Y-m-d H:00' : 'Y-m-d';
$data = \App\Models\GoalConversion::where('website_id', $this->website->id)
->whereBetween('converted_at', [$this->from, $this->to])
->select(
DB::raw("strftime('{$groupFormat}', converted_at) as period"),
DB::raw('COUNT(*) as completions'),
)
->groupBy('period')
->orderBy('period')
->get()
->keyBy('period');
$periods = collect();
$cursor = $this->from->copy();
$increment = $days <= 2 ? 'addHour' : 'addDay';
while ($cursor->lte($this->to)) {
$key = $cursor->format($carbonFormat);
$periods->push([
'period' => $key,
'completions' => $data->get($key)?->completions ?? 0,
]);
$cursor->$increment();
}
return $periods;
}
// ---- Real-Time ----
public static function activeVisitors(Website $website, int $minutes = 5): int