Skip to content

Commit

Permalink
Resource- & Realtime-metrics (#14)
Browse files Browse the repository at this point in the history
* Add Resource analytics skeleton class

* Fix styling

* Fix styling

* wip

* Get top refererers

* Fix styling

* Get top traffic sources and landingspages

* Fix styling

* Get top backlinks

* Add more period methods

* Add realtime analytics

* Fix styling

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Mar 24, 2023
1 parent 54b7c47 commit 6c64be6
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 5 deletions.
60 changes: 56 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,32 @@ $averageSessionDuration = Analytics::averageSessionDuration(Period::days(7));
### Available periods

```php
// Set the period to the last x minutes:
Period::minutes(30);

// Set the period to the last x hours:
Period::hours(1);

// Set the period to the last x days:
Period::days(1);
Period::days(2);

// Set the period to the last x weeks:
Period::weeks(2);
Period::weeks(3);

// Set the period to the last x months:
Period::months(3);
Period::months(4);

// Set the period to the last x years:
Period::years(4);
Period::years(5);

// Set the period to a custom date range using a Carbon object:
$startDate = Carbon::now()->subDays(7);
$endDate = Carbon::now();

Period::create($startDate, $endDate);

// Set the period to since the given date:
Period::since(Carbon::now()->subDays(7));
```

## Available methods
Expand Down Expand Up @@ -237,6 +252,43 @@ $data = Analytics::totalViewsByCity(Period::days(14));
$data = Analytics::topViewsByCity(Period::days(14));
```

### Realtime Analytics

Methods to retrieve realtime analytics data for your website or application. You can use these methods to get information such as the current active visitors on your website. All of the methods take a Period object as a parameter to specify the time range for the analytics data. The default time range is set to 30 minutes when no Period object is passed.

Here are some examples of how to use the methods:

````php
use Vormkracht10\Analytics\Facades\Analytics;
use Vormkracht10\Analytics\Period;

// Get the total active users for the last 30 minutes:
$data = Analytics::totalActiveUsers();
```

### Resource Analytics

Methods to retrieve resource analytics data for your website or application. You can use these methods to get information such as the top landing pages, exit pages and referrers but also your most important social media

Here are some examples of how to use the methods:

```php
use Vormkracht10\Analytics\Facades\Analytics;
use Vormkracht10\Analytics\Period;

// Get the top 10 referrals for the last 14 days:
$data = Analytics::getTopReferrers(period: Period::days(14), limit: 10);

// Get the top 20 landing pages for the last 14 days:
$data = Analytics::getTopLandingPages(period: Period::days(14), limit: 20);

// Get the top 5 traffic sources for the last 14 days:
$data = Analytics::getTopTrafficSources(period: Period::days(14), limit: 5);

// Get the top 10 backlinks for the last 14 days:
$data = Analytics::getTopBacklinks(period: Period::days(14), limit: 10);
````

### Sessions Analytics

Methods to retrieve session duration analytics data for your website or application. You can use these methods to get information such as the average session time from your visitors. All of the methods take a Period object as a parameter to specify the time range for the analytics data.
Expand Down
6 changes: 5 additions & 1 deletion src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Vormkracht10\Analytics\Service\GoogleAnalyticsService;
use Vormkracht10\Analytics\Traits\Analytics\DemographicAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\DevicesAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\RealtimeAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\ResourceAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\SessionsAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\UsersAnalytics;
use Vormkracht10\Analytics\Traits\Analytics\ViewsAnalytics;
Expand All @@ -18,7 +20,9 @@ class Analytics
SessionsAnalytics,
UsersAnalytics,
DevicesAnalytics,
DemographicAnalytics;
DemographicAnalytics,
ResourceAnalytics,
RealtimeAnalytics;

public ?int $propertyId = null;

Expand Down
26 changes: 26 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,30 @@ public static function years(int $years): self

return new self($startDate, $endDate);
}

public static function custom(Carbon $startDate, Carbon $endDate): self
{
return new self($startDate, $endDate);
}

public static function since(Carbon $startDate): self
{
return new self($startDate, Carbon::today());
}

public static function hours(int $hours): self
{
$endDate = Carbon::now();
$startDate = Carbon::now()->subHours($hours);

return new self($startDate, $endDate);
}

public static function minutes(int $minutes): self
{
$endDate = Carbon::now();
$startDate = Carbon::now()->subMinutes($minutes);

return new self($startDate, $endDate);
}
}
29 changes: 29 additions & 0 deletions src/Traits/Analytics/RealtimeAnalytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Vormkracht10\Analytics\Traits\Analytics;

use Illuminate\Support\Arr;
use Vormkracht10\Analytics\Period;

trait RealtimeAnalytics
{
/**
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function activeUsers(Period|null $period = null): int
{
if (is_null($period)) {
$period = Period::minutes(30);
}

$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('activeUsers');

$result = $this->getReport($googleAnalytics)
->dataTable;

return (int) Arr::first(Arr::flatten($result));
}
}
61 changes: 61 additions & 0 deletions src/Traits/Analytics/ResourceAnalytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Vormkracht10\Analytics\Traits\Analytics;

use Vormkracht10\Analytics\Enums\Direction;
use Vormkracht10\Analytics\Period;

trait ResourceAnalytics
{
public function getTopReferrers(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('pageReferrer')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}

public function getLandingPages(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('landingPage')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}

public function getTopTrafficSources(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('defaultChannelGroup')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}

public function getTopBacklinks(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('sessionSourceMedium')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}
}

0 comments on commit 6c64be6

Please sign in to comment.