Skip to content

Commit

Permalink
Realtime Reports (#21)
Browse files Browse the repository at this point in the history
* Minute range trait for realtime analytics

* Fix styling

* wip

* Fix styling

* wip

* wip

* wip

* wip

* Fix styling

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Type hint items

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Aug 4, 2023
1 parent 7bf6d7c commit 6805ed5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
31 changes: 29 additions & 2 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public function getReport(GoogleAnalyticsService $googleAnalytics): AnalyticsRes
{
$client = $this->getClient();

$response = $client->runReport([
$parameters = [
'property' => 'properties/'.$this->getPropertyId(),
'dateRanges' => $googleAnalytics->dateRanges,
'minuteRanges' => $googleAnalytics->minuteRanges,
'dimensions' => $googleAnalytics->dimensions,
'metrics' => $googleAnalytics->metrics,
'orderBys' => $googleAnalytics->orderBys,
Expand All @@ -84,7 +85,33 @@ public function getReport(GoogleAnalyticsService $googleAnalytics): AnalyticsRes
'limit' => $googleAnalytics->limit,
'offset' => $googleAnalytics->offset,
'keepEmptyRows' => $googleAnalytics->keepEmptyRows,
]);
];

$response = $client->runReport($parameters);

return $this->formatResponse($response);
}

public function getRealtimeReport(GoogleAnalyticsService $googleAnalytics): AnalyticsResponse
{
$client = $this->getClient();

$parameters = [
'property' => 'properties/'.$this->getPropertyId(),
'dateRanges' => $googleAnalytics->dateRanges,
'minuteRanges' => $googleAnalytics->minuteRanges,
'dimensions' => $googleAnalytics->dimensions,
'metrics' => $googleAnalytics->metrics,
'orderBys' => $googleAnalytics->orderBys,
'metricAggregations' => $googleAnalytics->metricAggregations,
'dimensionFilter' => $googleAnalytics->dimensionFilter,
'metricFilter' => $googleAnalytics->metricFilter,
'limit' => $googleAnalytics->limit,
'offset' => $googleAnalytics->offset,
'keepEmptyRows' => $googleAnalytics->keepEmptyRows,
];

$response = $client->runRealtimeReport($parameters);

return $this->formatResponse($response);
}
Expand Down
5 changes: 3 additions & 2 deletions src/AnalyticsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Vormkracht10\Analytics;

use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
use Google\Analytics\Data\V1beta\RunReportResponse;

class AnalyticsResponse
{
public RunReportResponse $response;
public RunReportResponse|RunRealtimeReportResponse $response;

public array $dataTable;

public array $metricAggregationsTable;

public function setResponse(RunReportResponse $response): self
public function setResponse(RunReportResponse|RunRealtimeReportResponse $response): self
{
$this->response = $response;

Expand Down
2 changes: 2 additions & 0 deletions src/Service/GoogleAnalyticsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Vormkracht10\Analytics\Traits\Google\FilterByTrait;
use Vormkracht10\Analytics\Traits\Google\MetricAggregationTrait;
use Vormkracht10\Analytics\Traits\Google\MetricTrait;
use Vormkracht10\Analytics\Traits\Google\MinuteRangeTrait;
use Vormkracht10\Analytics\Traits\Google\OrderByTrait;
use Vormkracht10\Analytics\Traits\Google\RowConfigTrait;
use Vormkracht10\Analytics\Traits\ResponseFormatterTrait;

class GoogleAnalyticsService
{
use DateRangeTrait,
MinuteRangeTrait,
MetricTrait,
DimensionTrait,
OrderByTrait,
Expand Down
18 changes: 14 additions & 4 deletions src/Traits/Analytics/RealtimeAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ trait RealtimeAnalytics
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function activeUsers(Period $period = null, string $path = null): int
public function activeUsers(Period $period = null, string $path = null): int|array
{
if (is_null($period)) {
$period = Period::minutes(30);
}

$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->setMinuteRange(
name: null,
start: 29,
end: 0,
)
->addMetrics('activeUsers');

if ($path) {
$googleAnalytics->addDimension('pagePath');
// Filter by path is not possible yet on the Realtime API. Currently it is only possible to filter by unifiedScreenName.
// @see https://stackoverflow.com/a/70684184/7603806
// @see https://developers.google.com/analytics/devguides/reporting/data/v1/realtime-api-schema#dimensions
$googleAnalytics->addDimension('unifiedScreenName');

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

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

return (int) Arr::first(Arr::flatten($result));
Expand Down
45 changes: 45 additions & 0 deletions src/Traits/Google/MinuteRangeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Vormkracht10\Analytics\Traits\Google;

use Google\Analytics\Data\V1beta\MinuteRange;

trait MinuteRangeTrait
{
public array $minuteRanges = [];

public function setMinuteRange(?string $name, ?int $start, ?int $end): self
{
$this->minuteRanges = [
new MinuteRange([
'name' => $name,
'start_minutes_ago' => $start,
'end_minutes_ago' => $end,
]),
];

return $this;
}

public function setMinuteRanges(array ...$items): self
{
$this->minuteRanges = [];

foreach ($items as $item) {
$this->minuteRanges[] = new MinuteRange([
'name' => $item['name'],
'start_minutes_ago' => $item['start'],
'end_minutes_ago' => $item['end'],
]);
}

return $this;
}

private function validateStartAndEnd(?int $start, ?int $end): void
{
if ($start > $end) {
throw new \Exception('Start cannot be greater than end');
}
}
}
9 changes: 5 additions & 4 deletions src/Traits/ResponseFormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vormkracht10\Analytics\Traits;

use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
use Google\Analytics\Data\V1beta\RunReportResponse;
use Vormkracht10\Analytics\AnalyticsResponse;

Expand All @@ -11,7 +12,7 @@ trait ResponseFormatterTrait

public array $dimensionHeaders = [];

public function formatResponse(RunReportResponse $response): AnalyticsResponse
public function formatResponse(RunReportResponse|RunRealtimeReportResponse $response): AnalyticsResponse
{
$this->setDimensionAndMetricHeaders($response);

Expand All @@ -21,7 +22,7 @@ public function formatResponse(RunReportResponse $response): AnalyticsResponse
->setMetricAggregationsTable($this->getMetricAggregationsTable($response));
}

private function setDimensionAndMetricHeaders(RunReportResponse $response): void
private function setDimensionAndMetricHeaders(RunReportResponse|RunRealtimeReportResponse $response): void
{
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
$this->dimensionHeaders[] = $dimensionHeader->getName();
Expand All @@ -32,7 +33,7 @@ private function setDimensionAndMetricHeaders(RunReportResponse $response): void
}
}

private function getTable(RunReportResponse $response): array
private function getTable(RunReportResponse|RunRealtimeReportResponse $response): array
{
$table = [];

Expand All @@ -53,7 +54,7 @@ private function getTable(RunReportResponse $response): array
return $table;
}

private function getMetricAggregationsTable(RunReportResponse $response): array
private function getMetricAggregationsTable(RunReportResponse|RunRealtimeReportResponse $response): array
{
$aggregationMethods = [
'getTotals',
Expand Down

0 comments on commit 6805ed5

Please sign in to comment.