| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CoreHome\Tracker; |
|
|
| use Piwik\Common; |
| use Piwik\Date; |
| use Piwik\EventDispatcher; |
| use Piwik\Exception\UnexpectedWebsiteFoundException; |
| use Piwik\Tracker\Cache; |
| use Piwik\Tracker\Request; |
| use Piwik\Tracker\RequestProcessor; |
| use Piwik\Tracker\Settings; |
| use Piwik\Tracker\TrackerConfig; |
| use Piwik\Tracker\Visit\VisitProperties; |
| use Piwik\Tracker\VisitExcluded; |
| use Piwik\Tracker\VisitorRecognizer; |
| use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class VisitRequestProcessor extends RequestProcessor |
| { |
| |
|
|
| |
| |
| |
| private $eventDispatcher; |
|
|
| |
| |
| |
| private $visitorRecognizer; |
|
|
| |
| |
| |
| private $userSettings; |
|
|
| |
| |
| |
| private $visitStandardLength; |
|
|
| |
| |
| |
| |
| |
| private $trackerAlwaysNewVisitor; |
|
|
| public function __construct( |
| EventDispatcher $eventDispatcher, |
| VisitorRecognizer $visitorRecognizer, |
| Settings $userSettings, |
| $visitStandardLength, |
| $trackerAlwaysNewVisitor |
| ) { |
| $this->eventDispatcher = $eventDispatcher; |
| $this->visitorRecognizer = $visitorRecognizer; |
| $this->userSettings = $userSettings; |
| $this->visitStandardLength = $visitStandardLength; |
| $this->trackerAlwaysNewVisitor = $trackerAlwaysNewVisitor; |
| } |
|
|
| public function processRequestParams(VisitProperties $visitProperties, Request $request) |
| { |
| |
| $visitProperties->setProperty('location_ip', $request->getIp()); |
|
|
| $excluded = new VisitExcluded($request); |
| $isExcluded = $excluded->isExcluded(); |
| $request->setMetadata('CoreHome', 'isVisitExcluded', $isExcluded); |
|
|
| if ($isExcluded) { |
| return true; |
| } |
|
|
| $privacyConfig = new PrivacyManagerConfig($request->getIdSiteIfExists()); |
|
|
| if ($privacyConfig->randomizeConfigId) { |
| |
| $request->setMetadata('CoreHome', 'visitorId', $this->userSettings->getRandomConfigId()); |
| $request->setMetadata('CoreHome', 'isVisitorKnown', false); |
| $request->setMetadata('CoreHome', 'isNewVisit', true); |
| $request->setMetadata('CoreHome', 'lastKnownVisit', false); |
|
|
| return false; |
| } |
|
|
| $ip = $request->getIpString(); |
| if ($privacyConfig->useAnonymizedIpForVisitEnrichment) { |
| $ip = $visitProperties->getProperty('location_ip'); |
| } |
|
|
| |
| $visitorId = $this->userSettings->getConfigId($request, $ip); |
| $request->setMetadata('CoreHome', 'visitorId', $visitorId); |
|
|
| $isKnown = $this->visitorRecognizer->findKnownVisitor($visitorId, $visitProperties, $request); |
| $request->setMetadata('CoreHome', 'isVisitorKnown', $isKnown); |
|
|
| $isNewVisit = $this->isVisitNew($visitProperties, $request, $this->visitorRecognizer->getLastKnownVisit()); |
| $request->setMetadata('CoreHome', 'isNewVisit', $isNewVisit); |
|
|
| $request->setMetadata('CoreHome', 'lastKnownVisit', $this->visitorRecognizer->getLastKnownVisit()); |
|
|
| if (!$isNewVisit) { |
| $this->visitorRecognizer->updateVisitPropertiesFromLastVisitRow($visitProperties); |
| } |
|
|
| return false; |
| } |
|
|
| public function afterRequestProcessed(VisitProperties $visitProperties, Request $request) |
| { |
| $ip = $visitProperties->getProperty('location_ip'); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $this->eventDispatcher->postEvent('Tracker.setVisitorIp', [&$ip, (int) $request->getIdSiteIfExists()]); |
|
|
| $visitProperties->setProperty('location_ip', $ip); |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function isVisitNew(VisitProperties $visitProperties, Request $request, $lastKnownVisit) |
| { |
| $isKnown = $request->getMetadata('CoreHome', 'isVisitorKnown'); |
| if (!$isKnown) { |
| return true; |
| } |
|
|
| $isNewVisitForced = $request->getParam('new_visit'); |
| $isNewVisitForced = !empty($isNewVisitForced); |
| if ($isNewVisitForced) { |
| Common::printDebug("-> New visit forced: &new_visit=1 in request"); |
| return true; |
| } |
|
|
| if ($this->trackerAlwaysNewVisitor) { |
| Common::printDebug("-> New visit forced: Debug.tracker_always_new_visitor = 1 in config.ini.php"); |
| return true; |
| } |
|
|
| $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit($visitProperties, $request, $lastKnownVisit); |
| if (!$isLastActionInTheSameVisit) { |
| Common::printDebug("Visitor detected, but last action was more than 30 minutes ago..."); |
|
|
| return true; |
| } |
|
|
| $wasLastActionYesterday = $this->wasLastActionNotToday($visitProperties, $request, $lastKnownVisit); |
| $forceNewVisitAtMidnight = TrackerConfig::getBoolConfigValue('create_new_visit_after_midnight', false, $request->getIdSiteIfExists()); |
|
|
| if ($wasLastActionYesterday && $forceNewVisitAtMidnight) { |
| Common::printDebug("Visitor detected, but last action was yesterday..."); |
|
|
| return true; |
| } |
|
|
| if (!$this->lastUserIdWasSetAndDoesMatch($visitProperties, $request)) { |
| Common::printDebug("Visitor detected, but last user_id does not match..."); |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| protected function isLastActionInTheSameVisit(VisitProperties $visitProperties, Request $request, $lastKnownVisit) |
| { |
| $lastActionTime = $this->getLastKnownActionTime($visitProperties, $lastKnownVisit); |
|
|
| return isset($lastActionTime) |
| && false !== $lastActionTime |
| && ($lastActionTime > ($request->getCurrentTimestamp() - $this->visitStandardLength)); |
| } |
|
|
| |
| |
| |
| |
| private function wasLastActionNotToday(VisitProperties $visitProperties, Request $request, $lastKnownVisit) |
| { |
| $lastActionTime = $this->getLastKnownActionTime($visitProperties, $lastKnownVisit); |
|
|
| if (empty($lastActionTime)) { |
| return false; |
| } |
|
|
| $idSite = $request->getIdSite(); |
| $timezone = $this->getTimezoneForSite($idSite); |
|
|
| if (empty($timezone)) { |
| throw new UnexpectedWebsiteFoundException('An unexpected website was found, check idSite in the request'); |
| } |
|
|
| $date = Date::factory((int)$lastActionTime, $timezone); |
| $now = $request->getCurrentTimestamp(); |
| $now = Date::factory((int)$now, $timezone); |
|
|
| return $date->toString() !== $now->toString(); |
| } |
|
|
| private function getTimezoneForSite($idSite) // TODO: duplicate function in Visit |
| { |
| try { |
| $site = Cache::getCacheWebsiteAttributes($idSite); |
| } catch (UnexpectedWebsiteFoundException $e) { |
| return null; |
| } |
|
|
| if (!empty($site['timezone'])) { |
| return $site['timezone']; |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private function getLastKnownActionTime(VisitProperties $visitProperties, $lastKnownVisit) |
| { |
| if (isset($lastKnownVisit['visit_last_action_time'])) { |
| return strtotime($lastKnownVisit['visit_last_action_time']); |
| } |
|
|
| return $visitProperties->getProperty('visit_last_action_time'); |
| } |
|
|
| |
| |
| |
| |
| protected function lastUserIdWasSetAndDoesMatch(VisitProperties $visitProperties, Request $request) |
| { |
| $lastUserId = $visitProperties->getProperty('user_id'); |
|
|
| if (empty($lastUserId)) { |
| return true; |
| } |
|
|
| $currentUserId = $request->getForcedUserId(); |
|
|
| if (empty($currentUserId)) { |
| return true; |
| } |
|
|
| return $lastUserId === $currentUserId; |
| } |
| } |
|
|