Skip to content

Commit

Permalink
Realtime reports (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslavstefanec authored Dec 11, 2024
1 parent 57bb9b7 commit 1744fb4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,29 @@ public function get(
$metricFilter
);
}

public function getRealtime(
Period $period,
array $metrics,
array $dimensions = [],
int $maxResults = 10,
array $orderBy = [],
int $offset = 0,
?FilterExpression $dimensionFilter = null,
bool $keepEmptyRows = false,
?FilterExpression $metricFilter = null,
): Collection {
return $this->client->getRealtime(
$this->propertyId,
$period,
$metrics,
$dimensions,
$maxResults,
$orderBy,
$offset,
$dimensionFilter,
$keepEmptyRows,
$metricFilter
);
}
}
67 changes: 67 additions & 0 deletions src/AnalyticsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\RunReportResponse;
use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -76,6 +77,56 @@ public function get(
return $result;
}

public function getRealtime(
string $propertyId,
Period $period,
array $metrics,
array $dimensions = [],
int $maxResults = 10,
array $orderBy = [],
int $offset = 0,
?FilterExpression $dimensionFilter = null,
bool $keepEmptyRows = false,
?FilterExpression $metricFilter = null,
): Collection {
$typeCaster = resolve(TypeCaster::class);

$response = $this->runRealtimeReport([
'property' => "properties/{$propertyId}",
'dateRanges' => [
$period->toDateRange(),
],
'metrics' => $this->getFormattedMetrics($metrics),
'dimensions' => $this->getFormattedDimensions($dimensions),
'limit' => $maxResults,
'offset' => $offset,
'orderBys' => $orderBy,
'dimensionFilter' => $dimensionFilter,
'keepEmptyRows' => $keepEmptyRows,
'metricFilter' => $metricFilter,
]);

$result = collect();

foreach ($response->getRows() as $row) {
$rowResult = [];

foreach ($row->getDimensionValues() as $i => $dimensionValue) {
$rowResult[$dimensions[$i]] =
$typeCaster->castValue($dimensions[$i], $dimensionValue->getValue());
}

foreach ($row->getMetricValues() as $i => $metricValue) {
$rowResult[$metrics[$i]] =
$typeCaster->castValue($metrics[$i], $metricValue->getValue());
}

$result->push($rowResult);
}

return $result;
}

public function runReport(array $request): RunReportResponse
{
$cacheName = $this->determineCacheName(func_get_args());
Expand All @@ -91,6 +142,22 @@ public function runReport(array $request): RunReportResponse
);
}

public function runRealtimeReport(array $request): RunRealtimeReportResponse
{
$cacheName = $this->determineCacheName(func_get_args());

if ($this->cacheLifeTimeInMinutes === 0) {
$this->cache->forget($cacheName);
}

return $this->cache->remember(
$cacheName,
$this->cacheLifeTimeInMinutes,
fn () => $this->service->runRealtimeReport($request),
);
}


public function getAnalyticsService(): BetaAnalyticsDataClient
{
return $this->service;
Expand Down

0 comments on commit 1744fb4

Please sign in to comment.