diff options
author | diosmosis <benaka@piwik.pro> | 2015-07-10 01:22:22 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-08-06 17:37:58 +0300 |
commit | 620441141b12837d50f0020dcc208c3f1fb898f4 (patch) | |
tree | 0bb6a0dc301637f9d009434cf4af37d34adbd4d9 /plugins/CoreHome | |
parent | 1f3a3a7234edf5f4d932ec331faaccd49c2c7c65 (diff) |
Move 'is new visit' detection to CoreHome RequestProcessor and out of core\Tracker\Visit.php.
Diffstat (limited to 'plugins/CoreHome')
-rw-r--r-- | plugins/CoreHome/Tracker/VisitRequestProcessor.php | 117 |
1 files changed, 116 insertions, 1 deletions
diff --git a/plugins/CoreHome/Tracker/VisitRequestProcessor.php b/plugins/CoreHome/Tracker/VisitRequestProcessor.php index 596d0b926f..f531015424 100644 --- a/plugins/CoreHome/Tracker/VisitRequestProcessor.php +++ b/plugins/CoreHome/Tracker/VisitRequestProcessor.php @@ -8,11 +8,17 @@ 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\Visit\VisitProperties; use Piwik\Tracker\VisitExcluded; +use Piwik\Tracker\VisitorRecognizer; /** * Encapsulates core tracking logic related to visits. @@ -24,9 +30,21 @@ class VisitRequestProcessor extends RequestProcessor */ private $eventDispatcher; - public function __construct(EventDispatcher $eventDispatcher) + /** + * @var VisitorRecognizer + */ + private $visitorRecognizer; + + /** + * @var Settings + */ + private $userSettings; + + public function __construct(EventDispatcher $eventDispatcher, VisitorRecognizer $visitorRecognizer, Settings $userSettings) { $this->eventDispatcher = $eventDispatcher; + $this->visitorRecognizer = $visitorRecognizer; + $this->userSettings = $userSettings; } public function processRequestParams(VisitProperties $visitProperties, Request $request) @@ -40,6 +58,16 @@ class VisitRequestProcessor extends RequestProcessor return true; } + // visitor recognition + $visitorId = $this->userSettings->getConfigId($request, $visitProperties->visitorInfo['location_ip']); + $visitProperties->setRequestMetadata('CoreHome', 'visitorId', $visitorId); + + $isKnown = $this->visitorRecognizer->findKnownVisitor($visitorId, $visitProperties, $request); + $visitProperties->setRequestMetadata('CoreHome', 'isVisitorKnown', $isKnown); + + $isNewVisit = $this->isVisitNew($visitProperties, $request); + $visitProperties->setRequestMetadata('CoreHome', 'isNewVisit', $isNewVisit); + return false; } @@ -55,4 +83,91 @@ class VisitRequestProcessor extends RequestProcessor */ $this->eventDispatcher->postEvent('Tracker.setVisitorIp', array(&$visitProperties->visitorInfo['location_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. + * + * @param VisitProperties $visitProperties The current visit/visitor information. + * @param Request $request + * @return bool + */ + private function isVisitNew(VisitProperties $visitProperties, Request $request) + { + $isKnown = $visitProperties->getRequestMetadata('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); + if ($wasLastActionYesterday) { + 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->visitorInfo['visit_last_action_time']; + + return isset($lastActionTime) + && false !== $lastActionTime + && ($lastActionTime > ($request->getCurrentTimestamp() - \Piwik\Config::getInstance()->Tracker['visit_standard_length'])); // TODO: move to DI + } + + /** + * Returns true if the last action was not today. + * @param VisitProperties $visitor + * @return bool + */ + private function wasLastActionNotToday(VisitProperties $visitProperties, Request $request) + { + $lastActionTime = $visitProperties->visitorInfo['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; + } } |