b38ef228
Alex Savenko
generate GaResource
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
/**
* Created by PhpStorm.
* User: Alex Savenko
* Date: 09.02.2017
* Time: 18:12
*/
namespace App\Controllers;
|
a941da22
Alex Savenko
ga output
|
12
|
use App\Model\Project;
|
897d06c3
Alex Savenko
generate GaResource
|
13
14
15
|
use Google_Client;
use Google_Service_AnalyticsReporting;
use Google_Service_AnalyticsReporting_DateRange;
|
f51dd710
Alex Savenko
dimensions +dynam...
|
16
|
use Google_Service_AnalyticsReporting_Dimension;
|
897d06c3
Alex Savenko
generate GaResource
|
17
18
19
|
use Google_Service_AnalyticsReporting_GetReportsRequest;
use Google_Service_AnalyticsReporting_Metric;
use Google_Service_AnalyticsReporting_ReportRequest;
|
b38ef228
Alex Savenko
generate GaResource
|
20
|
use PhalconRest\Mvc\Controllers\CrudResourceController;
|
b38ef228
Alex Savenko
generate GaResource
|
21
22
23
|
class GaController extends CrudResourceController {
|
897d06c3
Alex Savenko
generate GaResource
|
24
25
|
const SECRET_JSON = 'ca4a1bd8aa14.json';
const VIEW_ID = '119240817';
|
c9087298
Alex Savenko
ga output
|
26
|
const SCOPE = 'https://www.googleapis.com/auth/analytics.readonly';
|
b38ef228
Alex Savenko
generate GaResource
|
27
|
|
897d06c3
Alex Savenko
generate GaResource
|
28
29
|
public function getAction() {
|
c9087298
Alex Savenko
ga output
|
30
|
/** user params **/
|
95c83ce2
Alex Savenko
ga output
|
31
|
$user_id = $this->request->get('user_id')?? '1';
|
95c83ce2
Alex Savenko
ga output
|
32
|
$view_id= $this->request->get('view_id');
|
c9087298
Alex Savenko
ga output
|
33
34
35
|
$chart = $this->request->get('chart') ?? false;
/** google params **/
|
180e10be
Alex Savenko
dynamic dateRange
|
36
|
$get_metrics = $this->request->get('metric') ?? 'users';
|
129bec7c
Alex Savenko
ga output
|
37
|
$get_dimensions = $this->request->get('dimension');
|
180e10be
Alex Savenko
dynamic dateRange
|
38
39
|
$get_start_date = $this->request->get('start') ?? '30daysAgo';
$get_end_date = $this->request->get('end') ?? 'today';
|
c9087298
Alex Savenko
ga output
|
40
|
|
180e10be
Alex Savenko
dynamic dateRange
|
41
|
|
95c83ce2
Alex Savenko
ga output
|
42
|
if (empty($view_id)) {
|
2cda8b9d
Alex Savenko
ga output
|
43
|
$result = [];
|
de9cf95a
Alex Savenko
ga output
|
44
|
$projects = Project::find(['user_id' => $user_id]);
|
9ba864b0
Alex Savenko
ga output
|
45
|
foreach ($projects as $project) {
|
9791b548
Alex Savenko
ga output
|
46
|
$view_id = (string)$project->ga_view_id;
|
1d97c107
Alex Savenko
ga output
|
47
48
49
|
if (!empty($view_id)) {
$result[] = $this->sendGaRequest($project->name, $view_id, $get_metrics, $get_dimensions, $get_start_date, $get_end_date, $chart);
}
|
9ba864b0
Alex Savenko
ga output
|
50
51
52
|
}
}
else {
|
c0f7b2aa
Alex Savenko
ga output
|
53
|
$project = Project::findFirst(['ga_view_id' => $view_id]);
|
7e1d4fa3
Alex Savenko
ga output
|
54
|
$result = $this->sendGaRequest($project->name , $view_id, $get_metrics, $get_dimensions, $get_start_date, $get_end_date, $chart);
|
9ba864b0
Alex Savenko
ga output
|
55
|
}
|
2a57dd72
Alex Savenko
ga fix
|
56
|
return $result;
|
129bec7c
Alex Savenko
ga output
|
57
58
59
|
}
|
7e1d4fa3
Alex Savenko
ga output
|
60
|
public function sendGaRequest($project_name, $view, $get_metrics, $get_dimensions, $start, $end, $chart = false) {
|
129bec7c
Alex Savenko
ga output
|
61
|
|
897d06c3
Alex Savenko
generate GaResource
|
62
63
64
|
putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
|
c9087298
Alex Savenko
ga output
|
65
|
$client->setScopes([self::SCOPE]);
|
897d06c3
Alex Savenko
generate GaResource
|
66
67
|
$analytics = new Google_Service_AnalyticsReporting($client);
|
c9087298
Alex Savenko
ga output
|
68
|
/** Create the DateRange object. **/
|
29f4a05f
Alex Savenko
multiple metrics
|
69
|
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
|
129bec7c
Alex Savenko
ga output
|
70
71
|
$dateRange->setStartDate($start);
$dateRange->setEndDate($end);
|
29f4a05f
Alex Savenko
multiple metrics
|
72
|
|
c9087298
Alex Savenko
ga output
|
73
|
/** Create the Metrics object. **/
|
29f4a05f
Alex Savenko
multiple metrics
|
74
|
$metrics = [];
|
180e10be
Alex Savenko
dynamic dateRange
|
75
|
$get_metrics = explode(',', $get_metrics);
|
29f4a05f
Alex Savenko
multiple metrics
|
76
77
78
|
foreach ($get_metrics as $metric) {
$metrics_obj = new Google_Service_AnalyticsReporting_Metric();
$metrics_obj->setExpression('ga:'.$metric);
|
6d167872
Alex Savenko
alias to one temp...
|
79
|
$metrics_obj->setAlias('ga:'.$metric);
|
29f4a05f
Alex Savenko
multiple metrics
|
80
81
|
$metrics[] = $metrics_obj;
}
|
897d06c3
Alex Savenko
generate GaResource
|
82
|
|
c9087298
Alex Savenko
ga output
|
83
|
/** Create the Dimensions object. **/
|
584f7b1c
Alex Savenko
ga output
|
84
|
if (!empty($get_dimensions)) {
|
b22a7c02
Alex Savenko
empty dimension
|
85
86
87
88
89
90
91
|
$dimensions = [];
$get_dimensions = explode(',', $get_dimensions);
foreach ($get_dimensions as $dimension) {
$dimension_obj = new Google_Service_AnalyticsReporting_Dimension();
$dimension_obj->setName("ga:".$dimension);
$dimensions[] = $dimension_obj;
}
|
04026e8b
Alex Savenko
dimensions +dynam...
|
92
|
}
|
f51dd710
Alex Savenko
dimensions +dynam...
|
93
|
|
c9087298
Alex Savenko
ga output
|
94
|
//Create the ReportRequest object.
|
29f4a05f
Alex Savenko
multiple metrics
|
95
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
129bec7c
Alex Savenko
ga output
|
96
|
$request->setViewId($view);
|
29f4a05f
Alex Savenko
multiple metrics
|
97
|
$request->setDateRanges($dateRange);
|
11d0a620
Alex Savenko
ga include empty ...
|
98
|
$request->setIncludeEmptyRows(true);
|
c6d3dfe5
Alex Savenko
registration
|
99
100
101
|
if (!empty($dimensions)) {
$request->setDimensions(array($dimensions));
}
|
c5aa2e2b
Alex Savenko
dimensions +dynam...
|
102
|
$request->setMetrics(array($metrics));
|
897d06c3
Alex Savenko
generate GaResource
|
103
|
|
29f4a05f
Alex Savenko
multiple metrics
|
104
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
c6d3dfe5
Alex Savenko
registration
|
105
|
$body->setReportRequests(array($request));
|
897d06c3
Alex Savenko
generate GaResource
|
106
|
|
c6d3dfe5
Alex Savenko
registration
|
107
|
$response = $analytics->reports->batchGet($body);
|
897d06c3
Alex Savenko
generate GaResource
|
108
|
|
a6a1fb9f
Alex Savenko
ga output
|
109
110
|
//have to rewrite code below (2 rows)
$response = $response->toSimpleObject();
|
a6a1fb9f
Alex Savenko
ga output
|
111
|
$response = $response->reports[0]['data']['rows'];
|
9b8fbac2
Alex Savenko
ga output
|
112
|
|
7e1d4fa3
Alex Savenko
ga output
|
113
114
115
116
117
|
if ($chart) {
$result = self::responseChartTransform($response, $project_name);
} else {
$result = self::responseDataTransform($response, $project_name);
}
|
5937dcc7
Alex Savenko
ga output
|
118
|
|
5285e167
Alex Savenko
ga output
|
119
|
return $result;
|
5937dcc7
Alex Savenko
ga output
|
120
121
122
|
}
|
7e1d4fa3
Alex Savenko
ga output
|
123
|
public static function responseDataTransform(array $response, $project_name) {
|
5937dcc7
Alex Savenko
ga output
|
124
|
|
9cbda58b
Alex Savenko
ga output
|
125
|
$result = [];
|
9cbda58b
Alex Savenko
ga output
|
126
|
|
c04bfb94
Alex Savenko
ga output
|
127
|
foreach ($response as $item) {
|
d29dde0e
Alex Savenko
ga output
|
128
129
130
131
|
$metric_val = $item['metrics'][0]['values'];
$dimension_val = $item['dimensions'][0];
|
889ea43e
Alex Savenko
ga output
|
132
133
|
$result['name'] = $project_name;
|
f842989c
Alex Savenko
ga output
|
134
|
if (count($metric_val) > 1) {
|
65b1cb95
Alex Savenko
ga output
|
135
136
|
for ($i = 0; $i < count($metric_val); $i++) {
$result[$dimension_val][] = $metric_val[$i];
|
d29dde0e
Alex Savenko
ga output
|
137
138
139
140
|
}
} else {
$result[$dimension_val] = $metric_val[0];
}
|
9cbda58b
Alex Savenko
ga output
|
141
142
|
}
|
5937dcc7
Alex Savenko
ga output
|
143
|
return $result;
|
05915c96
Alex Savenko
ga output
|
144
|
|
7fe1d3b2
Alex Savenko
create response
|
145
|
}
|
5937dcc7
Alex Savenko
ga output
|
146
|
|
7e1d4fa3
Alex Savenko
ga output
|
147
148
149
|
public static function responseChartTransform(array $response, $project_name) {
$result = [];
|
a7c416d0
Alex Savenko
ga output
|
150
|
|
7e1d4fa3
Alex Savenko
ga output
|
151
152
153
|
foreach ($response as $item) {
$metric_val = $item['metrics'][0]['values'];
|
92a5b7e0
Alex Savenko
ga fix
|
154
|
$result['name'] = $project_name;
|
7e1d4fa3
Alex Savenko
ga output
|
155
156
157
|
if (count($metric_val) > 1) {
for ($i = 0; $i < count($metric_val); $i++) {
|
cea9b628
Alex Savenko
ga fix
|
158
|
$result['data'][] = (int)$metric_val[$i];
|
7e1d4fa3
Alex Savenko
ga output
|
159
160
|
}
} else {
|
92a5b7e0
Alex Savenko
ga fix
|
161
|
$result['data'][] = (int)$metric_val[0];
|
7e1d4fa3
Alex Savenko
ga output
|
162
163
164
165
166
167
168
|
}
}
return $result;
}
|
5937dcc7
Alex Savenko
ga output
|
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
public function printResults($reports) {
$res = '';
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}
for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
$entry = $metricHeaders[$j];
$values = $metrics[$j];
//print("Metric type: " . $entry->getType() . "\n" );
for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
$value = $values->getValues()[ $valueIndex ];
$res .= "<b>" . $entry->getName() . "</b>: " . $value . '<br/>';
}
}
}
}
return $res;
}
|
b38ef228
Alex Savenko
generate GaResource
|
207
|
}
|