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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik_Plugins
* @package Piwik_Live
*/
/**
* @see plugins/Referers/functions.php
*/
require_once PIWIK_INCLUDE_PATH . '/plugins/Live/Visitor.php';
/**
* @package Piwik_Live
*/
class Piwik_Live_API
{
static private $instance = null;
/**
* @return Piwik_Live_API
*/
static public function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self;
}
return self::$instance;
}
const TYPE_FETCH_VISITS = 1;
const TYPE_FETCH_PAGEVIEWS = 2;
/*
* @return Piwik_DataTable
*/
public function getLastVisitForVisitor( $visitorId, $idSite )
{
Piwik::checkUserHasViewAccess($idSite);
$limit = 1;
$visitorDetails = $this->loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $limit, $offset = false, $minIdVisit = false, $visitorId);
$table = $this->getCleanedVisitorsFromDetails($visitorDetails, $idSite);
return $table;
}
/*
* @return Piwik_DataTable
*/
public function getLastVisitsForVisitor( $visitorId, $idSite, $limit = 10 )
{
Piwik::checkUserHasViewAccess($idSite);
$visitorDetails = $this->loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $limit, $offset = 0, $minIdVisit = false, $visitorId);
$table = $this->getCleanedVisitorsFromDetails($visitorDetails, $idSite);
return $table;
}
/*
* @return Piwik_DataTable
*/
public function getLastVisits( $idSite, $limit = 10, $minIdVisit = false )
{
Piwik::checkUserHasViewAccess($idSite);
$visitorDetails = $this->loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $limit, $offset = 0, $minIdVisit, $visitorId = false);
$table = $this->getCleanedVisitorsFromDetails($visitorDetails, $idSite);
return $table;
}
/*
* @return Piwik_DataTable
*/
public function getLastVisitsDetails( $idSite, $period = false, $date = false, $limit = false, $offset = false, $minIdVisit = false )
{
if(empty($limit))
{
$limit = Piwik_Common::getRequestVar('filter_limit', 100, 'int');
}
if(empty($offset))
{
$offset = Piwik_Common::getRequestVar('filter_offset', 0, 'int');
}
Piwik::checkUserHasViewAccess($idSite);
$visitorDetails = $this->loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $limit, $offset, $minIdVisit);
$dataTable = $this->getCleanedVisitorsFromDetails($visitorDetails, $idSite);
return $dataTable;
}
/*
* @return Piwik_DataTable
*/
public function getUsersInLastXMin( $idSite, $minutes = 30 )
{
Piwik::checkUserHasViewAccess($idSite);
$visitorData = $this->loadLastVisitorInLastXTimeFromDatabase($idSite, $minutes, $days = 0, self::TYPE_FETCH_VISITS);
return $visitorData;
}
/*
* @return Piwik_DataTable
*/
public function getUsersInLastXDays( $idSite, $days = 10 )
{
Piwik::checkUserHasViewAccess($idSite);
$visitorData = $this->loadLastVisitorInLastXTimeFromDatabase($idSite, $minutes = 0, $days, self::TYPE_FETCH_VISITS);
return $visitorData;
}
/*
* @return array
*/
public function getPageImpressionsInLastXDays($idSite, $days = 10)
{
Piwik::checkUserHasViewAccess($idSite);
$visitorData = $this->loadLastVisitorInLastXTimeFromDatabase($idSite, $minutes = 0, $days, self::TYPE_FETCH_PAGEVIEWS);
return $visitorData;
}
/*
* @return array
*/
public function getPageImpressionsInLastXMin($idSite, $minutes = 30)
{
Piwik::checkUserHasViewAccess($idSite);
$visitorData = $this->loadLastVisitorInLastXTimeFromDatabase($idSite, $minutes, $days = 0, self::TYPE_FETCH_PAGEVIEWS);
return $visitorData;
}
/*
* @return Piwik_DataTable
*/
private function getCleanedVisitorsFromDetails($visitorDetails, $idSite)
{
$table = new Piwik_DataTable();
$site = new Piwik_Site($idSite);
$timezone = $site->getTimezone();
foreach($visitorDetails as $visitorDetail)
{
$this->cleanVisitorDetails($visitorDetail, $idSite);
$visitor = new Piwik_Live_Visitor($visitorDetail);
$visitorDetailsArray = $visitor->getAllVisitorDetails();
$visitorDetailsArray['siteCurrency'] = $site->getCurrency();
$dateTimeVisit = Piwik_Date::factory($visitorDetailsArray['lastActionTimestamp'], $timezone);
$visitorDetailsArray['serverDatePretty'] = $dateTimeVisit->getLocalized('%shortDay% %day% %shortMonth%');
$visitorDetailsArray['serverTimePretty'] = $dateTimeVisit->getLocalized('%time%');
$visitorDetailsArray['goalConversions'] = $visitorDetail['count_goal_conversions'];
if(!empty($visitorDetailsArray['goalTimePretty']))
{
$dateTimeConversion = Piwik_Date::factory($visitorDetailsArray['goalTimePretty'], $timezone);
$visitorDetailsArray['goalTimePretty'] = $dateTimeConversion->getLocalized('%shortDay% %day% %shortMonth% %time%');
}
$idvisit = $visitorDetailsArray['idVisit'];
$sql = "
SELECT
" .Piwik_Common::prefixTable('log_action').".name AS pageUrl,
" .Piwik_Common::prefixTable('log_action').".idaction AS pageIdAction
FROM " .Piwik_Common::prefixTable('log_link_visit_action')."
INNER JOIN " .Piwik_Common::prefixTable('log_action')."
ON " .Piwik_Common::prefixTable('log_link_visit_action').".idaction_url = " .Piwik_Common::prefixTable('log_action').".idaction
WHERE " .Piwik_Common::prefixTable('log_link_visit_action').".idvisit = $idvisit;
";
$visitorDetailsArray['actionDetails'] = Piwik_FetchAll($sql);
$sql = "
SELECT
" .Piwik_Common::prefixTable('log_action').".name AS pageTitle,
" .Piwik_Common::prefixTable('log_action').".idaction AS pageIdAction
FROM " .Piwik_Common::prefixTable('log_link_visit_action')."
INNER JOIN " .Piwik_Common::prefixTable('log_action')."
ON " .Piwik_Common::prefixTable('log_link_visit_action').".idaction_name = " .Piwik_Common::prefixTable('log_action').".idaction
WHERE " .Piwik_Common::prefixTable('log_link_visit_action').".idvisit = $idvisit;
";
$visitorDetailsArray['actionDetailsTitle'] = Piwik_FetchAll($sql);
$table->addRowFromArray( array(Piwik_DataTable_Row::COLUMNS => $visitorDetailsArray));
}
return $table;
}
/*
* @return array
*/
private function loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $limit = false, $offset = false, $minIdVisit = false, $visitorId = false)
{
// var_dump($period); var_dump($date); var_dump($limit); var_dump($offset); var_dump($minIdVisit); var_dump($visitorId);
$where = $whereBind = array();
$where[] = Piwik_Common::prefixTable('log_visit') . ".idsite = ? ";
$whereBind[] = $idSite;
if(!empty($visitorId))
{
$where[] = Piwik_Common::prefixTable('log_visit') . ".visitor_idcookie = ? ";
$whereBind[] = $visitorId;
}
if(!empty($minIdVisit))
{
$where[] = Piwik_Common::prefixTable('log_visit') . ".idvisit > ? ";
$whereBind[] = $minIdVisit;
}
// SQL Filter with provided period
if (!empty($period) && !empty($date))
{
$currentSite = new Piwik_Site($idSite);
$currentTimezone = $currentSite->getTimezone();
$processedDate = Piwik_Date::factory($date)->setTimezone($currentTimezone);
$processedPeriod = Piwik_Period::factory($period, $processedDate);
array_push( $where, Piwik_Common::prefixTable('log_visit') . ".visit_first_action_time BETWEEN ? AND ?");
array_push( $whereBind,
$processedPeriod->getDateStart()->toString('Y-m-d H:i:s'),
$processedPeriod->getDateEnd()->addDay(1)->toString('Y-m-d H:i:s')
);
}
$sqlWhere = "";
if(count($where) > 0)
{
$sqlWhere = " WHERE " . join(' AND ', $where);
}
// Group by idvisit so that a visitor converting 2 goals only appears twice
$sql = "SELECT " . Piwik_Common::prefixTable('log_visit') . ".* ,
" . Piwik_Common::prefixTable ( 'goal' ) . ".match_attribute as goal_match_attribute,
" . Piwik_Common::prefixTable ( 'goal' ) . ".name as goal_name,
" . Piwik_Common::prefixTable ( 'goal' ) . ".revenue as goal_revenue,
" . Piwik_Common::prefixTable ( 'log_conversion' ) . ".idaction_url as goal_idaction_url,
" . Piwik_Common::prefixTable ( 'log_conversion' ) . ".server_time as goal_server_time,
count(*) as count_goal_conversions
FROM " . Piwik_Common::prefixTable('log_visit') . "
LEFT JOIN ".Piwik_Common::prefixTable('log_conversion')."
ON " . Piwik_Common::prefixTable('log_visit') . ".idvisit = " . Piwik_Common::prefixTable('log_conversion') . ".idvisit
LEFT JOIN ".Piwik_Common::prefixTable('goal')."
ON (" . Piwik_Common::prefixTable('goal') . ".idsite = " . Piwik_Common::prefixTable('log_visit') . ".idsite
AND " . Piwik_Common::prefixTable('goal') . ".idgoal = " . Piwik_Common::prefixTable('log_conversion') . ".idgoal)
AND " . Piwik_Common::prefixTable('goal') . ".deleted = 0
$sqlWhere
GROUP BY idvisit
ORDER BY visit_last_action_time DESC";
if(!empty($limit))
{
$offsetSql = '';
if(!empty($offset))
{
$offsetSql = (int)$offset . ", ";
}
$sql .= " LIMIT $offsetSql ".(int)$limit;
}
$data = Piwik_FetchAll($sql, $whereBind);
// var_dump($whereBind); echo($sql);var_dump($data);
return $data;
}
/**
* Load last Visitors PAGES or DETAILS in MINUTES or DAYS from database
*
* @param int $idSite
* @param int $minutes
* @param int $days
* @param int $type self::TYPE_FETCH_VISITS or self::TYPE_FETCH_PAGEVIEWS
*
* @return mixed
*/
private function loadLastVisitorInLastXTimeFromDatabase($idSite, $minutes = 0, $days = 0, $type = false )
{
$where = $whereBind = array();
$where[] = " " . Piwik_Common::prefixTable('log_visit') . ".idsite = ? ";
$whereBind[] = $idSite;
if($minutes != 0)
{
$timeLimit = mktime(date('H'), date('i') - $minutes, 0, date('m'), date('d'), date('Y'));
$where[] = " visit_last_action_time > ?";
$whereBind[] = date('Y-m-d H:i:s', $timeLimit);
}
if($days != 0)
{
$timeLimit = mktime(date('H'), date('i'), 0, date('m'), date('d') - $days, date('Y'));
$where[] = " visit_last_action_time > ?";
$whereBind[] = date('Y-m-d H:i:s', $timeLimit);
}
$sqlWhere = "";
if(count($where) > 0)
{
$sqlWhere = " WHERE " . join(' AND ', $where);
}
// Details
if($type == self::TYPE_FETCH_VISITS)
{
$sql = "SELECT " . Piwik_Common::prefixTable('log_visit') . ".idvisit
FROM " . Piwik_Common::prefixTable('log_visit') . "
$sqlWhere
ORDER BY idvisit DESC";
}
// Pages
elseif($type == self::TYPE_FETCH_PAGEVIEWS)
{
$sql = "SELECT " . Piwik_Common::prefixTable('log_link_visit_action') . ".idaction_url
FROM " . Piwik_Common::prefixTable('log_link_visit_action') . "
INNER JOIN " . Piwik_Common::prefixTable('log_visit') . "
ON " . Piwik_Common::prefixTable('log_visit') . ".idvisit = " . Piwik_Common::prefixTable('log_link_visit_action') . ".idvisit
$sqlWhere";
}
else
{
// no $type is set --> ERROR
throw new Exception("type parameter is not properly set.");
}
// return $sql by fetching
return Piwik_FetchAll($sql, $whereBind);
}
/**
* Removes fields that are not meant to be displayed (md5 config hash)
* Or that the user should only access if he is super user or admin (cookie, IP)
*
* @return void
*/
private function cleanVisitorDetails( &$visitorDetails, $idSite )
{
$toUnset = array('config_md5config');
if(Piwik::isUserIsAnonymous())
{
$toUnset[] = 'visitor_idcookie';
$toUnset[] = 'location_ip';
}
foreach($toUnset as $keyName)
{
if(isset($visitorDetails[$keyName]))
{
unset($visitorDetails[$keyName]);
}
}
}
}
|