Commit 180e10beaca9f5fb3a12723fd4d3f57f4a81c17f
1 parent
66d82a12
dynamic dateRange
Showing
2 changed files
with
8 additions
and
68 deletions
Show diff stats
app/library/App/Controllers/GaController.php
| ... | ... | @@ -16,8 +16,6 @@ use Google_Service_AnalyticsReporting_GetReportsRequest; |
| 16 | 16 | use Google_Service_AnalyticsReporting_Metric; |
| 17 | 17 | use Google_Service_AnalyticsReporting_ReportRequest; |
| 18 | 18 | use PhalconRest\Mvc\Controllers\CrudResourceController; |
| 19 | -use Google_Service_AnalyticsReporting_Report; | |
| 20 | -use phpDocumentor\Reflection\Types\Array_; | |
| 21 | 19 | |
| 22 | 20 | class GaController extends CrudResourceController { |
| 23 | 21 | |
| ... | ... | @@ -26,8 +24,10 @@ class GaController extends CrudResourceController { |
| 26 | 24 | |
| 27 | 25 | public function getAction() { |
| 28 | 26 | |
| 29 | - $get_metrics = $this->request->get('metric'); | |
| 30 | - $get_metrics = explode(',', $get_metrics); | |
| 27 | + $get_metrics = $this->request->get('metric') ?? 'users'; | |
| 28 | + $get_start_date = $this->request->get('start') ?? '30daysAgo'; | |
| 29 | + $get_end_date = $this->request->get('end') ?? 'today'; | |
| 30 | + | |
| 31 | 31 | |
| 32 | 32 | putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); |
| 33 | 33 | $client = new Google_Client(); |
| ... | ... | @@ -37,11 +37,12 @@ class GaController extends CrudResourceController { |
| 37 | 37 | |
| 38 | 38 | // Создание объекта DateRange. |
| 39 | 39 | $dateRange = new Google_Service_AnalyticsReporting_DateRange(); |
| 40 | - $dateRange->setStartDate("30daysAgo"); | |
| 41 | - $dateRange->setEndDate("today"); | |
| 40 | + $dateRange->setStartDate($get_start_date); | |
| 41 | + $dateRange->setEndDate($get_end_date); | |
| 42 | 42 | |
| 43 | 43 | // Создание объекта Metrics. |
| 44 | 44 | $metrics = []; |
| 45 | + $get_metrics = explode(',', $get_metrics); | |
| 45 | 46 | foreach ($get_metrics as $metric) { |
| 46 | 47 | $metrics_obj = new Google_Service_AnalyticsReporting_Metric(); |
| 47 | 48 | $metrics_obj->setExpression('ga:'.$metric); | ... | ... |
app/library/App/Resources/GaResource.php
| ... | ... | @@ -28,70 +28,9 @@ class GaResource extends ApiResource { |
| 28 | 28 | ->deny(AclRoles::UNAUTHORIZED) |
| 29 | 29 | ->handler(GaController::class) |
| 30 | 30 | |
| 31 | - ->endpoint(ApiEndpoint::get('?{metric}', 'getAction') | |
| 31 | + ->endpoint(ApiEndpoint::get('?start&metric={metric1},{metric2}', 'getAction') | |
| 32 | 32 | ->allow(AclRoles::USER) |
| 33 | 33 | ->description('Returns data from Google Analytics Api') |
| 34 | - ->exampleResponse(' | |
| 35 | - { | |
| 36 | - "reports": [ | |
| 37 | - { | |
| 38 | - "columnHeader": { | |
| 39 | - "metricHeader": { | |
| 40 | - "metricHeaderEntries": [ | |
| 41 | - { | |
| 42 | - "name": "users", | |
| 43 | - "type": "INTEGER" | |
| 44 | - }, | |
| 45 | - { | |
| 46 | - "name": "sessionsPerUser", | |
| 47 | - "type": "FLOAT" | |
| 48 | - } | |
| 49 | - ] | |
| 50 | - } | |
| 51 | - }, | |
| 52 | - "data": { | |
| 53 | - "rows": [ | |
| 54 | - { | |
| 55 | - "metrics": [ | |
| 56 | - { | |
| 57 | - "values": [ | |
| 58 | - "4814", | |
| 59 | - "1.23764021603656" | |
| 60 | - ] | |
| 61 | - } | |
| 62 | - ] | |
| 63 | - } | |
| 64 | - ], | |
| 65 | - "totals": [ | |
| 66 | - { | |
| 67 | - "values": [ | |
| 68 | - "4814", | |
| 69 | - "1.23764021603656" | |
| 70 | - ] | |
| 71 | - } | |
| 72 | - ], | |
| 73 | - "rowCount": 1, | |
| 74 | - "minimums": [ | |
| 75 | - { | |
| 76 | - "values": [ | |
| 77 | - "4814", | |
| 78 | - "1.23764021603656" | |
| 79 | - ] | |
| 80 | - } | |
| 81 | - ], | |
| 82 | - "maximums": [ | |
| 83 | - { | |
| 84 | - "values": [ | |
| 85 | - "4814", | |
| 86 | - "1.23764021603656" | |
| 87 | - ] | |
| 88 | - } | |
| 89 | - ] | |
| 90 | - } | |
| 91 | - } | |
| 92 | - ] | |
| 93 | - } | |
| 94 | - ') | |
| 95 | 34 | ) |
| 96 | 35 | ; |
| 97 | 36 | ... | ... |