From 180e10beaca9f5fb3a12723fd4d3f57f4a81c17f Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 10 Feb 2017 17:35:10 +0200 Subject: [PATCH] dynamic dateRange --- app/library/App/Controllers/GaController.php | 13 +++++++------ app/library/App/Resources/GaResource.php | 63 +-------------------------------------------------------------- 2 files changed, 8 insertions(+), 68 deletions(-) diff --git a/app/library/App/Controllers/GaController.php b/app/library/App/Controllers/GaController.php index 257db65..6979211 100644 --- a/app/library/App/Controllers/GaController.php +++ b/app/library/App/Controllers/GaController.php @@ -16,8 +16,6 @@ use Google_Service_AnalyticsReporting_GetReportsRequest; use Google_Service_AnalyticsReporting_Metric; use Google_Service_AnalyticsReporting_ReportRequest; use PhalconRest\Mvc\Controllers\CrudResourceController; -use Google_Service_AnalyticsReporting_Report; -use phpDocumentor\Reflection\Types\Array_; class GaController extends CrudResourceController { @@ -26,8 +24,10 @@ class GaController extends CrudResourceController { public function getAction() { - $get_metrics = $this->request->get('metric'); - $get_metrics = explode(',', $get_metrics); + $get_metrics = $this->request->get('metric') ?? 'users'; + $get_start_date = $this->request->get('start') ?? '30daysAgo'; + $get_end_date = $this->request->get('end') ?? 'today'; + putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); $client = new Google_Client(); @@ -37,11 +37,12 @@ class GaController extends CrudResourceController { // Создание объекта DateRange. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); - $dateRange->setStartDate("30daysAgo"); - $dateRange->setEndDate("today"); + $dateRange->setStartDate($get_start_date); + $dateRange->setEndDate($get_end_date); // Создание объекта Metrics. $metrics = []; + $get_metrics = explode(',', $get_metrics); foreach ($get_metrics as $metric) { $metrics_obj = new Google_Service_AnalyticsReporting_Metric(); $metrics_obj->setExpression('ga:'.$metric); diff --git a/app/library/App/Resources/GaResource.php b/app/library/App/Resources/GaResource.php index ccdd9be..1e378e7 100644 --- a/app/library/App/Resources/GaResource.php +++ b/app/library/App/Resources/GaResource.php @@ -28,70 +28,9 @@ class GaResource extends ApiResource { ->deny(AclRoles::UNAUTHORIZED) ->handler(GaController::class) - ->endpoint(ApiEndpoint::get('?{metric}', 'getAction') + ->endpoint(ApiEndpoint::get('?start&metric={metric1},{metric2}', 'getAction') ->allow(AclRoles::USER) ->description('Returns data from Google Analytics Api') - ->exampleResponse(' - { - "reports": [ - { - "columnHeader": { - "metricHeader": { - "metricHeaderEntries": [ - { - "name": "users", - "type": "INTEGER" - }, - { - "name": "sessionsPerUser", - "type": "FLOAT" - } - ] - } - }, - "data": { - "rows": [ - { - "metrics": [ - { - "values": [ - "4814", - "1.23764021603656" - ] - } - ] - } - ], - "totals": [ - { - "values": [ - "4814", - "1.23764021603656" - ] - } - ], - "rowCount": 1, - "minimums": [ - { - "values": [ - "4814", - "1.23764021603656" - ] - } - ], - "maximums": [ - { - "values": [ - "4814", - "1.23764021603656" - ] - } - ] - } - } - ] - } - ') ) ; -- libgit2 0.21.4