| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Tracker; |
|
|
| use Piwik\Common; |
| use Piwik\EventDispatcher; |
| use Piwik\Plugin\Dimension\VisitDimension; |
| use Piwik\Tracker\Visit\VisitProperties; |
|
|
| |
| |
| |
| class VisitorRecognizer |
| { |
| |
| |
| |
| |
| public const KEY_ORIGINAL_VISIT_ROW = 'originalVisit'; |
|
|
| |
| |
| |
| |
| |
| private $visitFieldsToSelect; |
|
|
| |
| |
| |
| |
| |
| private $trustCookiesOnly; |
|
|
| |
| |
| |
| |
| |
| private $visitStandardLength; |
|
|
| |
| |
| |
| |
| |
| |
| private $lookBackNSecondsCustom; |
|
|
| |
| |
| |
| private $model; |
|
|
| |
| |
| |
| private $eventDispatcher; |
|
|
| |
| |
| |
| private $visitRow; |
|
|
| public function __construct( |
| $trustCookiesOnly, |
| $visitStandardLength, |
| $lookbackNSecondsCustom, |
| Model $model, |
| EventDispatcher $eventDispatcher |
| ) { |
| $this->trustCookiesOnly = $trustCookiesOnly; |
| $this->visitStandardLength = $visitStandardLength; |
| $this->lookBackNSecondsCustom = $lookbackNSecondsCustom; |
|
|
| $this->model = $model; |
| $this->eventDispatcher = $eventDispatcher; |
| } |
|
|
| public function setTrustCookiesOnly($trustCookiesOnly) |
| { |
| $this->trustCookiesOnly = $trustCookiesOnly; |
| } |
|
|
| public function findKnownVisitor($configId, VisitProperties $visitProperties, Request $request) |
| { |
| $idSite = $request->getIdSite(); |
| $idVisitor = $request->getVisitorId(); |
| $userId = $request->getForcedUserId(); |
|
|
| $isVisitorIdToLookup = !empty($idVisitor); |
|
|
| if ($isVisitorIdToLookup) { |
| $visitProperties->setProperty('idvisitor', $idVisitor); |
| Common::printDebug("Matching visitors with: visitorId=" . bin2hex($idVisitor) . " OR configId=" . bin2hex($configId)); |
| } else { |
| Common::printDebug("Visitor doesn't have the piwik cookie..."); |
| } |
|
|
| $persistedVisitAttributes = $this->getVisitorFieldsPersist(); |
|
|
| $shouldMatchOneFieldOnly = $this->shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup, $request); |
| list($timeLookBack, $timeLookAhead) = $this->getWindowLookupThisVisit($request); |
|
|
| $maxActions = TrackerConfig::getConfigValue('create_new_visit_after_x_actions', $request->getIdSiteIfExists()); |
|
|
| $visitRow = $this->model->findVisitor($idSite, $configId, $idVisitor, $userId, $persistedVisitAttributes, $shouldMatchOneFieldOnly, $isVisitorIdToLookup, $timeLookBack, $timeLookAhead); |
|
|
| if ( |
| !empty($maxActions) && $maxActions > 0 |
| && !empty($visitRow['visit_total_actions']) |
| && $maxActions <= $visitRow['visit_total_actions'] |
| ) { |
| $this->visitRow = false; |
| return false; |
| } |
|
|
| $this->visitRow = $visitRow; |
|
|
| if ( |
| $visitRow |
| && count($visitRow) > 0 |
| ) { |
| $visitProperties->setProperty('idvisitor', $visitRow['idvisitor']); |
| $visitProperties->setProperty('user_id', $visitRow['user_id']); |
|
|
| Common::printDebug("The visitor is known (idvisitor = " . bin2hex($visitProperties->getProperty('idvisitor')) . ", |
| config_id = " . bin2hex($configId) . ", |
| last action = " . date("r", $visitProperties->getProperty('visit_last_action_time')) . ", |
| first action = " . date("r", $visitProperties->getProperty('visit_first_action_time')) . ")"); |
|
|
| return true; |
| } else { |
| Common::printDebug("The visitor was not matched with an existing visitor..."); |
|
|
| return false; |
| } |
| } |
|
|
| public function removeUnchangedValues($visit, ?VisitProperties $originalVisit = null) |
| { |
| if (empty($originalVisit)) { |
| return $visit; |
| } |
|
|
| $originalRow = $originalVisit->getProperties(); |
| if ( |
| !empty($originalRow['idvisitor']) |
| && !empty($visit['idvisitor']) |
| && bin2hex($originalRow['idvisitor']) === bin2hex($visit['idvisitor']) |
| ) { |
| unset($visit['idvisitor']); |
| } |
|
|
| $fieldsToCompareValue = array('user_id', 'visit_last_action_time', 'visit_total_time'); |
| foreach ($fieldsToCompareValue as $field) { |
| if ( |
| !empty($originalRow[$field]) |
| && !empty($visit[$field]) |
| && $visit[$field] == $originalRow[$field] |
| ) { |
| |
| |
| |
| unset($visit[$field]); |
| } |
| } |
|
|
| return $visit; |
| } |
|
|
| public function updateVisitPropertiesFromLastVisitRow(VisitProperties $visitProperties) |
| { |
| |
| foreach ($this->getVisitorFieldsPersist() as $field) { |
| $value = $this->visitRow[$field]; |
| if ($field == 'visit_last_action_time' || $field == 'visit_first_action_time') { |
| $value = strtotime($value); |
| } |
|
|
| |
| $visitProperties->initializeProperty($field, $value); |
| } |
|
|
| Common::printDebug("The visit is part of an existing visit ( |
| idvisit = {$visitProperties->getProperty('idvisit')}, |
| visit_goal_buyer' = " . $visitProperties->getProperty('visit_goal_buyer') . ")"); |
| } |
|
|
| protected function shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup, Request $request) |
| { |
| $isForcedUserIdMustMatch = (false !== $request->getForcedUserId()); |
|
|
| |
| |
| if ($isVisitorIdToLookup && $this->trustCookiesOnly) { |
| return true; |
| } |
|
|
| if ($isForcedUserIdMustMatch) { |
| |
| return false; |
| } |
|
|
| |
| $isForcedVisitorIdMustMatch = ($request->getForcedVisitorId() != null); |
|
|
| if ($isForcedVisitorIdMustMatch) { |
| return true; |
| } |
|
|
| if (!$isVisitorIdToLookup) { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function getWindowLookupThisVisit(Request $request) |
| { |
| $lookAheadNSeconds = $this->visitStandardLength; |
| $lookBackNSeconds = $this->visitStandardLength; |
| if ($this->lookBackNSecondsCustom > $lookBackNSeconds) { |
| $lookBackNSeconds = $this->lookBackNSecondsCustom; |
| } |
|
|
| $timeLookBack = date('Y-m-d H:i:s', $request->getCurrentTimestamp() - $lookBackNSeconds); |
| $timeLookAhead = date('Y-m-d H:i:s', $request->getCurrentTimestamp() + $lookAheadNSeconds); |
|
|
| return array($timeLookBack, $timeLookAhead); |
| } |
|
|
| |
| |
| |
| private function getVisitorFieldsPersist() |
| { |
| if (is_null($this->visitFieldsToSelect)) { |
| $fields = array( |
| 'idvisitor', |
| 'idvisit', |
| 'user_id', |
|
|
| 'visit_exit_idaction_url', |
| 'visit_exit_idaction_name', |
| 'visitor_returning', |
| 'visitor_seconds_since_first', |
| 'visitor_seconds_since_order', |
| 'visitor_count_visits', |
| 'visit_goal_buyer', |
|
|
| 'location_country', |
| 'location_region', |
| 'location_city', |
| 'location_latitude', |
| 'location_longitude', |
|
|
| 'referer_name', |
| 'referer_keyword', |
| 'referer_type', |
| ); |
|
|
| $dimensions = VisitDimension::getAllDimensions(); |
|
|
| foreach ($dimensions as $dimension) { |
| if ($dimension->hasImplementedEvent('onExistingVisit') || $dimension->hasImplementedEvent('onAnyGoalConversion')) { |
| $fields[] = $dimension->getColumnName(); |
| } |
|
|
| foreach ($dimension->getRequiredVisitFields() as $field) { |
| $fields[] = $field; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $this->eventDispatcher->postEvent('Tracker.getVisitFieldsToPersist', array(&$fields)); |
|
|
| array_unshift($fields, 'visit_first_action_time'); |
| array_unshift($fields, 'visit_last_action_time'); |
|
|
| $this->visitFieldsToSelect = array_unique($fields); |
| } |
|
|
| return $this->visitFieldsToSelect; |
| } |
|
|
| public function getLastKnownVisit() |
| { |
| return $this->visitRow; |
| } |
| } |
|
|