Commit 29f4a05f77a58c5f1d7edbd9aa3b336904d178d0
1 parent
e480c54f
multiple metrics
Showing
1 changed file
with
24 additions
and
25 deletions
Show diff stats
app/library/App/Controllers/GaController.php
... | ... | @@ -17,6 +17,7 @@ use Google_Service_AnalyticsReporting_Metric; |
17 | 17 | use Google_Service_AnalyticsReporting_ReportRequest; |
18 | 18 | use PhalconRest\Mvc\Controllers\CrudResourceController; |
19 | 19 | use Google_Service_AnalyticsReporting_Report; |
20 | +use phpDocumentor\Reflection\Types\Array_; | |
20 | 21 | |
21 | 22 | class GaController extends CrudResourceController { |
22 | 23 | |
... | ... | @@ -25,7 +26,8 @@ class GaController extends CrudResourceController { |
25 | 26 | |
26 | 27 | public function getAction() { |
27 | 28 | |
28 | - $getMetric = $this->request->get('metric'); | |
29 | + $get_metrics = $this->request->get('metric'); | |
30 | + $get_metrics = explode(',', $get_metrics); | |
29 | 31 | |
30 | 32 | putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); |
31 | 33 | $client = new Google_Client(); |
... | ... | @@ -33,35 +35,32 @@ class GaController extends CrudResourceController { |
33 | 35 | $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); |
34 | 36 | $analytics = new Google_Service_AnalyticsReporting($client); |
35 | 37 | |
36 | - $param[] = ['metric' => 'ga:'.$getMetric, 'alias' => $getMetric]; | |
37 | - | |
38 | - $response = []; | |
39 | - | |
40 | - foreach ($param as $item) { | |
41 | - | |
42 | - // Создание объекта DateRange. | |
43 | - $dateRange = new Google_Service_AnalyticsReporting_DateRange(); | |
44 | - $dateRange->setStartDate("30daysAgo"); | |
45 | - $dateRange->setEndDate("today"); | |
38 | + // Создание объекта DateRange. | |
39 | + $dateRange = new Google_Service_AnalyticsReporting_DateRange(); | |
40 | + $dateRange->setStartDate("30daysAgo"); | |
41 | + $dateRange->setEndDate("today"); | |
42 | + | |
43 | + // Создание объекта Metrics. | |
44 | + $metrics = []; | |
45 | + foreach ($get_metrics as $metric) { | |
46 | + $metrics_obj = new Google_Service_AnalyticsReporting_Metric(); | |
47 | + $metrics_obj->setExpression('ga:'.$metric); | |
48 | + $metrics_obj->setAlias($metric); | |
49 | + $metrics[] = $metrics_obj; | |
50 | + } | |
46 | 51 | |
47 | - // Создание объекта Metrics. | |
48 | - $metrics = new Google_Service_AnalyticsReporting_Metric(); | |
49 | - $metrics->setExpression($item['metric'].',ga:sessionsPerUser'); | |
50 | - //$metrics->setExpression('ga:pageviewsPerSession'); | |
51 | - ///$sessions->setAlias($item['alias']); | |
52 | 52 | |
53 | + // Создание объекта ReportRequest. | |
54 | + $request = new Google_Service_AnalyticsReporting_ReportRequest(); | |
55 | + $request->setViewId(self::VIEW_ID); | |
56 | + $request->setDateRanges($dateRange); | |
57 | + $request->setMetrics(array($metrics)); | |
53 | 58 | |
54 | - // Создание объекта ReportRequest. | |
55 | - $request = new Google_Service_AnalyticsReporting_ReportRequest(); | |
56 | - $request->setViewId(self::VIEW_ID); | |
57 | - $request->setDateRanges($dateRange); | |
58 | - $request->setMetrics(array($metrics)); | |
59 | + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); | |
60 | + $body->setReportRequests( array( $request) ); | |
59 | 61 | |
60 | - $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); | |
61 | - $body->setReportRequests( array( $request) ); | |
62 | + $response = $analytics->reports->batchGet( $body ); | |
62 | 63 | |
63 | - $response = $analytics->reports->batchGet( $body ); | |
64 | - } | |
65 | 64 | |
66 | 65 | return $response->toSimpleObject(); |
67 | 66 | ... | ... |