From 1744fb45aa63834894c44331a00ca4e91172d924 Mon Sep 17 00:00:00 2001 From: Jaroslav Stefanec <55752260+jaroslavstefanec@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:19:37 +0100 Subject: [PATCH] Realtime reports (#528) --- src/Analytics.php | 25 +++++++++++++++ src/AnalyticsClient.php | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/Analytics.php b/src/Analytics.php index 5fa133e..60ea232 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -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 + ); + } } diff --git a/src/AnalyticsClient.php b/src/AnalyticsClient.php index 02d5454..62094df 100644 --- a/src/AnalyticsClient.php +++ b/src/AnalyticsClient.php @@ -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; @@ -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()); @@ -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;