phase 4
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\AnalyticsSession;
|
||||
use App\Models\Goal;
|
||||
use App\Models\GoalConversion;
|
||||
use App\Models\Visitor;
|
||||
use App\Models\Website;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class GoalEvaluationService
|
||||
{
|
||||
public function evaluate(
|
||||
Website $website,
|
||||
Visitor $visitor,
|
||||
AnalyticsSession $session,
|
||||
array $payload,
|
||||
Carbon $now,
|
||||
): void {
|
||||
$goals = $website->goals()->where('is_active', true)->get();
|
||||
|
||||
foreach ($goals as $goal) {
|
||||
if ($this->alreadyConverted($goal, $session)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->matches($goal, $session, $payload)) {
|
||||
GoalConversion::create([
|
||||
'goal_id' => $goal->id,
|
||||
'website_id' => $website->id,
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'value' => $goal->default_value,
|
||||
'converted_at' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function alreadyConverted(Goal $goal, AnalyticsSession $session): bool
|
||||
{
|
||||
return GoalConversion::where('goal_id', $goal->id)
|
||||
->where('analytics_session_id', $session->id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
private function matches(Goal $goal, AnalyticsSession $session, array $payload): bool
|
||||
{
|
||||
return match ($goal->type) {
|
||||
'url_destination' => $this->matchesUrlDestination($goal, $payload),
|
||||
'event_match' => $this->matchesEvent($goal, $payload),
|
||||
'duration' => $this->matchesDuration($goal, $session),
|
||||
'pages_per_session' => $this->matchesPagesPerSession($goal, $session),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
private function matchesUrlDestination(Goal $goal, array $payload): bool
|
||||
{
|
||||
$conditions = $goal->conditions;
|
||||
$path = $payload['path'] ?? '';
|
||||
$targetPath = $conditions['path'] ?? '';
|
||||
$matchType = $conditions['match_type'] ?? 'exact';
|
||||
|
||||
if (! $path || ! $targetPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match ($matchType) {
|
||||
'exact' => $path === $targetPath,
|
||||
'starts_with' => str_starts_with($path, $targetPath),
|
||||
'contains' => str_contains($path, $targetPath),
|
||||
'regex' => (bool) @preg_match($targetPath, $path),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
private function matchesEvent(Goal $goal, array $payload): bool
|
||||
{
|
||||
if (($payload['type'] ?? '') !== 'event') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$conditions = $goal->conditions;
|
||||
$eventName = $conditions['event_name'] ?? '';
|
||||
|
||||
if (! $eventName || ($payload['event_name'] ?? '') !== $eventName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$requiredCategory = $conditions['event_category'] ?? null;
|
||||
if ($requiredCategory && ($payload['event_category'] ?? '') !== $requiredCategory) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function matchesDuration(Goal $goal, AnalyticsSession $session): bool
|
||||
{
|
||||
$requiredSeconds = $goal->conditions['seconds'] ?? 0;
|
||||
|
||||
return $session->duration >= $requiredSeconds;
|
||||
}
|
||||
|
||||
private function matchesPagesPerSession(Goal $goal, AnalyticsSession $session): bool
|
||||
{
|
||||
$requiredPages = $goal->conditions['page_count'] ?? 0;
|
||||
|
||||
return $session->pages_count >= $requiredPages;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user