Commit 843214bf87f59cb69dc02ffd77d506461b01daf9

Authored by Alex Savenko
1 parent 2e1db97a

test google lib

Showing 1 changed file with 44 additions and 1 deletions   Show diff stats
app/library/App/Bootstrap/RouteBootstrap.php
@@ -7,8 +7,13 @@ use App\Constants\Services; @@ -7,8 +7,13 @@ use App\Constants\Services;
7 use Phalcon\Config; 7 use Phalcon\Config;
8 use Phalcon\DiInterface; 8 use Phalcon\DiInterface;
9 use PhalconRest\Api; 9 use PhalconRest\Api;
  10 +
10 use Google_Client; 11 use Google_Client;
11 use Google_Service_AnalyticsReporting; 12 use Google_Service_AnalyticsReporting;
  13 +use Google_Service_AnalyticsReporting_DateRange;
  14 +use Google_Service_AnalyticsReporting_Metric;
  15 +use Google_Service_AnalyticsReporting_ReportRequest;
  16 +use Google_Service_AnalyticsReporting_GetReportsRequest;
12 17
13 class RouteBootstrap implements BootstrapInterface 18 class RouteBootstrap implements BootstrapInterface
14 { 19 {
@@ -52,7 +57,18 @@ class RouteBootstrap implements BootstrapInterface @@ -52,7 +57,18 @@ class RouteBootstrap implements BootstrapInterface
52 $client->setDeveloperKey($api_key); 57 $client->setDeveloperKey($api_key);
53 $analytics = new Google_Service_AnalyticsReporting($client); 58 $analytics = new Google_Service_AnalyticsReporting($client);
54 59
55 - $var_2 = var_dump($analytics); 60 + $param[] = ['metric' => 'ga:sessions', 'alias' => 'Сессии'];
  61 + $param[] = ['metric' => 'ga:users', 'alias' => 'Пользователи'];
  62 + $param[] = ['metric' => 'ga:CTR', 'alias' => 'CTR'];
  63 + $param[] = ['metric' => 'ga:goal1Value', 'alias' => 'цель "Корзина"'];
  64 +
  65 + foreach ($param as $item) {
  66 +
  67 + $response = $this->getReport($analytics, $item['metric'], $item['alias']);
  68 +
  69 + }
  70 +
  71 + $var_2 = var_dump($response);
56 72
57 $msg = "testing fine"; 73 $msg = "testing fine";
58 74
@@ -62,4 +78,31 @@ class RouteBootstrap implements BootstrapInterface @@ -62,4 +78,31 @@ class RouteBootstrap implements BootstrapInterface
62 ]); 78 ]);
63 }); 79 });
64 } 80 }
  81 +
  82 + function getReport($analytics, $metric, $alias) {
  83 +
  84 + // Замена на свой идентификатор представления, напр. XXXX.
  85 + $VIEW_ID = "119240817";
  86 +
  87 + // Создание объекта DateRange.
  88 + $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  89 + $dateRange->setStartDate("30daysAgo");
  90 + $dateRange->setEndDate("today");
  91 +
  92 + // Создание объекта Metrics.
  93 + $sessions = new Google_Service_AnalyticsReporting_Metric();
  94 + $sessions->setExpression($metric);
  95 + $sessions->setAlias($alias);
  96 +
  97 +
  98 + // Создание объекта ReportRequest.
  99 + $request = new Google_Service_AnalyticsReporting_ReportRequest();
  100 + $request->setViewId($VIEW_ID);
  101 + $request->setDateRanges($dateRange);
  102 + $request->setMetrics(array($sessions));
  103 +
  104 + $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  105 + $body->setReportRequests( array( $request) );
  106 + return $analytics->reports->batchGet( $body );
  107 + }
65 } 108 }
66 \ No newline at end of file 109 \ No newline at end of file