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 **/
|
0c1a07f0
Alex Savenko
add filter param ...
|
31
32
33
|
$user_id = $this->request->get('user_id')?? '1';
$view_id = $this->request->get('view_id');
$chart = $this->request->get('chart') ?? false;
|
c9087298
Alex Savenko
ga output
|
34
35
|
/** google params **/
|
0c1a07f0
Alex Savenko
add filter param ...
|
36
37
38
39
40
|
$get_metrics = $this->request->get('metric') ?? 'users';
$get_dimensions = $this->request->get('dimension');
$get_start_date = $this->request->get('start') ?? '30daysAgo';
$get_end_date = $this->request->get('end') ?? 'today';
$filter_expression = $this->request->get('filter');
|
c9087298
Alex Savenko
ga output
|
41
|
|
180e10be
Alex Savenko
dynamic dateRange
|
42
|
|
95c83ce2
Alex Savenko
ga output
|
43
|
if (empty($view_id)) {
|
2cda8b9d
Alex Savenko
ga output
|
44
|
$result = [];
|
de9cf95a
Alex Savenko
ga output
|
45
|
$projects = Project::find(['user_id' => $user_id]);
|
9ba864b0
Alex Savenko
ga output
|
46
|
foreach ($projects as $project) {
|
9791b548
Alex Savenko
ga output
|
47
|
$view_id = (string)$project->ga_view_id;
|
1d97c107
Alex Savenko
ga output
|
48
|
if (!empty($view_id)) {
|
0c1a07f0
Alex Savenko
add filter param ...
|
49
|
$result[] = $this->sendGaRequest($project->name, $view_id, $get_metrics, $get_dimensions, $get_start_date, $get_end_date, $chart, $filter_expression);
|
1d97c107
Alex Savenko
ga output
|
50
|
}
|
9ba864b0
Alex Savenko
ga output
|
51
52
53
|
}
}
else {
|
c0f7b2aa
Alex Savenko
ga output
|
54
|
$project = Project::findFirst(['ga_view_id' => $view_id]);
|
0c1a07f0
Alex Savenko
add filter param ...
|
55
|
$result = $this->sendGaRequest($project->name , $view_id, $get_metrics, $get_dimensions, $get_start_date, $get_end_date, $chart, $filter_expression);
|
9ba864b0
Alex Savenko
ga output
|
56
|
}
|
2a57dd72
Alex Savenko
ga fix
|
57
|
return $result;
|
129bec7c
Alex Savenko
ga output
|
58
59
60
|
}
|
c6c9c77e
Alex Savenko
add php docs to g...
|
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/**
* Send request to Google Analytics Reporting API
*
* @param string $project_name
* @param string $view
* @param string $get_metrics
* @param string $get_dimensions
* @param string $start
* @param string $end
* @param bool $chart
* @param string $filter_expression
* @return array
*/
|
0c1a07f0
Alex Savenko
add filter param ...
|
74
|
public function sendGaRequest($project_name, $view, $get_metrics, $get_dimensions, $start, $end, $chart = false, $filter_expression = null) {
|
129bec7c
Alex Savenko
ga output
|
75
|
|
897d06c3
Alex Savenko
generate GaResource
|
76
77
78
|
putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
|
c9087298
Alex Savenko
ga output
|
79
|
$client->setScopes([self::SCOPE]);
|
897d06c3
Alex Savenko
generate GaResource
|
80
81
|
$analytics = new Google_Service_AnalyticsReporting($client);
|
c9087298
Alex Savenko
ga output
|
82
|
/** Create the DateRange object. **/
|
29f4a05f
Alex Savenko
multiple metrics
|
83
|
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
|
129bec7c
Alex Savenko
ga output
|
84
85
|
$dateRange->setStartDate($start);
$dateRange->setEndDate($end);
|
29f4a05f
Alex Savenko
multiple metrics
|
86
|
|
c9087298
Alex Savenko
ga output
|
87
|
/** Create the Metrics object. **/
|
29f4a05f
Alex Savenko
multiple metrics
|
88
|
$metrics = [];
|
180e10be
Alex Savenko
dynamic dateRange
|
89
|
$get_metrics = explode(',', $get_metrics);
|
29f4a05f
Alex Savenko
multiple metrics
|
90
91
92
|
foreach ($get_metrics as $metric) {
$metrics_obj = new Google_Service_AnalyticsReporting_Metric();
$metrics_obj->setExpression('ga:'.$metric);
|
6d167872
Alex Savenko
alias to one temp...
|
93
|
$metrics_obj->setAlias('ga:'.$metric);
|
29f4a05f
Alex Savenko
multiple metrics
|
94
95
|
$metrics[] = $metrics_obj;
}
|
897d06c3
Alex Savenko
generate GaResource
|
96
|
|
c9087298
Alex Savenko
ga output
|
97
|
/** Create the Dimensions object. **/
|
584f7b1c
Alex Savenko
ga output
|
98
|
if (!empty($get_dimensions)) {
|
b22a7c02
Alex Savenko
empty dimension
|
99
100
101
102
103
104
105
|
$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...
|
106
|
}
|
f51dd710
Alex Savenko
dimensions +dynam...
|
107
|
|
c6c9c77e
Alex Savenko
add php docs to g...
|
108
|
/** Create the ReportRequest object. **/
|
29f4a05f
Alex Savenko
multiple metrics
|
109
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
129bec7c
Alex Savenko
ga output
|
110
|
$request->setViewId($view);
|
29f4a05f
Alex Savenko
multiple metrics
|
111
|
$request->setDateRanges($dateRange);
|
11d0a620
Alex Savenko
ga include empty ...
|
112
|
$request->setIncludeEmptyRows(true);
|
c6d3dfe5
Alex Savenko
registration
|
113
114
115
|
if (!empty($dimensions)) {
$request->setDimensions(array($dimensions));
}
|
c5aa2e2b
Alex Savenko
dimensions +dynam...
|
116
|
$request->setMetrics(array($metrics));
|
0c1a07f0
Alex Savenko
add filter param ...
|
117
118
119
|
if (!empty($filter_expression)) {
$request->setFiltersExpression("ga:".$filter_expression);
}
|
897d06c3
Alex Savenko
generate GaResource
|
120
|
|
29f4a05f
Alex Savenko
multiple metrics
|
121
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
c6d3dfe5
Alex Savenko
registration
|
122
|
$body->setReportRequests(array($request));
|
897d06c3
Alex Savenko
generate GaResource
|
123
|
|
c6d3dfe5
Alex Savenko
registration
|
124
|
$response = $analytics->reports->batchGet($body);
|
897d06c3
Alex Savenko
generate GaResource
|
125
|
|
c6c9c77e
Alex Savenko
add php docs to g...
|
126
|
/** can be refactored (code below 2 rows) **/
|
a6a1fb9f
Alex Savenko
ga output
|
127
|
$response = $response->toSimpleObject();
|
a6a1fb9f
Alex Savenko
ga output
|
128
|
$response = $response->reports[0]['data']['rows'];
|
9b8fbac2
Alex Savenko
ga output
|
129
|
|
7e1d4fa3
Alex Savenko
ga output
|
130
131
132
133
134
|
if ($chart) {
$result = self::responseChartTransform($response, $project_name);
} else {
$result = self::responseDataTransform($response, $project_name);
}
|
5937dcc7
Alex Savenko
ga output
|
135
|
|
5285e167
Alex Savenko
ga output
|
136
|
return $result;
|
5937dcc7
Alex Savenko
ga output
|
137
138
139
|
}
|
c6c9c77e
Alex Savenko
add php docs to g...
|
140
141
142
143
144
145
146
|
/**
* Data-transformer for tables. Used by default.
*
* @param array $response
* @param string $project_name
* @return array
*/
|
7e1d4fa3
Alex Savenko
ga output
|
147
|
public static function responseDataTransform(array $response, $project_name) {
|
5937dcc7
Alex Savenko
ga output
|
148
|
|
9cbda58b
Alex Savenko
ga output
|
149
|
$result = [];
|
9cbda58b
Alex Savenko
ga output
|
150
|
|
c04bfb94
Alex Savenko
ga output
|
151
|
foreach ($response as $item) {
|
d29dde0e
Alex Savenko
ga output
|
152
153
154
155
|
$metric_val = $item['metrics'][0]['values'];
$dimension_val = $item['dimensions'][0];
|
889ea43e
Alex Savenko
ga output
|
156
157
|
$result['name'] = $project_name;
|
f842989c
Alex Savenko
ga output
|
158
|
if (count($metric_val) > 1) {
|
65b1cb95
Alex Savenko
ga output
|
159
160
|
for ($i = 0; $i < count($metric_val); $i++) {
$result[$dimension_val][] = $metric_val[$i];
|
d29dde0e
Alex Savenko
ga output
|
161
162
163
164
|
}
} else {
$result[$dimension_val] = $metric_val[0];
}
|
9cbda58b
Alex Savenko
ga output
|
165
166
|
}
|
5937dcc7
Alex Savenko
ga output
|
167
|
return $result;
|
05915c96
Alex Savenko
ga output
|
168
|
|
7fe1d3b2
Alex Savenko
create response
|
169
|
}
|
5937dcc7
Alex Savenko
ga output
|
170
|
|
c6c9c77e
Alex Savenko
add php docs to g...
|
171
172
173
174
175
176
177
|
/**
* Data-transformer for charts, when query string contains "?chart=true"
*
* @param array $response
* @param string $project_name
* @return array
*/
|
7e1d4fa3
Alex Savenko
ga output
|
178
179
180
|
public static function responseChartTransform(array $response, $project_name) {
$result = [];
|
a7c416d0
Alex Savenko
ga output
|
181
|
|
7e1d4fa3
Alex Savenko
ga output
|
182
183
184
|
foreach ($response as $item) {
$metric_val = $item['metrics'][0]['values'];
|
92a5b7e0
Alex Savenko
ga fix
|
185
|
$result['name'] = $project_name;
|
7e1d4fa3
Alex Savenko
ga output
|
186
187
188
|
if (count($metric_val) > 1) {
for ($i = 0; $i < count($metric_val); $i++) {
|
cea9b628
Alex Savenko
ga fix
|
189
|
$result['data'][] = (int)$metric_val[$i];
|
7e1d4fa3
Alex Savenko
ga output
|
190
191
|
}
} else {
|
92a5b7e0
Alex Savenko
ga fix
|
192
|
$result['data'][] = (int)$metric_val[0];
|
7e1d4fa3
Alex Savenko
ga output
|
193
194
195
196
197
198
199
|
}
}
return $result;
}
|
5937dcc7
Alex Savenko
ga output
|
200
|
|
c6c9c77e
Alex Savenko
add php docs to g...
|
201
202
203
|
/**
* without usage, from google docs.
*/
|
5937dcc7
Alex Savenko
ga output
|
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
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
|
236
|
}
|