Commit f51dd710b9bfd753db0fc65744dd54bd1a63b474

Authored by Alex Savenko
1 parent 3c070e2b

dimensions +dynamics +multiple

app/library/App/Controllers/GaController.php
... ... @@ -12,6 +12,7 @@ namespace App\Controllers;
12 12 use Google_Client;
13 13 use Google_Service_AnalyticsReporting;
14 14 use Google_Service_AnalyticsReporting_DateRange;
  15 +use Google_Service_AnalyticsReporting_Dimension;
15 16 use Google_Service_AnalyticsReporting_GetReportsRequest;
16 17 use Google_Service_AnalyticsReporting_Metric;
17 18 use Google_Service_AnalyticsReporting_ReportRequest;
... ... @@ -27,6 +28,7 @@ class GaController extends CrudResourceController {
27 28 $get_metrics = $this->request->get('metric') ?? 'users';
28 29 $get_start_date = $this->request->get('start') ?? '30daysAgo';
29 30 $get_end_date = $this->request->get('end') ?? 'today';
  31 + $get_dimension = $this->request->get('dimension') ?? 'browser';
30 32  
31 33  
32 34 putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON);
... ... @@ -50,12 +52,22 @@ class GaController extends CrudResourceController {
50 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 65 // Создание объекта ReportRequest.
55 66 $request = new Google_Service_AnalyticsReporting_ReportRequest();
56 67 $request->setViewId(self::VIEW_ID);
57 68 $request->setDateRanges($dateRange);
58 69 $request->setMetrics(array($metrics));
  70 + $request->setDimensions(array($dimension));
59 71  
60 72 $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
61 73 $body->setReportRequests( array( $request) );
... ...
app/library/App/Resources/GaResource.php
... ... @@ -20,7 +20,6 @@ class GaResource extends ApiResource {
20 20  
21 21 $this
22 22 ->name('Google Analytics')
23   - //->model(Project::class)
24 23 ->expectsJsonData()
25 24 //->transformer(ModelTransformer::class)
26 25 ->itemKey('ga')
... ... @@ -28,12 +27,12 @@ class GaResource extends ApiResource {
28 27 ->deny(AclRoles::UNAUTHORIZED)
29 28 ->handler(GaController::class)
30 29  
31   - ->endpoint(ApiEndpoint::get('/', 'getAction')
  30 + ->endpoint(ApiEndpoint::get('', 'getAction')
32 31 ->allow(AclRoles::USER)
33 32 ->description('Returns data from Google Analytics Api')
34 33 ->exampleResponse([
35 34 'lifehack' => 'for example request data',
36   - 'start' => '30daysAgo',
  35 + 'start' => '30daysAgo / 2015-03-01',
37 36 'end' => 'today',
38 37 'metric' => 'user,sessions'
39 38 ])
... ...