diff --git a/composer.json b/composer.json index 0a189b8..4950c67 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "google/analytics-data": "^0.8.0", "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0" }, diff --git a/src/LaravelGoogleAnalytics.php b/src/LaravelGoogleAnalytics.php index 05b19d9..837d4aa 100644 --- a/src/LaravelGoogleAnalytics.php +++ b/src/LaravelGoogleAnalytics.php @@ -13,11 +13,14 @@ use AkkiIo\LaravelGoogleAnalytics\Traits\FilterByMetricTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\MetricAggregationTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\MetricTrait; +use AkkiIo\LaravelGoogleAnalytics\Traits\MinuteRangeTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\OrderByDimensionTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\OrderByMetricTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\ResponseTrait; use AkkiIo\LaravelGoogleAnalytics\Traits\RowOperationTrait; use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; +use Google\ApiCore\ApiException; +use Google\ApiCore\ValidationException; class LaravelGoogleAnalytics { @@ -36,7 +39,7 @@ class LaravelGoogleAnalytics use CustomDemographicsTrait; use CustomTechTrait; use ResponseTrait; - + use MinuteRangeTrait; public ?int $propertyId = null; public $credentials = null; public array $orderBys = []; @@ -134,4 +137,29 @@ public function get(): LaravelGoogleAnalyticsResponse return $this->formatResponse($response); } + + /** + * @throws ValidationException + * @throws ApiException + */ + public function getRealTimeReport(): LaravelGoogleAnalyticsResponse + { + + $response = $this->getClient()->runRealtimeReport([ + 'property' => "properties/{$this->getPropertyId()}", + 'minuteRanges' => $this->minuteRanges, + 'metrics' => $this->metrics, + 'dimensions' => $this->dimensions, + 'orderBys' => $this->orderBys, + 'metricAggregations' => $this->metricAggregations, + 'dimensionFilter' => $this->dimensionFilter, + 'metricFilter' => $this->metricFilter, + 'limit' => $this->limit, + 'offset' => $this->offset, + 'keepEmptyRows' => $this->keepEmptyRows, + ]); + + return $this->formatResponse($response); + + } } diff --git a/src/LaravelGoogleAnalyticsResponse.php b/src/LaravelGoogleAnalyticsResponse.php index b88e4d0..8f47be5 100644 --- a/src/LaravelGoogleAnalyticsResponse.php +++ b/src/LaravelGoogleAnalyticsResponse.php @@ -2,11 +2,12 @@ namespace AkkiIo\LaravelGoogleAnalytics; +use Google\Analytics\Data\V1beta\RunRealtimeReportResponse; use Google\Analytics\Data\V1beta\RunReportResponse; class LaravelGoogleAnalyticsResponse { - public RunReportResponse $googleResponse; + public RunReportResponse|RunRealtimeReportResponse $googleResponse; public array $table; @@ -15,10 +16,10 @@ class LaravelGoogleAnalyticsResponse /** * Set google response. * - * @param RunReportResponse $googleResponse + * @param RunReportResponse|RunRealtimeReportResponse $googleResponse * @return $this */ - public function setGoogleResponse(RunReportResponse $googleResponse): self + public function setGoogleResponse(RunReportResponse|RunRealtimeReportResponse $googleResponse): self { $this->googleResponse = $googleResponse; diff --git a/src/Traits/CustomTechTrait.php b/src/Traits/CustomTechTrait.php index d511fc1..978f282 100644 --- a/src/Traits/CustomTechTrait.php +++ b/src/Traits/CustomTechTrait.php @@ -3,13 +3,15 @@ namespace AkkiIo\LaravelGoogleAnalytics\Traits; use AkkiIo\LaravelGoogleAnalytics\Period; +use Google\ApiCore\ApiException; +use Google\ApiCore\ValidationException; trait CustomTechTrait { /** * Get total users by platform. * - * @param Period $period + * @param Period $period * @return array * * @throws \Google\ApiCore\ApiException @@ -28,7 +30,7 @@ public function getTotalUsersByPlatform(Period $period): array /** * Get total users by operating system. * - * @param Period $period + * @param Period $period * @return array * * @throws \Google\ApiCore\ApiException @@ -47,7 +49,7 @@ public function getTotalUsersByOperatingSystem(Period $period): array /** * Get total users by browser. * - * @param Period $period + * @param Period $period * @return array * * @throws \Google\ApiCore\ApiException @@ -66,7 +68,7 @@ public function getTotalUsersByBrowser(Period $period): array /** * Get total users by screen resolution. * - * @param Period $period + * @param Period $period * @return array * * @throws \Google\ApiCore\ApiException @@ -85,8 +87,8 @@ public function getTotalUsersByScreenResolution(Period $period): array /** * Get most users by platform. * - * @param Period $period - * @param int $count + * @param Period $period + * @param int $count * @return array * * @throws \Google\ApiCore\ApiException @@ -106,8 +108,8 @@ public function getMostUsersByPlatform(Period $period, int $count = 20): array /** * Get most users by operating system. * - * @param Period $period - * @param int $count + * @param Period $period + * @param int $count * @return array * * @throws \Google\ApiCore\ApiException @@ -127,8 +129,8 @@ public function getMostUsersByOperatingSystem(Period $period, int $count = 20): /** * Get most users by browser. * - * @param Period $period - * @param int $count + * @param Period $period + * @param int $count * @return array * * @throws \Google\ApiCore\ApiException @@ -148,8 +150,8 @@ public function getMostUsersByBrowser(Period $period, int $count = 20): array /** * Get most users by screen resolution. * - * @param Period $period - * @param int $count + * @param Period $period + * @param int $count * @return array * * @throws \Google\ApiCore\ApiException @@ -165,4 +167,17 @@ public function getMostUsersByScreenResolution(Period $period, int $count = 20): ->get() ->table; } + + /** + * @throws ApiException + * @throws ValidationException + */ + public function getCurrentOnlineUsers() + { + return $this + ->metrics('activeUsers') + ->minuteRange(1) + ->getRealTimeReport() + ->table; + } } diff --git a/src/Traits/DateRangeTrait.php b/src/Traits/DateRangeTrait.php index b0cf624..2daed14 100644 --- a/src/Traits/DateRangeTrait.php +++ b/src/Traits/DateRangeTrait.php @@ -2,6 +2,7 @@ namespace AkkiIo\LaravelGoogleAnalytics\Traits; +use AkkiIo\LaravelGoogleAnalytics\LaravelGoogleAnalytics; use AkkiIo\LaravelGoogleAnalytics\Period; use Google\Analytics\Data\V1beta\DateRange; @@ -27,8 +28,8 @@ public function dateRange(Period $period): self /** * Set the date Ranges. * - * @param Period ...$items - * @return $this + * @param Period ...$items + * @return LaravelGoogleAnalytics|DateRangeTrait */ public function dateRanges(Period ...$items): self { diff --git a/src/Traits/MinuteRangeTrait.php b/src/Traits/MinuteRangeTrait.php new file mode 100644 index 0000000..a183f8e --- /dev/null +++ b/src/Traits/MinuteRangeTrait.php @@ -0,0 +1,21 @@ +minuteRanges[] = (new MinuteRange())->setName( + $start . '-' . $end . '-minutes-ago') + ->setStartMinutesAgo($start) + ->setEndMinutesAgo($end); + + return $this; + } +} diff --git a/src/Traits/ResponseTrait.php b/src/Traits/ResponseTrait.php index 4da12b3..b0e3dbe 100644 --- a/src/Traits/ResponseTrait.php +++ b/src/Traits/ResponseTrait.php @@ -3,6 +3,7 @@ namespace AkkiIo\LaravelGoogleAnalytics\Traits; use AkkiIo\LaravelGoogleAnalytics\LaravelGoogleAnalyticsResponse; +use Google\Analytics\Data\V1beta\RunRealtimeReportResponse; use Google\Analytics\Data\V1beta\RunReportResponse; trait ResponseTrait @@ -16,7 +17,7 @@ trait ResponseTrait * @param RunReportResponse $response * @return LaravelGoogleAnalyticsResponse */ - private function formatResponse(RunReportResponse $response): LaravelGoogleAnalyticsResponse + private function formatResponse(RunReportResponse|RunRealtimeReportResponse $response): LaravelGoogleAnalyticsResponse { $this->setDimensionAndMetricHeaders($response); @@ -32,7 +33,7 @@ private function formatResponse(RunReportResponse $response): LaravelGoogleAnaly * @param RunReportResponse $response * @return array */ - private function getMetricAggregationsTable(RunReportResponse $response): array + private function getMetricAggregationsTable(RunReportResponse|RunRealtimeReportResponse $response): array { $output = []; @@ -63,10 +64,10 @@ private function getMetricAggregationsTable(RunReportResponse $response): array /** * Get table collection. * - * @param RunReportResponse $response + * @param RunReportResponse|RunRealtimeReportResponse $response * @return array */ - private function getTable(RunReportResponse $response): array + private function getTable(RunReportResponse|RunRealtimeReportResponse $response): array { $output = []; @@ -90,7 +91,7 @@ private function getTable(RunReportResponse $response): array * @param RunReportResponse $response * @return void */ - private function setDimensionAndMetricHeaders(RunReportResponse $response) + private function setDimensionAndMetricHeaders(RunReportResponse|RunRealtimeReportResponse $response): void { foreach ($response->getDimensionHeaders() as $header) { $this->dimensionHeaders[] = $header->getName();