Commit f51dd710b9bfd753db0fc65744dd54bd1a63b474
1 parent
3c070e2b
dimensions +dynamics +multiple
Showing
2 changed files
with
14 additions
and
3 deletions
Show diff stats
app/library/App/Controllers/GaController.php
@@ -12,6 +12,7 @@ namespace App\Controllers; | @@ -12,6 +12,7 @@ namespace App\Controllers; | ||
12 | use Google_Client; | 12 | use Google_Client; |
13 | use Google_Service_AnalyticsReporting; | 13 | use Google_Service_AnalyticsReporting; |
14 | use Google_Service_AnalyticsReporting_DateRange; | 14 | use Google_Service_AnalyticsReporting_DateRange; |
15 | +use Google_Service_AnalyticsReporting_Dimension; | ||
15 | use Google_Service_AnalyticsReporting_GetReportsRequest; | 16 | use Google_Service_AnalyticsReporting_GetReportsRequest; |
16 | use Google_Service_AnalyticsReporting_Metric; | 17 | use Google_Service_AnalyticsReporting_Metric; |
17 | use Google_Service_AnalyticsReporting_ReportRequest; | 18 | use Google_Service_AnalyticsReporting_ReportRequest; |
@@ -27,6 +28,7 @@ class GaController extends CrudResourceController { | @@ -27,6 +28,7 @@ class GaController extends CrudResourceController { | ||
27 | $get_metrics = $this->request->get('metric') ?? 'users'; | 28 | $get_metrics = $this->request->get('metric') ?? 'users'; |
28 | $get_start_date = $this->request->get('start') ?? '30daysAgo'; | 29 | $get_start_date = $this->request->get('start') ?? '30daysAgo'; |
29 | $get_end_date = $this->request->get('end') ?? 'today'; | 30 | $get_end_date = $this->request->get('end') ?? 'today'; |
31 | + $get_dimension = $this->request->get('dimension') ?? 'browser'; | ||
30 | 32 | ||
31 | 33 | ||
32 | putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); | 34 | putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); |
@@ -50,12 +52,22 @@ class GaController extends CrudResourceController { | @@ -50,12 +52,22 @@ class GaController extends CrudResourceController { | ||
50 | $metrics[] = $metrics_obj; | 52 | $metrics[] = $metrics_obj; |
51 | } | 53 | } |
52 | 54 | ||
55 | + //Create the Dimensions object. | ||
56 | + $dimension = []; | ||
57 | + $get_dimension = explode(',', $get_dimension); | ||
58 | + foreach ($get_dimension as $dimension) { | ||
59 | + $dimension_obj = new Google_Service_AnalyticsReporting_Dimension(); | ||
60 | + $dimension_obj->setName("ga:".$get_dimension); | ||
61 | + $dimension[] = $dimension_obj; | ||
62 | + } | ||
63 | + | ||
53 | 64 | ||
54 | // Создание объекта ReportRequest. | 65 | // Создание объекта ReportRequest. |
55 | $request = new Google_Service_AnalyticsReporting_ReportRequest(); | 66 | $request = new Google_Service_AnalyticsReporting_ReportRequest(); |
56 | $request->setViewId(self::VIEW_ID); | 67 | $request->setViewId(self::VIEW_ID); |
57 | $request->setDateRanges($dateRange); | 68 | $request->setDateRanges($dateRange); |
58 | $request->setMetrics(array($metrics)); | 69 | $request->setMetrics(array($metrics)); |
70 | + $request->setDimensions(array($dimension)); | ||
59 | 71 | ||
60 | $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); | 72 | $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); |
61 | $body->setReportRequests( array( $request) ); | 73 | $body->setReportRequests( array( $request) ); |
app/library/App/Resources/GaResource.php
@@ -20,7 +20,6 @@ class GaResource extends ApiResource { | @@ -20,7 +20,6 @@ class GaResource extends ApiResource { | ||
20 | 20 | ||
21 | $this | 21 | $this |
22 | ->name('Google Analytics') | 22 | ->name('Google Analytics') |
23 | - //->model(Project::class) | ||
24 | ->expectsJsonData() | 23 | ->expectsJsonData() |
25 | //->transformer(ModelTransformer::class) | 24 | //->transformer(ModelTransformer::class) |
26 | ->itemKey('ga') | 25 | ->itemKey('ga') |
@@ -28,12 +27,12 @@ class GaResource extends ApiResource { | @@ -28,12 +27,12 @@ class GaResource extends ApiResource { | ||
28 | ->deny(AclRoles::UNAUTHORIZED) | 27 | ->deny(AclRoles::UNAUTHORIZED) |
29 | ->handler(GaController::class) | 28 | ->handler(GaController::class) |
30 | 29 | ||
31 | - ->endpoint(ApiEndpoint::get('/', 'getAction') | 30 | + ->endpoint(ApiEndpoint::get('', 'getAction') |
32 | ->allow(AclRoles::USER) | 31 | ->allow(AclRoles::USER) |
33 | ->description('Returns data from Google Analytics Api') | 32 | ->description('Returns data from Google Analytics Api') |
34 | ->exampleResponse([ | 33 | ->exampleResponse([ |
35 | 'lifehack' => 'for example request data', | 34 | 'lifehack' => 'for example request data', |
36 | - 'start' => '30daysAgo', | 35 | + 'start' => '30daysAgo / 2015-03-01', |
37 | 'end' => 'today', | 36 | 'end' => 'today', |
38 | 'metric' => 'user,sessions' | 37 | 'metric' => 'user,sessions' |
39 | ]) | 38 | ]) |