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