1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
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
236
237
238
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Live;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\DataTable;
use Piwik\Plugins\Goals\API as APIGoals;
use Piwik\Plugins\Live\Visualizations\VisitorLog;
use Piwik\Url;
use Piwik\View;
/**
*/
class Controller extends \Piwik\Plugin\Controller
{
const SIMPLE_VISIT_COUNT_WIDGET_LAST_MINUTES_CONFIG_KEY = 'live_widget_visitor_count_last_minutes';
private $profileSummaryProvider;
public function __construct(ProfileSummaryProvider $profileSummaryProvider)
{
$this->profileSummaryProvider = $profileSummaryProvider;
parent::__construct();
}
function index()
{
return $this->widget();
}
public function widget()
{
Piwik::checkUserHasViewAccess($this->idSite);
$view = new View('@Live/index');
$view->idSite = $this->idSite;
$view->isWidgetized = Common::getRequestVar('widget', 0, 'int');
$view = $this->setCounters($view);
$view->liveRefreshAfterMs = (int)Config::getInstance()->General['live_widget_refresh_after_seconds'] * 1000;
$view->visitors = $this->getLastVisitsStart();
$view->liveTokenAuth = Piwik::getCurrentUserTokenAuth();
return $this->render($view);
}
public function ajaxTotalVisitors()
{
Piwik::checkUserHasViewAccess($this->idSite);
$view = new View('@Live/ajaxTotalVisitors');
$view = $this->setCounters($view);
$view->idSite = $this->idSite;
return $this->render($view);
}
private function render(View $view)
{
$rendered = $view->render();
return $rendered;
}
public function indexVisitorLog()
{
Piwik::checkUserHasViewAccess($this->idSite);
$view = new View('@Live/indexVisitorLog.twig');
$view->visitorLog = $this->renderReport('getLastVisitsDetails');
return $view->render();
}
/**
* Widget
*/
public function getVisitorLog()
{
return $this->renderReport('getLastVisitsDetails');
}
public function getLastVisitsStart()
{
Piwik::checkUserHasViewAccess($this->idSite);
// hack, ensure we load today's visits by default
$_GET['date'] = 'today';
\Piwik\Period\Factory::checkPeriodIsEnabled('day');
$_GET['period'] = 'day';
$view = new View('@Live/getLastVisitsStart');
$view->idSite = (int) $this->idSite;
$error = '';
$visitors = new DataTable();
try {
$api = new Request("method=Live.getLastVisitsDetails&idSite={$this->idSite}&filter_limit=10&format=original&serialize=0&disable_generic_filters=1");
$visitors = $api->process();
} catch (\Exception $e) {
$error = $e->getMessage();
}
$view->error = $error;
$view->visitors = $visitors;
return $this->render($view);
}
private function setCounters($view)
{
$segment = Request::getRawSegmentFromRequest();
$last30min = Request::processRequest('Live.getCounters', [
'idSite' => $this->idSite,
'lastMinutes' => 30,
'segment' => $segment,
'showColumns' => 'visits,actions',
], $default = []);
$last30min = $last30min[0];
$today = Request::processRequest('Live.getCounters', [
'idSite' => $this->idSite,
'lastMinutes' => 24 * 60,
'segment' => $segment,
'showColumns' => 'visits,actions',
], $default = []);
$today = $today[0];
$view->visitorsCountHalfHour = $last30min['visits'];
$view->visitorsCountToday = $today['visits'];
$view->pisHalfhour = $last30min['actions'];
$view->pisToday = $today['actions'];
return $view;
}
/**
* Echo's HTML for visitor profile popup.
*/
public function getVisitorProfilePopup()
{
Piwik::checkUserHasViewAccess($this->idSite);
$visitorData = Request::processRequest('Live.getVisitorProfile');
if (empty($visitorData)) {
throw new \Exception('Visitor could not be found'); // for example when URL parameter is not set
}
VisitorLog::groupActionsByPageviewId($visitorData['lastVisits']);
$view = new View('@Live/getVisitorProfilePopup.twig');
$view->idSite = $this->idSite;
$view->goals = Request::processRequest('Goals.getGoals', ['idSite' => $this->idSite, 'filter_limit' => '-1'], $default = []);
$view->visitorData = $visitorData;
$view->exportLink = $this->getVisitorProfileExportLink();
$this->setWidgetizedVisitorProfileUrl($view);
$summaryEntries = array();
$profileSummaries = $this->profileSummaryProvider->getAllInstances();
foreach ($profileSummaries as $profileSummary) {
$profileSummary->setProfile($view->visitorData);
$summaryEntries[] = [$profileSummary->getOrder(), $profileSummary->render()];
}
usort($summaryEntries, function($a, $b) {
return version_compare($a[0], $b[0]);
});
$summary = '';
foreach ($summaryEntries AS $summaryEntry) {
$summary .= $summaryEntry[1];
}
$view->profileSummary = $summary;
return $view->render();
}
public function getVisitList()
{
$this->checkSitePermission();
Piwik::checkUserHasViewAccess($this->idSite);
$filterLimit = Common::getRequestVar('filter_offset', 0, 'int');
$startCounter = Common::getRequestVar('start_number', 0, 'int');
$limit = Config::getInstance()->General['live_visitor_profile_max_visits_to_aggregate'];
if ($startCounter >= $limit) {
return ''; // do not return more visits than configured for profile
}
$nextVisits = Request::processRequest('Live.getLastVisitsDetails', array(
'segment' => Live::getSegmentWithVisitorId(),
'filter_limit' => VisitorProfile::VISITOR_PROFILE_MAX_VISITS_TO_SHOW,
'filter_offset' => $filterLimit,
'period' => false,
'date' => false
));
if (empty($nextVisits)) {
return '';
}
VisitorLog::groupActionsByPageviewId($nextVisits);
$view = new View('@Live/getVisitList.twig');
$view->idSite = $this->idSite;
$view->startCounter = $startCounter < $nextVisits->getRowsCount() ? $nextVisits->getRowsCount() : $startCounter;
$view->visits = $nextVisits;
return $view->render();
}
private function getVisitorProfileExportLink()
{
return Url::getCurrentQueryStringWithParametersModified(array(
'module' => 'API',
'action' => 'index',
'method' => 'Live.getVisitorProfile',
'format' => 'XML',
'expanded' => 1
));
}
private function setWidgetizedVisitorProfileUrl($view)
{
if (\Piwik\Plugin\Manager::getInstance()->isPluginLoaded('Widgetize')) {
$view->widgetizedLink = Url::getCurrentQueryStringWithParametersModified(array(
'module' => 'Widgetize',
'action' => 'iframe',
'moduleToWidgetize' => 'Live',
'actionToWidgetize' => 'getVisitorProfilePopup'
));
}
}
}
|