Skip to content

Commit

Permalink
Merge pull request #81 from bezhanSalleh/patch/metric-diff
Browse files Browse the repository at this point in the history
patched metric-diff
  • Loading branch information
bezhanSalleh authored Oct 24, 2024
2 parents 186aa53 + 515770c commit d076058
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
25 changes: 13 additions & 12 deletions src/FilamentGoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

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 @@ -35,11 +35,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()
Expand Down Expand Up @@ -86,7 +87,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)
Expand All @@ -100,7 +101,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;
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/MetricDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 d076058

Please sign in to comment.