eventDispatcher = $eventDispatcher; $this->visitorRecognizer = $visitorRecognizer; $this->userSettings = $userSettings; $this->visitStandardLength = $visitStandardLength; } public function processRequestParams(VisitProperties $visitProperties, Request $request) { // the IP is needed by isExcluded() and GoalManager->recordGoals() $visitProperties->setProperty('location_ip', $request->getIp()); // TODO: move VisitExcluded logic to here (or move to service class stored in DI) $excluded = new VisitExcluded($request, $visitProperties->getProperty('location_ip')); if ($excluded->isExcluded()) { return true; } $privacyConfig = new PrivacyManagerConfig(); $ip = $request->getIpString(); if ($privacyConfig->useAnonymizedIpForVisitEnrichment) { $ip = $visitProperties->getProperty('location_ip'); } // visitor recognition $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); $request->setMetadata('CoreHome', 'isNewVisit', $isNewVisit); return false; } public function afterRequestProcessed(VisitProperties $visitProperties, Request $request) { $ip = $visitProperties->getProperty('location_ip'); /** * Triggered after visits are tested for exclusion so plugins can modify the IP address * persisted with a visit. * * This event is primarily used by the **PrivacyManager** plugin to anonymize IP addresses. * * @param string &$ip The visitor's IP address. */ $this->eventDispatcher->postEvent('Tracker.setVisitorIp', array(&$ip)); $visitProperties->setProperty('location_ip', $ip); } /** * Determines if the tracker if the current action should be treated as the start of a new visit or * an action in an existing visit. * * Note: public only for tests. * * @param VisitProperties $visitProperties The current visit/visitor information. * @param Request $request * @return bool */ public function isVisitNew(VisitProperties $visitProperties, Request $request) { $isKnown = $request->getMetadata('CoreHome', 'isVisitorKnown'); if (!$isKnown) { return true; } $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit($visitProperties, $request); if (!$isLastActionInTheSameVisit) { Common::printDebug("Visitor detected, but last action was more than 30 minutes ago..."); return true; } $wasLastActionYesterday = $this->wasLastActionNotToday($visitProperties, $request); $forceNewVisitAtMidnight = (bool) Config::getInstance()->Tracker['create_new_visit_after_midnight']; if ($wasLastActionYesterday && $forceNewVisitAtMidnight) { Common::printDebug("Visitor detected, but last action was yesterday..."); return true; } return false; } /** * Returns true if the last action was done during the last 30 minutes * @return bool */ protected function isLastActionInTheSameVisit(VisitProperties $visitProperties, Request $request) { $lastActionTime = $visitProperties->getProperty('visit_last_action_time'); return isset($lastActionTime) && false !== $lastActionTime && ($lastActionTime > ($request->getCurrentTimestamp() - $this->visitStandardLength)); } /** * Returns true if the last action was not today. * @param VisitProperties $visitor * @return bool */ private function wasLastActionNotToday(VisitProperties $visitProperties, Request $request) { $lastActionTime = $visitProperties->getProperty('visit_last_action_time'); 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; } }