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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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\Date;
use Piwik\DataTable;
use Piwik\Metrics\Formatter;
use Piwik\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Site;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Plugins\Referrers\API as APIReferrers;
use Piwik\View;
class VisitorDetails extends VisitorDetailsAbstract
{
public function extendVisitorDetails(&$visitor)
{
$idSite = $this->getIdSite();
$website = new Site($idSite);
$timezone = $website->getTimezone();
$currency = $website->getCurrency();
$currencies = APISitesManager::getInstance()->getCurrencySymbols();
$visitor += array(
'idSite' => $idSite,
'idVisit' => $this->getIdVisit(),
'visitIp' => $this->getIp(),
'visitorId' => $this->getVisitorId(),
'fingerprint' => $this->getFingerprint(),
// => false are placeholders to be filled in API later
'actionDetails' => false,
'goalConversions' => false,
'siteCurrency' => false,
'siteCurrencySymbol' => false,
// all time entries
'serverDate' => $this->getServerDate(),
'visitServerHour' => $this->getVisitServerHour(),
'lastActionTimestamp' => $this->getTimestampLastAction(),
'lastActionDateTime' => $this->getDateTimeLastAction(),
);
$visitor['siteCurrency'] = $currency;
$visitor['siteCurrencySymbol'] = @$currencies[$visitor['siteCurrency']];
$visitor['siteName'] = $website->getName();
$visitor['serverTimestamp'] = $visitor['lastActionTimestamp'];
$visitor['firstActionTimestamp'] = strtotime($this->details['visit_first_action_time']);
$dateTimeVisit = Date::factory($visitor['lastActionTimestamp'], $timezone);
if ($dateTimeVisit) {
$visitor['serverTimePretty'] = $dateTimeVisit->getLocalized(Date::TIME_FORMAT);
$visitor['serverDatePretty'] = $dateTimeVisit->getLocalized(Date::DATE_FORMAT_LONG);
}
$dateTimeVisitFirstAction = Date::factory($visitor['firstActionTimestamp'], $timezone);
$visitor['serverDatePrettyFirstAction'] = $dateTimeVisitFirstAction->getLocalized(Date::DATE_FORMAT_LONG);
$visitor['serverTimePrettyFirstAction'] = $dateTimeVisitFirstAction->getLocalized(Date::TIME_FORMAT);
}
public function renderAction($action, $previousAction, $visitorDetails)
{
switch ($action['type']) {
case 'ecommerceOrder':
case 'ecommerceAbandonedCart':
$template = '@Live/_actionEcommerce.twig';
break;
case 'goal':
if (empty($action['goalName'])) {
return; // goal deleted
}
$template = '@Live/_actionGoal.twig';
break;
case 'action':
case 'search':
case 'outlink':
case 'download':
$template = '@Live/_actionCommon.twig';
break;
}
if (empty($template)) {
return;
}
$sitesModel = new \Piwik\Plugins\SitesManager\Model();
$view = new View($template);
$view->mainUrl = trim(Site::getMainUrlFor($this->getIdSite()));
$view->additionalUrls = $sitesModel->getAliasSiteUrlsFromId($this->getIdSite());
$view->action = $action;
$view->previousAction = $previousAction;
$view->visitInfo = $visitorDetails;
return $view->render();
}
public function renderActionTooltip($action, $visitInfo)
{
$view = new View('@Live/_actionTooltip');
$view->action = $action;
$view->visitInfo = $visitInfo;
return [[ 0, $view->render() ]];
}
public function renderVisitorDetails($visitorDetails)
{
$view = new View('@Live/_visitorDetails.twig');
$view->visitInfo = $visitorDetails;
return [[ 0, $view->render() ]];
}
public function renderIcons($visitorDetails)
{
$view = new View('@Live/_visitorLogIcons.twig');
$view->visitor = $visitorDetails;
return $view->render();
}
function getVisitorId()
{
if (isset($this->details['idvisitor'])) {
return bin2hex($this->details['idvisitor']);
}
return false;
}
function getVisitServerHour()
{
return date('G', strtotime($this->details['visit_last_action_time']));
}
function getServerDate()
{
return date('Y-m-d', strtotime($this->details['visit_last_action_time']));
}
function getIp()
{
if (isset($this->details['location_ip'])) {
return IPUtils::binaryToStringIP($this->details['location_ip']);
}
return null;
}
function getIdVisit()
{
return $this->details['idvisit'];
}
function getIdSite()
{
return isset($this->details['idsite']) ? $this->details['idsite'] : Common::getRequestVar('idSite');
}
function getFingerprint()
{
if (isset($this->details['config_id'])) {
return bin2hex($this->details['config_id']);
}
return false;
}
function getTimestampLastAction()
{
return strtotime($this->details['visit_last_action_time']);
}
function getDateTimeLastAction()
{
return date('Y-m-d H:i:s', strtotime($this->details['visit_last_action_time']));
}
public function initProfile($visits, &$profile)
{
$profile['totalVisits'] = 0;
$profile['totalVisitDuration'] = 0;
}
public function handleProfileVisit($visit, &$profile)
{
++$profile['totalVisits'];
$profile['totalVisitDuration'] += $visit->getColumn('visitDuration');
}
public function finalizeProfile($visits, &$profile)
{
$formatter = new Formatter();
$profile['totalVisitDurationPretty'] = $formatter->getPrettyTimeFromSeconds($profile['totalVisitDuration'], true);
$rows = $visits->getRows();
$firstVisit = $profile['visit_first'];
if (count($rows) >= Config::getInstance()->General['live_visitor_profile_max_visits_to_aggregate']) {
$firstVisit = $this->fetchFirstVisit();
}
$profile['userId'] = $visits->getLastRow()->getColumn('userId');
$profile['firstVisit'] = $this->getVisitorProfileVisitSummary($firstVisit);
$profile['lastVisit'] = $this->getVisitorProfileVisitSummary($profile['visit_last']);
$profile['visitsAggregated'] = count($rows);
}
/**
* Fetch first visit from Live API
*
* @return DataTable\Row
*/
private function fetchFirstVisit()
{
$response = Request::processRequest('Live.getFirstVisitForVisitorId', [
'idSite' => $this->getIdSite(),
'visitorId' => $this->getVisitorId(),
]);
return $response->getFirstRow();
}
/**
* Returns a summary for an important visit. Used to describe the first & last visits of a visitor.
*
* @param DataTable\Row $visit
* @return array
*/
private function getVisitorProfileVisitSummary($visit)
{
$today = Date::today();
$serverDate = $visit->getColumn('firstActionTimestamp');
return array(
'date' => $serverDate,
'prettyDate' => Date::factory($serverDate)->getLocalized(Date::DATE_FORMAT_LONG),
'daysAgo' => (int)Date::secondsToDays($today->getTimestamp() - Date::factory($serverDate)->getTimestamp()),
'referrerType' => $visit->getColumn('referrerType'),
'referrerUrl' => $visit->getColumn('referrerUrl') ?: '',
'referralSummary' => self::getReferrerSummaryForVisit($visit),
);
}
/**
* Returns a summary for a visit's referral.
*
* @param DataTable\Row $visit
* @return bool|mixed|string
*/
public static function getReferrerSummaryForVisit($visit)
{
$referrerType = $visit->getColumn('referrerType');
if ($referrerType === false
|| $referrerType == 'direct'
) {
return Piwik::translate('Referrers_DirectEntry');
}
if ($referrerType == 'search') {
$referrerName = $visit->getColumn('referrerName');
$keyword = $visit->getColumn('referrerKeyword');
if ($keyword !== false
&& $keyword != APIReferrers::getKeywordNotDefinedString()
) {
$referrerName .= ' (' . $keyword . ')';
}
return $referrerName;
}
if ($referrerType == 'campaign') {
$summary = Piwik::translate('Referrers_ColumnCampaign') . ': ' . $visit->getColumn('referrerName');
$keyword = $visit->getColumn('referrerKeyword');
if (!empty($keyword)) {
$summary .= ' - ' . $keyword;
}
return $summary;
}
return $visit->getColumn('referrerName');
}
}
|