Commit 897d06c399ceb5cb420fdb420ae5bebbd5c418e5
1 parent
5b99f932
generate GaResource
Showing
3 changed files
with
50 additions
and
5 deletions
Show diff stats
app/library/App/Bootstrap/RouteBootstrap.php
... | ... | @@ -60,8 +60,6 @@ class RouteBootstrap implements BootstrapInterface |
60 | 60 | $client = new Google_Client(); |
61 | 61 | $client->useApplicationDefaultCredentials(); |
62 | 62 | $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); |
63 | - $api_key = 'AIzaSyBNYr7n7bcHbO-sJ9xuWwS9xQ2OY5aFAiE'; | |
64 | - $client->setDeveloperKey($api_key); | |
65 | 63 | $analytics = new Google_Service_AnalyticsReporting($client); |
66 | 64 | |
67 | 65 | $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии']; | ... | ... |
app/library/App/Controllers/GaController.php
... | ... | @@ -9,13 +9,60 @@ |
9 | 9 | namespace App\Controllers; |
10 | 10 | |
11 | 11 | |
12 | +use Google_Client; | |
13 | +use Google_Service_AnalyticsReporting; | |
14 | +use Google_Service_AnalyticsReporting_DateRange; | |
15 | +use Google_Service_AnalyticsReporting_GetReportsRequest; | |
16 | +use Google_Service_AnalyticsReporting_Metric; | |
17 | +use Google_Service_AnalyticsReporting_ReportRequest; | |
12 | 18 | use PhalconRest\Mvc\Controllers\CrudResourceController; |
13 | 19 | |
14 | 20 | class GaController extends CrudResourceController { |
15 | 21 | |
16 | - public function gaRequest() { | |
22 | + const SECRET_JSON = 'ca4a1bd8aa14.json'; | |
23 | + const VIEW_ID = '119240817'; | |
17 | 24 | |
18 | - return "123"; | |
25 | + public function getAction() { | |
26 | + | |
27 | + putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); | |
28 | + $client = new Google_Client(); | |
29 | + $client->useApplicationDefaultCredentials(); | |
30 | + $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); | |
31 | + $api_key = 'AIzaSyBNYr7n7bcHbO-sJ9xuWwS9xQ2OY5aFAiE'; | |
32 | + $client->setDeveloperKey($api_key); | |
33 | + $analytics = new Google_Service_AnalyticsReporting($client); | |
34 | + | |
35 | + $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии']; | |
36 | + | |
37 | + $response = []; | |
38 | + | |
39 | + foreach ($param as $item) { | |
40 | + | |
41 | + // Создание объекта DateRange. | |
42 | + $dateRange = new Google_Service_AnalyticsReporting_DateRange(); | |
43 | + $dateRange->setStartDate("30daysAgo"); | |
44 | + $dateRange->setEndDate("today"); | |
45 | + | |
46 | + // Создание объекта Metrics. | |
47 | + $sessions = new Google_Service_AnalyticsReporting_Metric(); | |
48 | + $sessions->setExpression($item['metric']); | |
49 | + $sessions->setAlias($item['alias']); | |
50 | + | |
51 | + | |
52 | + // Создание объекта ReportRequest. | |
53 | + $request = new Google_Service_AnalyticsReporting_ReportRequest(); | |
54 | + $request->setViewId(self::VIEW_ID); | |
55 | + $request->setDateRanges($dateRange); | |
56 | + $request->setMetrics(array($sessions)); | |
57 | + | |
58 | + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); | |
59 | + $body->setReportRequests( array( $request) ); | |
60 | + | |
61 | + $response = $analytics->reports->batchGet( $body ); | |
62 | + } | |
63 | + | |
64 | + | |
65 | + $this->createArrayResponse($response, 'data'); | |
19 | 66 | |
20 | 67 | } |
21 | 68 | ... | ... |
app/library/App/Resources/GaResource.php
... | ... | @@ -28,7 +28,7 @@ class GaResource extends ApiResource { |
28 | 28 | ->deny(AclRoles::UNAUTHORIZED) |
29 | 29 | ->handler(GaController::class) |
30 | 30 | |
31 | - ->endpoint(ApiEndpoint::get('/ga', 'gaRequest') | |
31 | + ->endpoint(ApiEndpoint::get('/', 'getAction') | |
32 | 32 | ->allow(AclRoles::USER) |
33 | 33 | ->description('Returns data from Google Analytics Api') |
34 | 34 | ) | ... | ... |