From 897d06c399ceb5cb420fdb420ae5bebbd5c418e5 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Feb 2017 18:35:38 +0200 Subject: [PATCH] generate GaResource --- app/library/App/Bootstrap/RouteBootstrap.php | 2 -- app/library/App/Controllers/GaController.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- app/library/App/Resources/GaResource.php | 2 +- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/app/library/App/Bootstrap/RouteBootstrap.php b/app/library/App/Bootstrap/RouteBootstrap.php index 3fda244..619f306 100755 --- a/app/library/App/Bootstrap/RouteBootstrap.php +++ b/app/library/App/Bootstrap/RouteBootstrap.php @@ -60,8 +60,6 @@ class RouteBootstrap implements BootstrapInterface $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); - $api_key = 'AIzaSyBNYr7n7bcHbO-sJ9xuWwS9xQ2OY5aFAiE'; - $client->setDeveloperKey($api_key); $analytics = new Google_Service_AnalyticsReporting($client); $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии']; diff --git a/app/library/App/Controllers/GaController.php b/app/library/App/Controllers/GaController.php index 5141f34..95d6b42 100644 --- a/app/library/App/Controllers/GaController.php +++ b/app/library/App/Controllers/GaController.php @@ -9,13 +9,60 @@ namespace App\Controllers; +use Google_Client; +use Google_Service_AnalyticsReporting; +use Google_Service_AnalyticsReporting_DateRange; +use Google_Service_AnalyticsReporting_GetReportsRequest; +use Google_Service_AnalyticsReporting_Metric; +use Google_Service_AnalyticsReporting_ReportRequest; use PhalconRest\Mvc\Controllers\CrudResourceController; class GaController extends CrudResourceController { - public function gaRequest() { + const SECRET_JSON = 'ca4a1bd8aa14.json'; + const VIEW_ID = '119240817'; - return "123"; + public function getAction() { + + putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); + $client = new Google_Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); + $api_key = 'AIzaSyBNYr7n7bcHbO-sJ9xuWwS9xQ2OY5aFAiE'; + $client->setDeveloperKey($api_key); + $analytics = new Google_Service_AnalyticsReporting($client); + + $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии']; + + $response = []; + + foreach ($param as $item) { + + // Создание объекта DateRange. + $dateRange = new Google_Service_AnalyticsReporting_DateRange(); + $dateRange->setStartDate("30daysAgo"); + $dateRange->setEndDate("today"); + + // Создание объекта Metrics. + $sessions = new Google_Service_AnalyticsReporting_Metric(); + $sessions->setExpression($item['metric']); + $sessions->setAlias($item['alias']); + + + // Создание объекта ReportRequest. + $request = new Google_Service_AnalyticsReporting_ReportRequest(); + $request->setViewId(self::VIEW_ID); + $request->setDateRanges($dateRange); + $request->setMetrics(array($sessions)); + + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); + $body->setReportRequests( array( $request) ); + + $response = $analytics->reports->batchGet( $body ); + } + + + $this->createArrayResponse($response, 'data'); } diff --git a/app/library/App/Resources/GaResource.php b/app/library/App/Resources/GaResource.php index bcf258d..c32f113 100644 --- a/app/library/App/Resources/GaResource.php +++ b/app/library/App/Resources/GaResource.php @@ -28,7 +28,7 @@ class GaResource extends ApiResource { ->deny(AclRoles::UNAUTHORIZED) ->handler(GaController::class) - ->endpoint(ApiEndpoint::get('/ga', 'gaRequest') + ->endpoint(ApiEndpoint::get('/', 'getAction') ->allow(AclRoles::USER) ->description('Returns data from Google Analytics Api') ) -- libgit2 0.21.4