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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
<?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\Tracker;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Matomo\Network\IPUtils;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Plugins\UserCountry\Columns\Base;
use Piwik\Tracker;
use Piwik\Tracker\Visit\VisitProperties;
/**
* Class used to handle a Visit.
* A visit is either NEW or KNOWN.
* - If a visit is NEW then we process the visitor information (settings, referrers, etc.) and save
* a new line in the log_visit table.
* - If a visit is KNOWN then we update the visit row in the log_visit table, updating the number of pages
* views, time spent, etc.
*
* Whether a visit is NEW or KNOWN we also save the action in the DB.
* One request to the matomo.php script is associated to one action.
*
*/
class Visit implements VisitInterface
{
const UNKNOWN_CODE = 'xx';
/**
* @var GoalManager
*/
protected $goalManager;
/**
* @var Request
*/
protected $request;
/**
* @var Settings
*/
protected $userSettings;
public static $dimensions;
/**
* @var RequestProcessor[]
*/
protected $requestProcessors;
/**
* @var VisitProperties
*/
protected $visitProperties;
/**
* @var ArchiveInvalidator
*/
private $invalidator;
protected $fieldsThatRequireAuth = array(
'city',
'region',
'country',
'lat',
'long'
);
public function __construct()
{
$requestProcessors = StaticContainer::get('Piwik\Plugin\RequestProcessors');
$this->requestProcessors = $requestProcessors->getRequestProcessors();
$this->visitProperties = null;
$this->userSettings = StaticContainer::get('Piwik\Tracker\Settings');
$this->invalidator = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
}
/**
* @param Request $request
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
private function checkSiteExists(Request $request)
{
try {
$request->getIdSite();
} catch (UnexpectedWebsiteFoundException $e) {
// we allow 0... the request will fail anyway as the site won't exist... allowing 0 will help us
// reporting this tracking problem as it is a common issue. Otherwise we would not be able to report
// this problem in tracking failures
StaticContainer::get(Failures::class)->logFailure(Failures::FAILURE_ID_INVALID_SITE, $request);
throw $e;
}
}
private function validateRequest(Request $request)
{
// Check for params that aren't allowed to be included unless the request is authenticated
foreach ($this->fieldsThatRequireAuth as $field) {
Base::getValueFromUrlParamsIfAllowed($field, $request);
}
// Special logic for timestamp as some overrides are OK without auth and others aren't
$request->getCurrentTimestamp();
}
/**
* Main algorithm to handle the visit.
*
* Once we have the visitor information, we have to determine if the visit is a new or a known visit.
*
* 1) When the last action was done more than 30min ago,
* or if the visitor is new, then this is a new visit.
*
* 2) If the last action is less than 30min ago, then the same visit is going on.
* Because the visit goes on, we can get the time spent during the last action.
*
* NB:
* - In the case of a new visit, then the time spent
* during the last action of the previous visit is unknown.
*
* - In the case of a new visit but with a known visitor,
* we can set the 'returning visitor' flag.
*
* In all the cases we set a cookie to the visitor with the new information.
*/
public function handle()
{
$this->checkSiteExists($this->request);
foreach ($this->requestProcessors as $processor) {
Common::printDebug("Executing " . get_class($processor) . "::manipulateRequest()...");
$processor->manipulateRequest($this->request);
}
$this->validateRequest($this->request);
$this->visitProperties = new VisitProperties();
foreach ($this->requestProcessors as $processor) {
Common::printDebug("Executing " . get_class($processor) . "::processRequestParams()...");
$abort = $processor->processRequestParams($this->visitProperties, $this->request);
if ($abort) {
Common::printDebug("-> aborting due to processRequestParams method");
return;
}
}
$isNewVisit = $this->request->getMetadata('CoreHome', 'isNewVisit');
if (!$isNewVisit) {
$isNewVisit = $this->triggerPredicateHookOnDimensions($this->getAllVisitDimensions(), 'shouldForceNewVisit');
$this->request->setMetadata('CoreHome', 'isNewVisit', $isNewVisit);
}
foreach ($this->requestProcessors as $processor) {
Common::printDebug("Executing " . get_class($processor) . "::afterRequestProcessed()...");
$abort = $processor->afterRequestProcessed($this->visitProperties, $this->request);
if ($abort) {
Common::printDebug("-> aborting due to afterRequestProcessed method");
return;
}
}
$isNewVisit = $this->request->getMetadata('CoreHome', 'isNewVisit');
// Known visit when:
// ( - the visitor has the Piwik cookie with the idcookie ID used by Piwik to match the visitor
// OR
// - the visitor doesn't have the Piwik cookie but could be match using heuristics @see recognizeTheVisitor()
// )
// AND
// - the last page view for this visitor was less than 30 minutes ago @see isLastActionInTheSameVisit()
if (!$isNewVisit) {
try {
$this->handleExistingVisit($this->request->getMetadata('Goals', 'visitIsConverted'));
} catch (VisitorNotFoundInDb $e) {
$this->request->setMetadata('CoreHome', 'visitorNotFoundInDb', true); // TODO: perhaps we should just abort here?
}
}
// New visit when:
// - the visitor has the Piwik cookie but the last action was performed more than 30 min ago @see isLastActionInTheSameVisit()
// - the visitor doesn't have the Piwik cookie, and couldn't be matched in @see recognizeTheVisitor()
// - the visitor does have the Piwik cookie but the idcookie and idvisit found in the cookie didn't match to any existing visit in the DB
if ($isNewVisit) {
$this->handleNewVisit($this->request->getMetadata('Goals', 'visitIsConverted'));
}
// update the cookie with the new visit information
$this->request->setThirdPartyCookie($this->request->getVisitorIdForThirdPartyCookie());
foreach ($this->requestProcessors as $processor) {
Common::printDebug("Executing " . get_class($processor) . "::recordLogs()...");
$processor->recordLogs($this->visitProperties, $this->request);
}
$this->markArchivedReportsAsInvalidIfArchiveAlreadyFinished();
}
/**
* In the case of a known visit, we have to do the following actions:
*
* 1) Insert the new action
* 2) Update the visit information
*
* @param Visitor $visitor
* @param Action $action
* @param $visitIsConverted
* @throws VisitorNotFoundInDb
*/
protected function handleExistingVisit($visitIsConverted)
{
Common::printDebug("Visit is known (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")");
// TODO it should be its own dimension
$this->visitProperties->setProperty('time_spent_ref_action', $this->getTimeSpentReferrerAction());
$valuesToUpdate = $this->getExistingVisitFieldsToUpdate($visitIsConverted);
// update visitorInfo
foreach ($valuesToUpdate as $name => $value) {
$this->visitProperties->setProperty($name, $value);
}
foreach ($this->requestProcessors as $processor) {
$processor->onExistingVisit($valuesToUpdate, $this->visitProperties, $this->request);
}
$this->updateExistingVisit($valuesToUpdate);
$this->visitProperties->setProperty('visit_last_action_time', $this->request->getCurrentTimestamp());
}
/**
* @return int Time in seconds
*/
protected function getTimeSpentReferrerAction()
{
$timeSpent = $this->request->getCurrentTimestamp() -
$this->visitProperties->getProperty('visit_last_action_time');
if ($timeSpent < 0) {
$timeSpent = 0;
}
$visitStandardLength = $this->getVisitStandardLength();
if ($timeSpent > $visitStandardLength) {
$timeSpent = $visitStandardLength;
}
return $timeSpent;
}
/**
* In the case of a new visit, we have to do the following actions:
*
* 1) Insert the new action
*
* 2) Insert the visit information
*
* @param Visitor $visitor
* @param Action $action
* @param bool $visitIsConverted
*/
protected function handleNewVisit($visitIsConverted)
{
Common::printDebug("New Visit (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")");
$this->setNewVisitorInformation();
$dimensions = $this->getAllVisitDimensions();
$this->triggerHookOnDimensions($dimensions, 'onNewVisit');
if ($visitIsConverted) {
$this->triggerHookOnDimensions($dimensions, 'onConvertedVisit');
}
foreach ($this->requestProcessors as $processor) {
$processor->onNewVisit($this->visitProperties, $this->request);
}
$this->printVisitorInformation();
$idVisit = $this->insertNewVisit($this->visitProperties->getProperties());
$this->visitProperties->setProperty('idvisit', $idVisit);
$this->visitProperties->setProperty('visit_first_action_time', $this->request->getCurrentTimestamp());
$this->visitProperties->setProperty('visit_last_action_time', $this->request->getCurrentTimestamp());
}
private function getModel()
{
return new Model();
}
/**
* Returns visitor cookie
*
* @return string binary
*/
protected function getVisitorIdcookie()
{
$isKnown = $this->request->getMetadata('CoreHome', 'isVisitorKnown');
if ($isKnown) {
return $this->visitProperties->getProperty('idvisitor');
}
// If the visitor had a first party ID cookie, then we use this value
$idVisitor = $this->visitProperties->getProperty('idvisitor');
if (!empty($idVisitor)
&& Tracker::LENGTH_BINARY_ID == strlen($this->visitProperties->getProperty('idvisitor'))
) {
return $this->visitProperties->getProperty('idvisitor');
}
return Common::hex2bin($this->generateUniqueVisitorId());
}
/**
* @return string returns random 16 chars hex string
*/
public static function generateUniqueVisitorId()
{
return substr(Common::generateUniqId(), 0, Tracker::LENGTH_HEX_ID_STRING);
}
/**
* Returns the visitor's IP address
*
* @return string
*/
protected function getVisitorIp()
{
return $this->visitProperties->getProperty('location_ip');
}
/**
* Gets the UserSettings object
*
* @return Settings
*/
protected function getSettingsObject()
{
return $this->userSettings;
}
// is the host any of the registered URLs for this website?
public static function isHostKnownAliasHost($urlHost, $idSite)
{
$websiteData = Cache::getCacheWebsiteAttributes($idSite);
if (isset($websiteData['hosts'])) {
$canonicalHosts = array();
foreach ($websiteData['hosts'] as $host) {
$canonicalHosts[] = self::toCanonicalHost($host);
}
$canonicalHost = self::toCanonicalHost($urlHost);
if (in_array($canonicalHost, $canonicalHosts)) {
return true;
}
}
return false;
}
private static function toCanonicalHost($host)
{
$hostLower = Common::mb_strtolower($host);
return str_replace('www.', '', $hostLower);
}
/**
* @param $valuesToUpdate
* @throws VisitorNotFoundInDb
*/
protected function updateExistingVisit($valuesToUpdate)
{
if (empty($valuesToUpdate)) {
Common::printDebug('There are no values to be updated for this visit');
return;
}
$idSite = $this->request->getIdSite();
$idVisit = $this->visitProperties->getProperty('idvisit');
$wasInserted = $this->getModel()->updateVisit($idSite, $idVisit, $valuesToUpdate);
// Debug output
if (isset($valuesToUpdate['idvisitor'])) {
$valuesToUpdate['idvisitor'] = bin2hex($valuesToUpdate['idvisitor']);
}
if ($wasInserted) {
Common::printDebug('Updated existing visit: ' . var_export($valuesToUpdate, true));
} elseif (!$this->getModel()->hasVisit($idSite, $idVisit)) {
// mostly for WordPress. see https://github.com/matomo-org/matomo/pull/15587
// as WP doesn't set `MYSQLI_CLIENT_FOUND_ROWS` and therefore when the update succeeded but no value changed
// it would still return 0 vs OnPremise would return 1 or 2.
throw new VisitorNotFoundInDb(
"The visitor with idvisitor=" . bin2hex($this->visitProperties->getProperty('idvisitor'))
. " and idvisit=" . @$this->visitProperties->getProperty('idvisit')
. " wasn't found in the DB, we fallback to a new visitor");
}
}
private function printVisitorInformation()
{
$debugVisitInfo = $this->visitProperties->getProperties();
$debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']);
$debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']);
$debugVisitInfo['location_ip'] = IPUtils::binaryToStringIP($debugVisitInfo['location_ip']);
Common::printDebug($debugVisitInfo);
}
private function setNewVisitorInformation()
{
$idVisitor = $this->getVisitorIdcookie();
$visitorIp = $this->getVisitorIp();
$configId = $this->request->getMetadata('CoreHome', 'visitorId');
$this->visitProperties->clearProperties();
$this->visitProperties->setProperty('idvisitor', $idVisitor);
$this->visitProperties->setProperty('config_id', $configId);
$this->visitProperties->setProperty('location_ip', $visitorIp);
}
/**
* Gather fields=>values that needs to be updated for the existing visit in log_visit
*
* @param $visitIsConverted
* @return array
*/
private function getExistingVisitFieldsToUpdate($visitIsConverted)
{
$valuesToUpdate = array();
$valuesToUpdate = $this->setIdVisitorForExistingVisit($valuesToUpdate);
$dimensions = $this->getAllVisitDimensions();
$valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onExistingVisit', $valuesToUpdate);
if ($visitIsConverted) {
$valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onConvertedVisit', $valuesToUpdate);
}
// Custom Variables overwrite previous values on each page view
return $valuesToUpdate;
}
/**
* @param VisitDimension[] $dimensions
* @param string $hook
* @param Visitor $visitor
* @param Action|null $action
* @param array|null $valuesToUpdate If null, $this->visitorInfo will be updated
*
* @return array|null The updated $valuesToUpdate or null if no $valuesToUpdate given
*/
private function triggerHookOnDimensions($dimensions, $hook, $valuesToUpdate = null)
{
$visitor = $this->makeVisitorFacade();
/** @var Action $action */
$action = $this->request->getMetadata('Actions', 'action');
foreach ($dimensions as $dimension) {
$value = $dimension->$hook($this->request, $visitor, $action);
if ($value !== false) {
$fieldName = $dimension->getColumnName();
$visitor->setVisitorColumn($fieldName, $value);
if (is_float($value)) {
$value = Common::forceDotAsSeparatorForDecimalPoint($value);
}
if ($valuesToUpdate !== null) {
$valuesToUpdate[$fieldName] = $value;
} else {
$this->visitProperties->setProperty($fieldName, $value);
}
}
}
return $valuesToUpdate;
}
private function triggerPredicateHookOnDimensions($dimensions, $hook)
{
$visitor = $this->makeVisitorFacade();
/** @var Action $action */
$action = $this->request->getMetadata('Actions', 'action');
foreach ($dimensions as $dimension) {
if ($dimension->$hook($this->request, $visitor, $action)) {
return true;
}
}
return false;
}
protected function getAllVisitDimensions()
{
if (is_null(self::$dimensions)) {
self::$dimensions = VisitDimension::getAllDimensions();
$dimensionNames = array();
foreach (self::$dimensions as $dimension) {
$dimensionNames[] = $dimension->getColumnName();
}
Common::printDebug("Following dimensions have been collected from plugins: " . implode(", ",
$dimensionNames));
}
return self::$dimensions;
}
private function getVisitStandardLength()
{
return Config::getInstance()->Tracker['visit_standard_length'];
}
/**
* @param $visitor
* @param $valuesToUpdate
* @return mixed
*/
private function setIdVisitorForExistingVisit($valuesToUpdate)
{
// Might update the idvisitor when it was forced or overwritten for this visit
if (strlen($this->visitProperties->getProperty('idvisitor')) == Tracker::LENGTH_BINARY_ID) {
$binIdVisitor = $this->visitProperties->getProperty('idvisitor');
$valuesToUpdate['idvisitor'] = $binIdVisitor;
}
return $valuesToUpdate;
}
protected function insertNewVisit($visit)
{
return $this->getModel()->createVisit($visit);
}
private function markArchivedReportsAsInvalidIfArchiveAlreadyFinished()
{
$idSite = (int)$this->request->getIdSite();
$time = $this->request->getCurrentTimestamp();
$timezone = $this->getTimezoneForSite($idSite);
if (!isset($timezone)) {
return;
}
$date = Date::factory((int)$time, $timezone);
// $date->isToday() is buggy when server and website timezones don't match - so we'll do our own checking
$startOfToday = Date::factoryInTimezone('yesterday', $timezone)->addDay(1);
$isLaterThanYesterday = $date->getTimestamp() >= $startOfToday->getTimestamp();
if ($isLaterThanYesterday) {
return; // don't try to invalidate archives for today or later
}
$this->invalidator->rememberToInvalidateArchivedReportsLater($idSite, $date);
}
private function getTimezoneForSite($idSite)
{
try {
$site = Cache::getCacheWebsiteAttributes($idSite);
} catch (UnexpectedWebsiteFoundException $e) {
return null;
}
if (!empty($site['timezone'])) {
return $site['timezone'];
}
}
private function makeVisitorFacade()
{
return Visitor::makeFromVisitProperties($this->visitProperties, $this->request);
}
}
|