diff --git a/src/Analytics.php b/src/Analytics.php index a4cbee6..847bc63 100755 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -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, @@ -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); } diff --git a/src/AnalyticsResponse.php b/src/AnalyticsResponse.php index 05408e4..cada18d 100644 --- a/src/AnalyticsResponse.php +++ b/src/AnalyticsResponse.php @@ -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; diff --git a/src/Service/GoogleAnalyticsService.php b/src/Service/GoogleAnalyticsService.php index 5c41a6e..6dfc2f9 100644 --- a/src/Service/GoogleAnalyticsService.php +++ b/src/Service/GoogleAnalyticsService.php @@ -7,6 +7,7 @@ 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; @@ -14,6 +15,7 @@ class GoogleAnalyticsService { use DateRangeTrait, + MinuteRangeTrait, MetricTrait, DimensionTrait, OrderByTrait, diff --git a/src/Traits/Analytics/RealtimeAnalytics.php b/src/Traits/Analytics/RealtimeAnalytics.php index 5265657..12e26ba 100644 --- a/src/Traits/Analytics/RealtimeAnalytics.php +++ b/src/Traits/Analytics/RealtimeAnalytics.php @@ -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)); diff --git a/src/Traits/Google/MinuteRangeTrait.php b/src/Traits/Google/MinuteRangeTrait.php new file mode 100644 index 0000000..bef9cd6 --- /dev/null +++ b/src/Traits/Google/MinuteRangeTrait.php @@ -0,0 +1,45 @@ +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'); + } + } +} diff --git a/src/Traits/ResponseFormatterTrait.php b/src/Traits/ResponseFormatterTrait.php index b358d30..0cd55ce 100644 --- a/src/Traits/ResponseFormatterTrait.php +++ b/src/Traits/ResponseFormatterTrait.php @@ -2,6 +2,7 @@ namespace Vormkracht10\Analytics\Traits; +use Google\Analytics\Data\V1beta\RunRealtimeReportResponse; use Google\Analytics\Data\V1beta\RunReportResponse; use Vormkracht10\Analytics\AnalyticsResponse; @@ -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); @@ -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(); @@ -32,7 +33,7 @@ private function setDimensionAndMetricHeaders(RunReportResponse $response): void } } - private function getTable(RunReportResponse $response): array + private function getTable(RunReportResponse|RunRealtimeReportResponse $response): array { $table = []; @@ -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',