RouteBootstrap.php
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace App\Bootstrap;
use App\BootstrapInterface;
use App\Constants\Services;
use Phalcon\Config;
use Phalcon\DiInterface;
use PhalconRest\Api;
use Google_Client;
use Google_Service_AnalyticsReporting;
use Google_Service_AnalyticsReporting_DateRange;
use Google_Service_AnalyticsReporting_Metric;
use Google_Service_AnalyticsReporting_ReportRequest;
use Google_Service_AnalyticsReporting_GetReportsRequest;
class RouteBootstrap implements BootstrapInterface
{
    public function run(Api $api, DiInterface $di, Config $config)
    {
        $api->get('/', function() use ($api) {
            /** @var \Phalcon\Mvc\View\Simple $view */
            $view = $api->di->get(Services::VIEW);
            return $view->render('general/index');
        });
        $api->get('/proxy.html', function() use ($api, $config) {
            /** @var \Phalcon\Mvc\View\Simple $view */
            $view = $api->di->get(Services::VIEW);
            $view->setVar('client', $config->clientHostName);
            return $view->render('general/proxy');
        });
        $api->get('/documentation.html', function() use ($api, $config) {
            /** @var \Phalcon\Mvc\View\Simple $view */
            $view = $api->di->get(Services::VIEW);
            $view->setVar('title', $config->application->title);
            $view->setVar('description', $config->application->description);
            $view->setVar('documentationPath', $config->hostName . '/export/documentation.json');
            return $view->render('general/documentation');
        });
        $api->get('/test.html', function() use ($api) {
            /** @var \Phalcon\Mvc\View\Simple $view */
            $view = $api->di->get(Services::VIEW);
            putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/ca4a1bd8aa14.json');
            $client = new Google_Client();
            $client->useApplicationDefaultCredentials();
            $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
            $analytics = new Google_Service_AnalyticsReporting($client);
            $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии'];
            $param[] = ['metric' => 'ga:users', 'alias' => 'Пользователи'];
            //$param[] = ['metric' => 'ga:CTR', 'alias' => 'CTR'];
            //$param[] = ['metric' => 'ga:goal1Value', 'alias' => 'цель "Корзина"'];
            $VIEW_ID = "119240817";
            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($VIEW_ID);
                $request->setDateRanges($dateRange);
                $request->setMetrics(array($sessions));
                $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
                $body->setReportRequests( array( $request) );
                $response =  $analytics->reports->batchGet( $body );
            }
            $var_2 = var_dump($response);
            $msg = "testing fine";
            return $view->render('general/test', [
                'var1' => $msg,
                'var2' => $var_2
            ]);
        });
    }
}