From ac7577cdff0cf4bf4b2a8770f53b83fd3eab9837 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 24 Oct 2024 02:38:11 +0200 Subject: [PATCH] patched metric-diff --- src/FilamentGoogleAnalytics.php | 27 +++++++++++++-------------- src/Traits/MetricDiff.php | 4 ++++ src/Traits/Sessions.php | 26 ++++++++++++++++++++------ src/Traits/SessionsDuration.php | 26 ++++++++++++++++++++------ 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/FilamentGoogleAnalytics.php b/src/FilamentGoogleAnalytics.php index b64dd89..15f7e0c 100755 --- a/src/FilamentGoogleAnalytics.php +++ b/src/FilamentGoogleAnalytics.php @@ -6,27 +6,25 @@ class FilamentGoogleAnalytics { - public string $previous; + public int | float $previous = 0; public string $format; - public function __construct(public ?string $value = null) - { - } + public function __construct(public int | float $value = 0) {} - public static function for(?string $value = null) + public static function for(int | float $value = 0): static { return new static($value); } - public function previous(string $previous) + public function previous(int | float $previous): static { $this->previous = $previous; return $this; } - public function format(string $format) + public function format(string $format): static { $this->format = $format; @@ -35,11 +33,12 @@ public function format(string $format) public function compute(): int { - if ($this->value == 0 || $this->previous == 0 || $this->previous == null) { - return 0; - } - - return (($this->value - $this->previous) / $this->previous) * 100; + return match (true) { + $this->value == 0 && $this->previous == 0 => 0, + $this->value > 0 && $this->previous == 0 => $this->value, + $this->previous != 0 => intval((($this->value - $this->previous) / $this->previous) * 100), + default => 0, + }; } public function trajectoryValue() @@ -86,7 +85,7 @@ public function trajectoryIcon() */ public function trajectoryDescription(): string { - return static::thousandsFormater(abs($this->compute())).$this->format.' '.$this->trajectoryLabel(); + return static::thousandsFormater(abs($this->compute())) . $this->format . ' ' . $this->trajectoryLabel(); } public static function thousandsFormater($value) @@ -100,7 +99,7 @@ public static function thousandsFormater($value) $x_parts = ['k', 'm', 'b', 't']; $x_count_parts = count($x_array) - 1; $x_display = $x; - $x_display = $x_array[0].((int) $x_array[1][0] !== 0 ? '.'.$x_array[1][0] : ''); + $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : ''); $x_display .= $x_parts[$x_count_parts - 1]; return $x_display; diff --git a/src/Traits/MetricDiff.php b/src/Traits/MetricDiff.php index b9e0d67..547e973 100644 --- a/src/Traits/MetricDiff.php +++ b/src/Traits/MetricDiff.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Support\Collection; use Spatie\Analytics\Facades\Analytics; +use Spatie\Analytics\OrderBy; use Spatie\Analytics\Period; trait MetricDiff @@ -15,6 +16,9 @@ private function get(string $metric, string $dimensions, Period $period): Collec $period, [$metric], [$dimensions], + orderBy: [ + OrderBy::dimension($dimensions, true), + ], ); $results = $analyticsData; diff --git a/src/Traits/Sessions.php b/src/Traits/Sessions.php index f6a0d0c..39d8992 100644 --- a/src/Traits/Sessions.php +++ b/src/Traits/Sessions.php @@ -13,10 +13,24 @@ private function sessionsToday(): array { $results = $this->get('sessions', 'date', Period::days(1)); - return [ - 'previous' => $results->first()['value'] ?? 0, - 'result' => $results->last()['value'] ?? 0, - ]; + return match (true) { + ($results->containsOneItem() && ($results->first()['date'])->isYesterday()) => [ + 'previous' => $results->first()['value'], + 'result' => 0, + ], + ($results->containsOneItem() && ($results->first()['date'])->isToday()) => [ + 'previous' => 0, + 'result' => $results->first()['value'], + ], + $results->isEmpty() => [ + 'previous' => 0, + 'result' => 0, + ], + default => [ + 'previous' => $results->last()['value'] ?? 0, + 'result' => $results->first()['value'] ?? 0, + ] + }; } private function sessionsYesterday(): array @@ -24,8 +38,8 @@ private function sessionsYesterday(): array $results = $this->get('sessions', 'date', Period::create(Carbon::yesterday()->clone()->subDay(), Carbon::yesterday())); return [ - 'previous' => $results->first()['value'] ?? 0, - 'result' => $results->last()['value'] ?? 0, + 'previous' => $results->last()['value'] ?? 0, + 'result' => $results->first()['value'] ?? 0, ]; } diff --git a/src/Traits/SessionsDuration.php b/src/Traits/SessionsDuration.php index 444cfc9..71b2ceb 100644 --- a/src/Traits/SessionsDuration.php +++ b/src/Traits/SessionsDuration.php @@ -13,10 +13,24 @@ private function sessionDurationToday(): array { $results = $this->get('averageSessionDuration', 'date', Period::days(1)); - return [ - 'previous' => $results->last()['value'] ?? 0, - 'result' => $results->first()['value'] ?? 0, - ]; + return match (true) { + ($results->containsOneItem() && ($results->first()['date'])->isYesterday()) => [ + 'previous' => $results->first()['value'], + 'result' => 0, + ], + ($results->containsOneItem() && ($results->first()['date'])->isToday()) => [ + 'previous' => 0, + 'result' => $results->first()['value'], + ], + $results->isEmpty() => [ + 'previous' => 0, + 'result' => 0, + ], + default => [ + 'previous' => $results->last()['value'] ?? 0, + 'result' => $results->first()['value'] ?? 0, + ] + }; } private function sessionDurationYesterday(): array @@ -24,8 +38,8 @@ private function sessionDurationYesterday(): array $results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::yesterday()->clone()->subDay(), Carbon::yesterday())); return [ - 'previous' => $results->first()['value'] ?? 0, - 'result' => $results->last()['value'] ?? 0, + 'previous' => $results->last()['value'] ?? 0, + 'result' => $results->first()['value'] ?? 0, ]; }