1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/craft/analytics/ |
4
|
|
|
* @copyright Copyright (c) 2018, Dukt |
5
|
|
|
* @license https://dukt.net/craft/analytics/docs/license |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\analytics\controllers; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\web\Controller; |
12
|
|
|
use dukt\analytics\errors\InvalidChartTypeException; |
13
|
|
|
use dukt\analytics\Plugin as Analytics; |
14
|
|
|
use yii\web\Response; |
15
|
|
|
|
16
|
|
|
class ReportsController extends Controller |
17
|
|
|
{ |
18
|
|
|
// Public Methods |
19
|
|
|
// ========================================================================= |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* E-commerce Report |
23
|
|
|
* |
24
|
|
|
* @return null |
25
|
|
|
* @throws \yii\base\InvalidConfigException |
26
|
|
|
*/ |
27
|
|
|
public function actionEcommerceWidget() |
28
|
|
|
{ |
29
|
|
|
if (Analytics::getInstance()->getAnalytics()->demoMode) { |
30
|
|
|
return $this->getEcommerceDemoResponse(); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$viewId = Craft::$app->getRequest()->getBodyParam('viewId'); |
34
|
|
|
$period = Craft::$app->getRequest()->getBodyParam('period'); |
35
|
|
|
|
36
|
|
|
$response = Analytics::$plugin->getReports()->getEcommerceReport($viewId, $period); |
37
|
|
|
|
38
|
|
|
return $this->asJson($response); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get element report. |
43
|
|
|
* |
44
|
|
|
* @return Response |
45
|
|
|
* @throws \yii\base\InvalidConfigException |
46
|
|
|
* @throws \yii\web\BadRequestHttpException |
47
|
|
|
*/ |
48
|
|
|
public function actionElement() |
49
|
|
|
{ |
50
|
|
|
$elementId = Craft::$app->getRequest()->getRequiredParam('elementId'); |
51
|
|
|
$siteId = (int)Craft::$app->getRequest()->getRequiredParam('siteId'); |
52
|
|
|
$metric = Craft::$app->getRequest()->getRequiredParam('metric'); |
53
|
|
|
|
54
|
|
|
$response = Analytics::$plugin->getReports()->getElementReport($elementId, $siteId, $metric); |
55
|
|
|
|
56
|
|
|
return $this->asJson([ |
57
|
|
|
'type' => 'area', |
58
|
|
|
'chart' => $response |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get realtime widget report. |
64
|
|
|
* |
65
|
|
|
* @return Response |
66
|
|
|
* @throws \yii\base\InvalidConfigException |
67
|
|
|
*/ |
68
|
|
|
public function actionRealtimeWidget() |
69
|
|
|
{ |
70
|
|
|
if (Analytics::getInstance()->getAnalytics()->demoMode) { |
71
|
|
|
return $this->getRealtimeDemoResponse(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
// Active users |
76
|
|
|
|
77
|
|
|
$activeUsers = 0; |
78
|
|
|
|
79
|
|
|
$viewId = Craft::$app->getRequest()->getBodyParam('viewId'); |
80
|
|
|
|
81
|
|
|
$request = [ |
82
|
|
|
'viewId' => $viewId, |
83
|
|
|
'metrics' => 'ga:activeVisitors', |
84
|
|
|
'optParams' => [] |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$response = Analytics::$plugin->getReports()->getRealtimeReport($request); |
88
|
|
|
|
89
|
|
|
if (!empty($response['totalsForAllResults']) && isset($response['totalsForAllResults']['ga:activeVisitors'])) { |
90
|
|
|
$activeUsers = $response['totalsForAllResults']['ga:activeVisitors']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
// Pageviews |
95
|
|
|
|
96
|
|
|
$pageviewsRequest = [ |
97
|
|
|
'viewId' => $viewId, |
98
|
|
|
'metrics' => 'rt:pageviews', |
99
|
|
|
'optParams' => ['dimensions' => 'rt:minutesAgo'] |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$pageviews = Analytics::$plugin->getReports()->getRealtimeReport($pageviewsRequest); |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
// Active pages |
106
|
|
|
|
107
|
|
|
$activePagesRequest = [ |
108
|
|
|
'viewId' => $viewId, |
109
|
|
|
'metrics' => 'rt:activeUsers', |
110
|
|
|
'optParams' => ['dimensions' => 'rt:pagePath', 'max-results' => 5] |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
$activePages = Analytics::$plugin->getReports()->getRealtimeReport($activePagesRequest); |
114
|
|
|
|
115
|
|
|
return $this->asJson([ |
116
|
|
|
'activeUsers' => $activeUsers, |
117
|
|
|
'pageviews' => $pageviews, |
118
|
|
|
'activePages' => $activePages, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get report widget report. |
124
|
|
|
* |
125
|
|
|
* @return Response |
126
|
|
|
* @throws InvalidChartTypeException |
127
|
|
|
* @throws \yii\base\InvalidConfigException |
128
|
|
|
*/ |
129
|
|
|
public function actionReportWidget() |
130
|
|
|
{ |
131
|
|
|
$viewId = Craft::$app->getRequest()->getBodyParam('viewId'); |
132
|
|
|
$chart = Craft::$app->getRequest()->getBodyParam('chart'); |
133
|
|
|
$period = Craft::$app->getRequest()->getBodyParam('period'); |
134
|
|
|
$options = Craft::$app->getRequest()->getBodyParam('options'); |
135
|
|
|
|
136
|
|
|
$request = [ |
137
|
|
|
'viewId' => $viewId, |
138
|
|
|
'chart' => $chart, |
139
|
|
|
'period' => $period, |
140
|
|
|
'options' => $options, |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$cacheId = ['getReport', $request]; |
144
|
|
|
|
145
|
|
|
$response = Analytics::$plugin->cache->get($cacheId); |
146
|
|
|
|
147
|
|
|
if (!$response) { |
148
|
|
|
switch ($chart) { |
149
|
|
|
case 'area': |
150
|
|
|
$response = Analytics::$plugin->getReports()->getAreaReport($request); |
151
|
|
|
break; |
152
|
|
|
case 'counter': |
153
|
|
|
$response = Analytics::$plugin->getReports()->getCounterReport($request); |
154
|
|
|
break; |
155
|
|
|
case 'pie': |
156
|
|
|
$response = Analytics::$plugin->getReports()->getPieReport($request); |
157
|
|
|
break; |
158
|
|
|
case 'table': |
159
|
|
|
$response = Analytics::$plugin->getReports()->getTableReport($request); |
160
|
|
|
break; |
161
|
|
|
case 'geo': |
162
|
|
|
$response = Analytics::$plugin->getReports()->getGeoReport($request); |
163
|
|
|
break; |
164
|
|
|
default: |
165
|
|
|
throw new InvalidChartTypeException('Chart type `'.$chart.'` not supported.'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($response) { |
|
|
|
|
169
|
|
|
Analytics::$plugin->cache->set($cacheId, $response); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->asJson($response); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Private Methods |
177
|
|
|
// ========================================================================= |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get realtime demo response. |
181
|
|
|
* |
182
|
|
|
* @return Response |
183
|
|
|
* @throws \yii\base\InvalidConfigException |
184
|
|
|
*/ |
185
|
|
|
private function getRealtimeDemoResponse(): Response |
186
|
|
|
{ |
187
|
|
|
if (Analytics::$plugin->getAnalytics()->demoMode === 'test') { |
188
|
|
|
return $this->getRealtimeDemoTestResponse(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$pageviews = [ |
192
|
|
|
'rows' => [] |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
for ($i = 0; $i <= 30; $i++) { |
196
|
|
|
$pageviews['rows'][] = [$i, random_int(0, 20)]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$activePages = [ |
200
|
|
|
'rows' => [ |
201
|
|
|
['/a-new-toga/', random_int(1, 20)], |
202
|
|
|
['/parka-with-stripes-on-back/', random_int(1, 20)], |
203
|
|
|
['/romper-for-a-red-eye/', random_int(1, 20)], |
204
|
|
|
['/the-fleece-awakens/', random_int(1, 20)], |
205
|
|
|
['/the-last-knee-high/', random_int(1, 20)], |
206
|
|
|
] |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
$activeUsers = 0; |
210
|
|
|
|
211
|
|
|
foreach ($activePages['rows'] as $row) { |
212
|
|
|
$activeUsers += $row[1]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->asJson([ |
216
|
|
|
'activeUsers' => $activeUsers, |
217
|
|
|
'pageviews' => $pageviews, |
218
|
|
|
'activePages' => $activePages, |
219
|
|
|
]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get realtime demo test response. |
224
|
|
|
* |
225
|
|
|
* @return Response |
226
|
|
|
*/ |
227
|
|
|
private function getRealtimeDemoTestResponse(): Response |
228
|
|
|
{ |
229
|
|
|
$pageviews = [ |
230
|
|
|
'rows' => [] |
231
|
|
|
]; |
232
|
|
|
|
233
|
|
|
for ($i = 0; $i <= 30; $i++) { |
234
|
|
|
$pageviews['rows'][] = [$i, random_int(0, 20)]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$activePages = [ |
238
|
|
|
'rows' => [ |
239
|
|
|
['/some-url/', random_int(1, 20)], |
240
|
|
|
['/some-super-long-url/with-kebab-case/', random_int(1, 20)], |
241
|
|
|
['/somesuperlongurlwithoutkebabcasebutstillsuperlong/', random_int(10000000, 20000000)], |
242
|
|
|
['/someothersuperlongurl/withoutkebabcasebutstillsuperlong/', random_int(1, 20)], |
243
|
|
|
['/one-last-url/', random_int(1, 20)], |
244
|
|
|
] |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
$activeUsers = 0; |
248
|
|
|
|
249
|
|
|
foreach ($activePages['rows'] as $row) { |
250
|
|
|
$activeUsers += $row[1]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this->asJson([ |
254
|
|
|
'activeUsers' => $activeUsers, |
255
|
|
|
'pageviews' => $pageviews, |
256
|
|
|
'activePages' => $activePages, |
257
|
|
|
]); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return Response |
262
|
|
|
*/ |
263
|
|
|
private function getEcommerceDemoResponse(): Response |
264
|
|
|
{ |
265
|
|
|
$date = new \DateTime(); |
266
|
|
|
$date = $date->modify('-12 months'); |
267
|
|
|
$rows = []; |
268
|
|
|
|
269
|
|
|
for ($i = 1; $i <= 12; $i++) { |
270
|
|
|
$rows[] = [ |
271
|
|
|
$date->format('Ym'), |
272
|
|
|
random_int(50000, 150000) |
273
|
|
|
]; |
274
|
|
|
|
275
|
|
|
$date->modify('+1 month'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$reportData = [ |
279
|
|
|
'chart' => [ |
280
|
|
|
'cols' => [ |
281
|
|
|
[ |
282
|
|
|
'id' => 'ga:yearMonth', |
283
|
|
|
'label' => 'Month of Year', |
284
|
|
|
'type' => 'date', |
285
|
|
|
], |
286
|
|
|
[ |
287
|
|
|
'id' => 'ga:transactionRevenue', |
288
|
|
|
'label' => 'Revenue', |
289
|
|
|
'type' => 'currency', |
290
|
|
|
], |
291
|
|
|
], |
292
|
|
|
'rows' => $rows, |
293
|
|
|
'totals' => [ |
294
|
|
|
[ |
295
|
|
|
'11385.0', |
296
|
|
|
'97.3076923076923', |
297
|
|
|
] |
298
|
|
|
], |
299
|
|
|
], |
300
|
|
|
'period' => 'year', |
301
|
|
|
'periodLabel' => 'This year', |
302
|
|
|
'view' => 'Craft Shop', |
303
|
|
|
]; |
304
|
|
|
|
305
|
|
|
$totalRevenue = 0; |
306
|
|
|
|
307
|
|
|
foreach($rows as $row) { |
308
|
|
|
$totalRevenue += $row[1]; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$totalTransactions = random_int(3400, 3800); |
312
|
|
|
$totalRevenuePerTransaction = $totalRevenue / $totalTransactions; |
313
|
|
|
$totalTransactionsPerSession = 8.291991495393338; |
314
|
|
|
|
315
|
|
|
return $this->asJson([ |
316
|
|
|
'period' => '365daysAgo - today', |
317
|
|
|
'reportData' => $reportData, |
318
|
|
|
'totalRevenue' => $totalRevenue, |
319
|
|
|
'totalRevenuePerTransaction' => $totalRevenuePerTransaction, |
320
|
|
|
'totalTransactions' => $totalTransactions, |
321
|
|
|
'totalTransactionsPerSession' => $totalTransactionsPerSession, |
322
|
|
|
]); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|