phase 8 and 9

This commit is contained in:
Stefanescu Mihai
2026-03-19 18:42:19 +02:00
parent d75cabfeec
commit 82ebb697f3
94 changed files with 4498 additions and 749 deletions
+44
View File
@@ -16,6 +16,8 @@ class AnalyticsQueryService
{
private readonly Carbon $todayStart;
private ?Collection $segmentVisitorIds = null;
public function __construct(
private readonly Website $website,
private readonly Carbon $from,
@@ -29,6 +31,27 @@ class AnalyticsQueryService
return new self($website, $from->startOfDay(), $to->endOfDay());
}
public function withSegment(?Collection $visitorIds): self
{
$this->segmentVisitorIds = $visitorIds;
return $this;
}
public function isSegmented(): bool
{
return $this->segmentVisitorIds !== null;
}
private function applySegmentFilter($query, string $column = 'visitor_id')
{
if ($this->segmentVisitorIds !== null) {
$query->whereIn($column, $this->segmentVisitorIds);
}
return $query;
}
/**
* Split the requested date range into two parts:
* - "aggregate range": completed days that can be served from aggregate tables
@@ -76,6 +99,13 @@ class AnalyticsQueryService
public function totalPageViews(): int
{
if ($this->isSegmented()) {
return $this->applySegmentFilter(
PageView::where('website_id', $this->website->id)
->whereBetween('viewed_at', [$this->from, $this->to])
)->count();
}
$total = 0;
[$aggFrom, $aggTo, $liveFrom, $liveTo] = $this->splitRange();
@@ -96,6 +126,13 @@ class AnalyticsQueryService
public function uniqueVisitors(): int
{
if ($this->isSegmented()) {
return $this->applySegmentFilter(
PageView::where('website_id', $this->website->id)
->whereBetween('viewed_at', [$this->from, $this->to])
)->distinct('visitor_id')->count('visitor_id');
}
$total = 0;
[$aggFrom, $aggTo, $liveFrom, $liveTo] = $this->splitRange();
@@ -117,6 +154,13 @@ class AnalyticsQueryService
public function totalSessions(): int
{
if ($this->isSegmented()) {
return $this->applySegmentFilter(
AnalyticsSession::where('website_id', $this->website->id)
->whereBetween('started_at', [$this->from, $this->to])
)->count();
}
$total = 0;
[$aggFrom, $aggTo, $liveFrom, $liveTo] = $this->splitRange();
+84
View File
@@ -0,0 +1,84 @@
<?php
namespace App\Services;
use App\Models\Event;
use App\Models\Funnel;
use App\Models\PageView;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class FunnelAnalysisService
{
public function __construct(
private readonly Funnel $funnel,
private readonly Carbon $from,
private readonly Carbon $to,
) {}
/**
* Analyze funnel: for each step, count how many unique visitors completed it
* (in order, within the same session or across sessions in the date range).
*
* Returns: [{label, type, value, visitors, drop_off, drop_off_pct, conversion_pct}]
*/
public function analyze(): array
{
$steps = $this->funnel->steps;
if (empty($steps)) {
return [];
}
$websiteId = $this->funnel->website_id;
// Step 1: get all visitor IDs who completed the first step
$currentVisitorIds = $this->visitorIdsForStep($steps[0], $websiteId);
$firstStepCount = $currentVisitorIds->count();
$results = [];
foreach ($steps as $i => $step) {
if ($i === 0) {
$stepVisitors = $firstStepCount;
} else {
$stepVisitorIds = $this->visitorIdsForStep($step, $websiteId);
$currentVisitorIds = $currentVisitorIds->intersect($stepVisitorIds);
$stepVisitors = $currentVisitorIds->count();
}
$prevCount = $i > 0 ? $results[$i - 1]['visitors'] : $stepVisitors;
$dropOff = $prevCount - $stepVisitors;
$dropOffPct = $prevCount > 0 ? round(($dropOff / $prevCount) * 100, 1) : 0;
$conversionPct = $firstStepCount > 0 ? round(($stepVisitors / $firstStepCount) * 100, 1) : 0;
$results[] = [
'label' => $step['label'] ?? $step['value'],
'type' => $step['type'],
'value' => $step['value'],
'visitors' => $stepVisitors,
'drop_off' => $dropOff,
'drop_off_pct' => $dropOffPct,
'conversion_pct' => $conversionPct,
];
}
return $results;
}
private function visitorIdsForStep(array $step, int $websiteId): Collection
{
if ($step['type'] === 'pageview') {
return PageView::where('website_id', $websiteId)
->whereBetween('viewed_at', [$this->from, $this->to])
->where('path', $step['value'])
->distinct()
->pluck('visitor_id');
}
return Event::where('website_id', $websiteId)
->whereBetween('occurred_at', [$this->from, $this->to])
->where('name', $step['value'])
->distinct()
->pluck('visitor_id');
}
}