phase 8 and 9
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user