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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Live;
use Exception;
use Piwik\DataTable;
use Piwik\Plugins\Live\Exception\MaxExecutionTimeExceededException;
class VisitorProfile
{
public const VISITOR_PROFILE_MAX_VISITS_TO_SHOW = 10;
protected $profile = [];
protected $idSite;
public function __construct($idSite)
{
$this->idSite = $idSite;
}
/**
* @param $visitorId
* @param $segment
* @param $numLastVisits
* @return array
* @throws Exception
*/
public function makeVisitorProfile(DataTable $visits, $visitorId, $segment, $numLastVisits)
{
$visitorDetailsManipulators = Visitor::getAllVisitorDetailsInstances();
$this->profile['visitorId'] = $visitorId;
$this->profile['hasMoreVisits'] = $visits->getMetadata('hasMoreVisits');
$this->profile['visit_first'] = $visits->getLastRow();
$this->profile['visit_last'] = $visits->getFirstRow();
foreach ($visitorDetailsManipulators as $instance) {
$instance->initProfile($visits, $this->profile);
}
/** @var DataTable\Row $visit */
foreach ($visits->getRows() as $visit) {
foreach ($visitorDetailsManipulators as $instance) {
$instance->handleProfileVisit($visit, $this->profile);
}
foreach ($visit->getColumn('actionDetails') as $action) {
foreach ($visitorDetailsManipulators as $instance) {
$instance->handleProfileAction($action, $this->profile);
}
}
}
// use N most recent visits for last_visits
$visits->deleteRowsOffset($numLastVisits);
$this->profile['lastVisits'] = $visits;
$this->handleAdjacentVisitorIds($visits, $visitorId, $segment);
foreach ($visitorDetailsManipulators as $instance) {
$instance->finalizeProfile($visits, $this->profile);
}
unset($this->profile['visit_first'], $this->profile['visit_last']);
return $this->profile;
}
/**
* @param $visitorId
* @param $segment
*/
private function handleAdjacentVisitorIds(DataTable $visits, $visitorId, $segment)
{
if (!$visits->getRowsCount()) {
$this->profile['nextVisitorId'] = false;
$this->profile['previousVisitorId'] = false;
return;
}
// get visitor IDs that are adjacent to this one in log_visit
// TODO: make sure order of visitor ids is not changed if a returning visitor visits while the user is
// looking at the popup.
$rows = $visits->getRows();
$latestVisitTime = reset($rows)->getColumn('lastActionDateTime');
$model = new Model();
try {
$this->profile['nextVisitorId'] = $model->queryAdjacentVisitorId($this->idSite, $visitorId, $latestVisitTime, $segment, $getNext = true);
} catch (MaxExecutionTimeExceededException $e) {
$this->profile['nextVisitorId'] = false;
$this->profile['previousVisitorId'] = false; // if query for next visitor is too slow, we assume query for previous visitor is too slow too
return;
}
try {
$this->profile['previousVisitorId'] = $model->queryAdjacentVisitorId($this->idSite, $visitorId, $latestVisitTime, $segment, $getNext = false);
} catch (MaxExecutionTimeExceededException $e) {
// we simply assume there is no previous visitor in that case
$this->profile['previousVisitorId'] = false;
}
}
}
|