Skip to content

Commit

Permalink
Merge pull request #2 from salman-mahmood/main
Browse files Browse the repository at this point in the history
Exception Handling for Analytics
  • Loading branch information
zfhassaan authored May 3, 2023
2 parents 1fd70d6 + c50ef76 commit b9ea6cb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 28 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,52 @@ $active_users = $analytics->runReports($period,['name' => $state],['name' => 'ac
In addition to running reports, Genlytics also provides a method to fetch real-time analytics data using runRealTime function. For example:

```php
$analytics->runRealTime(['name' => 'pagePath'],['name' => 'pageviews']);

// For Single Dimension and Metric
$dimensions = ['name' => 'browser']; //1
$metrics = ['name' => 'activeUsers']; //1
$period = [['start_date' => '30daysAgo', 'end_date' => 'today']];
$result = $analytics->runReports($period, $dimensions, $metrics);

// For Multiple Dimensions and Metrics
try {
$analytics = new Genlytics();

$dimensions = [
['name' => 'browser'], //1
['name' => 'country'], //2
['name' => 'date'], //3
['name' => 'city'], //4
['name' => 'dateHour'], //5
['name' => 'firstUserSourceMedium'], //6
['name' => 'mobileDeviceMarketingName'], //7
['name' => 'operatingSystemWithVersion'], //8
['name' => 'dayOfWeek'], //9
['name' => 'defaultChannelGroup'], //10
['name' => 'language'], //11
['name' => 'dayOfWeekName'], //12
['name' => 'deviceCategory'], //13
['name' => 'contentGroup'], //14
['name' => 'fullPageUrl'], //15
];

$metrics = [
['name' => 'activeUsers'], //1
['name' => 'engagedSessions'], //2
];
// Period can be from any range to any range which can be checked from the GA Query Builder
$period = [['start_date' => '30daysAgo', 'end_date' => 'today']];
$results = array_map(function ($d) use ($analytics, $metrics, $period) {
return array_map(function ($m) use ($analytics, $d, $period) {
$result = $analytics->runReports($period, $d, $m);
return $result->content();
}, $metrics);
}, $dimensions);

return response()->json(['analytics' => $results]);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
```
Genlytics also provides a method RunDimensionReport to fetch the dimension, for example:

Expand Down
75 changes: 48 additions & 27 deletions src/Genlytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Google\Analytics\Data\V1beta\Metric;
use Google\Auth\CredentialsLoader;
use Google\Service\AnalyticsReporting\DimensionFilter;
use Throwable;

class Genlytics
{
Expand All @@ -21,7 +22,8 @@ class Genlytics
/**
* Default Constructor for Genlytics
*/
public function __construct(){
public function __construct()
{
$this->initConfig();
}

Expand All @@ -30,7 +32,7 @@ public function __construct(){
*/
public function initConfig(): void
{
$this->property_id = 'properties/'.config('analytics.property_id');
$this->property_id = 'properties/' . config('analytics.property_id');
$this->client = new BetaAnalyticsDataClient();
}

Expand All @@ -45,15 +47,22 @@ public function initConfig(): void
* @return JsonResponse
* @throws ApiException
*/
public function runReports(Array $period, Array $dimension, Array $metrics ): JsonResponse
public function runReports(array $period, array $dimension, array $metrics): JsonResponse
{
$response = $this->client->runReport([
'property' => $this->property_id,
'dateRanges' => [new DateRange($period)],
'dimensions' => [new Dimension($dimension)],
'metrics' => [new Metric($metrics)]
]);
return $this->returnJson($response);

try {
$options = [
'property' => $this->property_id,
'dateRanges' => [new DateRange($period[0])],
'dimensions' => [new Dimension($dimension)],
'metrics' => [new Metric($metrics)]
];

$response = $this->client->runReport($options);
return $this->returnJson($response);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
}

/**
Expand All @@ -63,14 +72,18 @@ public function runReports(Array $period, Array $dimension, Array $metrics ): Js
* @param array $metrics
* @return JsonResponse
*/
public function runRealTime(Array $dimension, Array $metrics): JsonResponse
public function runRealTime(array $dimension, array $metrics): JsonResponse
{
$response = $this->client->runRealtimeReport([
'property'=> $this->property_id,
'dimensions'=>[new Dimension($dimension)],
'metrics'=>[new Metric($metrics)]
]);
return $this->returnJson($response);
try {
$response = $this->client->runRealtimeReport([
'property' => $this->property_id,
'dimensions' => [new Dimension($dimension)],
'metrics' => [new Metric($metrics)]
]);
return $this->returnJson($response);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
}


Expand All @@ -81,13 +94,17 @@ public function runRealTime(Array $dimension, Array $metrics): JsonResponse
* @param $dimension
* @return mixed
*/
public function RunDimensionReport(Array $period,$dimension): mixed
public function RunDimensionReport(array $period, $dimension): mixed
{
return $this->client->runReport([
'property' => $this->property_id,
'dateRanges' => [new DateRange($period)],
'dimensions' => [new Dimension($dimension)]
]);
try {
return $this->client->runReport([
'property' => $this->property_id,
'dateRanges' => [new DateRange($period)],
'dimensions' => [new Dimension($dimension)]
]);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
}

/**
Expand All @@ -98,10 +115,14 @@ public function RunDimensionReport(Array $period,$dimension): mixed
*/
protected function returnJson($response): JsonResponse
{
$result = [];
foreach ($response->getRows() as $row) {
$result[] = ['dimension' => $row->getDImensionValues()[0]->getValue(), 'metric' => $row->getMetricValues()[0]->getValue()];
try {
$result = [];
foreach ($response->getRows() as $row) {
$result[] = ['dimension' => $row->getDImensionValues()[0]->getValue(), 'metric' => $row->getMetricValues()[0]->getValue()];
}
return response()->json($result);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
return response()->json($result);
}
}

0 comments on commit b9ea6cb

Please sign in to comment.