Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Oct 24, 2024
1 parent 7b6c62f commit 1150bf0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
21 changes: 11 additions & 10 deletions src/FilamentGoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +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;

Expand All @@ -33,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 intval((($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()
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/MetricDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace BezhanSalleh\FilamentGoogleAnalytics\Traits;

use Carbon\Carbon;
use Spatie\Analytics\Period;
use Spatie\Analytics\OrderBy;
use Illuminate\Support\Collection;
use Spatie\Analytics\Facades\Analytics;
use Spatie\Analytics\Period;

trait MetricDiff
{
Expand All @@ -15,6 +16,9 @@ private function get(string $metric, string $dimensions, Period $period): Collec
$period,
[$metric],
[$dimensions],
orderBy: [
OrderBy::dimension($dimensions, true),
],
);

$results = $analyticsData;
Expand Down
26 changes: 20 additions & 6 deletions src/Traits/Sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,33 @@ 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
{
$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,
];
}

Expand Down
26 changes: 20 additions & 6 deletions src/Traits/SessionsDuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,33 @@ 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
{
$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,
];
}

Expand Down

0 comments on commit 1150bf0

Please sign in to comment.