1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\DashboardBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes; |
6
|
|
|
use Kunstmaan\DashboardBundle\Command\GoogleAnalyticsDataCollectCommand; |
7
|
|
|
use Kunstmaan\DashboardBundle\Entity\AnalyticsGoal; |
8
|
|
|
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview; |
9
|
|
|
use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment; |
10
|
|
|
use Kunstmaan\DashboardBundle\Repository\AnalyticsOverviewRepository; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
14
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class GoogleAnalyticsAJAXController |
21
|
|
|
*/ |
22
|
|
|
class GoogleAnalyticsAJAXController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @Route("/updateData", name="KunstmaanDashboardBundle_analytics_update") |
26
|
|
|
*/ |
27
|
|
|
public function runUpdateAction(Request $request) |
28
|
|
|
{ |
29
|
|
|
$configId = $request->query->get('configId'); |
30
|
|
|
$segmentId = $request->query->get('segmentId'); |
31
|
|
|
|
32
|
|
|
$command = new GoogleAnalyticsDataCollectCommand(); |
33
|
|
|
$command->setContainer($this->container); |
34
|
|
|
$input = new ArrayInput(['--config' => $configId, '--segment' => $segmentId]); |
35
|
|
|
$output = new NullOutput(); |
36
|
|
|
try { |
37
|
|
|
$command->run($input, $output); |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
$this->container->get('logger')->error($e->getMessage()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return new JsonResponse([], 200, ['Content-Type' => 'application/json']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return an ajax response with all data for an overview |
47
|
|
|
* |
48
|
|
|
* @Route("/getOverview/{id}", requirements={"id" = "\d+"}, name="KunstmaanDashboardBundle_analytics_overview_ajax") |
49
|
|
|
* |
50
|
|
|
* @param int $id |
51
|
|
|
* |
52
|
|
|
* @return Response|array |
53
|
|
|
*/ |
54
|
|
|
public function getOverviewAction(int $id) |
55
|
|
|
{ |
56
|
|
|
$em = $this->getDoctrine()->getManager(); |
57
|
|
|
/** @var AnalyticsOverviewRepository $analyticsOverviewRepository */ |
58
|
|
|
$analyticsOverviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview'); |
59
|
|
|
/** @var AnalyticsOverview $overview */ |
60
|
|
|
$overview = $analyticsOverviewRepository->find($id); |
61
|
|
|
|
62
|
|
|
// goals data |
63
|
|
|
$goals = []; |
64
|
|
|
foreach ($overview->getActiveGoals() as $key => $goal) { |
65
|
|
|
/** @var AnalyticsGoal $goal */ |
66
|
|
|
$goals[$key]['name'] = $goal->getName(); |
67
|
|
|
$goals[$key]['visits'] = $goal->getVisits(); |
68
|
|
|
$goals[$key]['id'] = $goal->getId(); |
69
|
|
|
$goals[$key]['chartData'] = json_decode($goal->getChartData()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// overview data |
73
|
|
|
$overviewData = [ |
74
|
|
|
'id' => $overview->getId(), |
75
|
|
|
'chartData' => json_decode($overview->getChartData(), true), |
76
|
|
|
'chartDataMaxValue' => $overview->getChartDataMaxValue(), |
77
|
|
|
'title' => $overview->getTitle(), |
78
|
|
|
'timespan' => $overview->getTimespan(), |
79
|
|
|
'startOffset' => $overview->getStartOffset(), |
80
|
|
|
'sessions' => number_format($overview->getSessions()), |
81
|
|
|
'users' => number_format($overview->getUsers()), |
82
|
|
|
'pagesPerSession' => round($overview->getPagesPerSession(), 2), |
83
|
|
|
'avgSessionDuration' => $overview->getAvgSessionDuration(), |
84
|
|
|
'returningUsers' => number_format($overview->getReturningUsers()), |
85
|
|
|
'newUsers' => round($overview->getNewUsers(), 2), |
86
|
|
|
'pageViews' => number_format($overview->getPageViews()), |
87
|
|
|
'returningUsersPercentage' => $overview->getReturningUsersPercentage(), |
88
|
|
|
'newUsersPercentage' => $overview->getNewUsersPercentage(), |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
// put all data in the return array |
92
|
|
|
$return = [ |
93
|
|
|
'responseCode' => 200, |
94
|
|
|
'overview' => $overviewData, |
95
|
|
|
'goals' => $goals, |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
// return json response |
99
|
|
|
return new JsonResponse($return, 200, ['Content-Type' => 'application/json']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/* =============================== ACCOUNT =============================== */ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @Route("/accounts/", name="kunstmaan_dashboard_ajax_accounts") |
106
|
|
|
*/ |
107
|
|
|
public function getAccountsAction(Request $request) |
108
|
|
|
{ |
109
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
110
|
|
|
$configId = $request->query->get('configId'); |
111
|
|
|
if ($configId) { |
112
|
|
|
$configHelper->init($configId); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$accounts = $configHelper->getAccounts(); |
116
|
|
|
|
117
|
|
|
return new JsonResponse($accounts, 200, ['Content-Type' => 'application/json']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @Route("/account/save", name="kunstmaan_dashboard_ajax_account_save") |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function saveAccountAction(Request $request) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$accountId = $request->query->get('id'); |
126
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
127
|
|
|
$configHelper->saveAccountId($accountId); |
128
|
|
|
|
129
|
|
|
return new JsonResponse(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/* =============================== PROPERTY =============================== */ |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @Route("/properties/", name="kunstmaan_dashboard_ajax_properties") |
136
|
|
|
*/ |
137
|
|
|
public function getPropertiesAction(Request $request) |
138
|
|
|
{ |
139
|
|
|
$accountId = $request->query->get('accountId'); |
140
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
141
|
|
|
$configId = $request->query->get('configId'); |
142
|
|
|
if ($configId) { |
143
|
|
|
$configHelper->init($configId); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$properties = $configHelper->getProperties($accountId); |
147
|
|
|
|
148
|
|
|
return new JsonResponse($properties, 200, ['Content-Type' => 'application/json']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @Route("/property/save", name="kunstmaan_dashboard_ajax_property_save") |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function savePropertyAction(Request $request) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$propertyId = $request->query->get('id'); |
157
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
158
|
|
|
$configHelper->savePropertyId($propertyId); |
159
|
|
|
|
160
|
|
|
return new JsonResponse(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/* =============================== PROFILE =============================== */ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @Route("/profiles/", name="kunstmaan_dashboard_ajax_profiles") |
167
|
|
|
*/ |
168
|
|
|
public function getProfilesAction(Request $request) |
169
|
|
|
{ |
170
|
|
|
$accountId = $request->query->get('accountId'); |
171
|
|
|
$propertyId = $request->query->get('propertyId'); |
172
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
173
|
|
|
$configId = $request->query->get('configId'); |
174
|
|
|
if ($configId) { |
175
|
|
|
$configHelper->init($configId); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$profiles = $configHelper->getProfiles($accountId, $propertyId); |
179
|
|
|
|
180
|
|
|
return new JsonResponse($profiles, 200, ['Content-Type' => 'application/json']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @Route("/profile/save", name="kunstmaan_dashboard_ajax_profile_save") |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function saveProfileAction(Request $request) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$propertyId = $request->query->get('id'); |
189
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
190
|
|
|
$configHelper->saveProfileId($propertyId); |
191
|
|
|
|
192
|
|
|
return new JsonResponse(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/* =============================== CONFIG =============================== */ |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @Route("/config/save", name="kunstmaan_dashboard_ajax_config_save") |
199
|
|
|
* |
200
|
|
|
* @param Request $request |
201
|
|
|
* |
202
|
|
|
* @return JsonResponse |
203
|
|
|
* @throws \Exception |
204
|
|
|
*/ |
205
|
|
|
public function saveConfigAction(Request $request) |
206
|
|
|
{ |
207
|
|
|
// get params |
208
|
|
|
$configId = $request->query->get('configId'); |
209
|
|
|
$accountId = $request->query->get('accountId'); |
210
|
|
|
$propertyId = $request->query->get('propertyId'); |
211
|
|
|
$profileId = $request->query->get('profileId'); |
212
|
|
|
$disableGoals = $request->query->get('disableGoals'); |
213
|
|
|
|
214
|
|
|
// edit the config |
215
|
|
|
$em = $this->getDoctrine()->getManager(); |
216
|
|
|
$config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId); |
217
|
|
|
if ($accountId && $propertyId && $profileId) { |
218
|
|
|
$config->setAccountId($accountId); |
219
|
|
|
$config->setPropertyId($propertyId); |
220
|
|
|
$config->setProfileId($profileId); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$em->persist($config); |
|
|
|
|
224
|
|
|
$em->flush(); |
225
|
|
|
|
226
|
|
|
// set the config name |
227
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
228
|
|
|
$configHelper->init($configId); |
229
|
|
|
$profile = $configHelper->getActiveProfile(); |
230
|
|
|
$config->setName($profile['profileName']); |
231
|
|
|
$config->setDisableGoals($disableGoals); |
232
|
|
|
|
233
|
|
|
$em->persist($config); |
|
|
|
|
234
|
|
|
$em->flush(); |
235
|
|
|
|
236
|
|
|
$this->addFlash( |
237
|
|
|
FlashTypes::SUCCESS, |
238
|
|
|
$this->get('translator')->trans('kuma_admin.ga_ajax_controller.flash.success') |
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
return new JsonResponse(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @Route("/config/remove", name="kunstmaan_dashboard_ajax_config_remove") |
246
|
|
|
*/ |
247
|
|
|
public function removeConfigAction(Request $request) |
248
|
|
|
{ |
249
|
|
|
// get params |
250
|
|
|
$configId = $request->query->get('configId'); |
251
|
|
|
|
252
|
|
|
// edit the config |
253
|
|
|
$em = $this->getDoctrine()->getManager(); |
254
|
|
|
$config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId); |
255
|
|
|
$em->remove($config); |
|
|
|
|
256
|
|
|
$em->flush(); |
257
|
|
|
|
258
|
|
|
return new JsonResponse(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @Route("/config/get", name="kunstmaan_dashboard_ajax_config_get") |
263
|
|
|
*/ |
264
|
|
|
public function getConfigAction(Request $request) |
265
|
|
|
{ |
266
|
|
|
$em = $this->getDoctrine()->getManager(); |
267
|
|
|
$config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst(); |
268
|
|
|
$accountId = $config->getAccountId(); |
269
|
|
|
|
270
|
|
|
if (!$accountId) { |
271
|
|
|
return new JsonResponse(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config'); |
275
|
|
|
$configHelper->getAccounts(); |
276
|
|
|
$configHelper->getProperties(); |
277
|
|
|
$configHelper->getProfiles(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/* =============================== SEGMENT =============================== */ |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @Route("/segment/add/", name="kunstmaan_dashboard_ajax_segment_add") |
284
|
|
|
*/ |
285
|
|
|
public function addSegmentAction(Request $request) |
286
|
|
|
{ |
287
|
|
|
$configId = $request->query->get('configId'); |
288
|
|
|
$em = $this->getDoctrine()->getManager(); |
289
|
|
|
|
290
|
|
|
// create a new segment |
291
|
|
|
$segment = new AnalyticsSegment(); |
292
|
|
|
$query = $request->query->get('query'); |
293
|
|
|
$name = $request->query->get('name'); |
294
|
|
|
$segment->setQuery($query); |
295
|
|
|
$segment->setName($name); |
296
|
|
|
|
297
|
|
|
// add the segment to the config |
298
|
|
|
$config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId); |
299
|
|
|
$segment->setConfig($config); |
300
|
|
|
$segments = $config->getSegments(); |
301
|
|
|
$segments[] = $segment; |
302
|
|
|
$config->setSegments($segments); |
303
|
|
|
|
304
|
|
|
$em->persist($config); |
|
|
|
|
305
|
|
|
$em->flush(); |
306
|
|
|
|
307
|
|
|
return new JsonResponse(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @Route("/segment/delete", name="kunstmaan_dashboard_ajax_segment_delete") |
312
|
|
|
*/ |
313
|
|
|
public function deleteSegmentAction(Request $request) |
314
|
|
|
{ |
315
|
|
|
$em = $this->getDoctrine()->getManager(); |
316
|
|
|
|
317
|
|
|
$id = $request->query->get('id'); |
318
|
|
|
$em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->deleteSegment($id); |
319
|
|
|
|
320
|
|
|
return new JsonResponse(); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @Route("/segment/edit", name="kunstmaan_dashboard_ajax_segment_edit") |
325
|
|
|
*/ |
326
|
|
|
public function editSegmentAction(Request $request) |
327
|
|
|
{ |
328
|
|
|
$em = $this->getDoctrine()->getManager(); |
329
|
|
|
|
330
|
|
|
$id = $request->query->get('id'); |
331
|
|
|
$query = $request->query->get('query'); |
332
|
|
|
$name = $request->query->get('name'); |
333
|
|
|
$segment = $em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->find($id); |
334
|
|
|
$segment->setName($name); |
335
|
|
|
$segment->setQuery($query); |
336
|
|
|
$em->persist($segment); |
|
|
|
|
337
|
|
|
$em->flush(); |
338
|
|
|
|
339
|
|
|
return new JsonResponse(); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.