Skip to content

Commit

Permalink
Added the functionality for real time reports. Includes 1 ready to us…
Browse files Browse the repository at this point in the history
…e method with name getCurrentOnlineUsers, and real time report builder. Corrected return types accordingly
  • Loading branch information
GeorgeFourkas committed Feb 28, 2023
1 parent 9eceb73 commit 8e00c13
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 29 additions & 1 deletion src/LaravelGoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -36,7 +39,7 @@ class LaravelGoogleAnalytics
use CustomDemographicsTrait;
use CustomTechTrait;
use ResponseTrait;

use MinuteRangeTrait;
public ?int $propertyId = null;
public $credentials = null;
public array $orderBys = [];
Expand Down Expand Up @@ -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);

}
}
7 changes: 4 additions & 3 deletions src/LaravelGoogleAnalyticsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
39 changes: 27 additions & 12 deletions src/Traits/CustomTechTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/Traits/DateRangeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AkkiIo\LaravelGoogleAnalytics\Traits;

use AkkiIo\LaravelGoogleAnalytics\LaravelGoogleAnalytics;
use AkkiIo\LaravelGoogleAnalytics\Period;
use Google\Analytics\Data\V1beta\DateRange;

Expand All @@ -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
{
Expand Down
21 changes: 21 additions & 0 deletions src/Traits/MinuteRangeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AkkiIo\LaravelGoogleAnalytics\Traits;

use AkkiIo\LaravelGoogleAnalytics\Period;
use Google\Analytics\Data\V1beta\MinuteRange;

trait MinuteRangeTrait
{
public array $minuteRanges = [];

public function minuteRange(int $start, int $end = 0): self
{
$this->minuteRanges[] = (new MinuteRange())->setName(
$start . '-' . $end . '-minutes-ago')
->setStartMinutesAgo($start)
->setEndMinutesAgo($end);

return $this;
}
}
11 changes: 6 additions & 5 deletions src/Traits/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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 = [];

Expand Down Expand Up @@ -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 = [];

Expand All @@ -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();
Expand Down

0 comments on commit 8e00c13

Please sign in to comment.