diff options
-rw-r--r-- | core/Tracker/Visit.php | 62 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Tracker/PingRequestTest.php | 26 |
2 files changed, 45 insertions, 43 deletions
diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index 3811e81c9b..4c5bc200b0 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -9,9 +9,9 @@ namespace Piwik\Tracker; +use Piwik\Archive\ArchiveInvalidator; use Piwik\Common; use Piwik\Config; -use Piwik\Archive\ArchiveInvalidator; use Piwik\Date; use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Network\IPUtils; @@ -128,7 +128,7 @@ class Visit implements VisitInterface } elseif ($isManualGoalConversion) { // this request is from the JS call to piwikTracker.trackGoal() $someGoalsConverted = $this->goalManager->detectGoalId($this->request->getIdSite()); - $visitIsConverted = $someGoalsConverted; + $visitIsConverted = $someGoalsConverted; // if we find a idgoal in the URL, but then the goal is not valid, this is most likely a fake request if (!$someGoalsConverted) { @@ -142,7 +142,7 @@ class Visit implements VisitInterface $action->writeDebugInfo(); $someGoalsConverted = $this->goalManager->detectGoalsMatchingUrl($this->request->getIdSite(), $action); - $visitIsConverted = $someGoalsConverted; + $visitIsConverted = $someGoalsConverted; $action->loadIdsFromLogActionTable(); } @@ -151,7 +151,7 @@ class Visit implements VisitInterface * Visitor recognition */ $visitorId = $this->getSettingsObject()->getConfigId(); - $visitor = new Visitor($this->request, $visitorId, $this->visitorInfo, $this->visitorCustomVariables); + $visitor = new Visitor($this->request, $visitorId, $this->visitorInfo, $this->visitorCustomVariables); $visitor->recognize(); $this->visitorInfo = $visitor->getVisitorInfo(); @@ -169,10 +169,11 @@ class Visit implements VisitInterface // on a ping request that is received before the standard visit length, we just update the visit time w/o // adding a new action if ($this->isPingRequest()) { + Common::printDebug("-> ping=1 request: we do not track a new action."); $action = null; } - $idReferrerActionUrl = $this->visitorInfo['visit_exit_idaction_url']; + $idReferrerActionUrl = $this->visitorInfo['visit_exit_idaction_url']; $idReferrerActionName = $this->visitorInfo['visit_exit_idaction_name']; try { @@ -207,6 +208,14 @@ class Visit implements VisitInterface // - the visitor doesn't have the Piwik cookie, and couldn't be matched in @see recognizeTheVisitor() // - the visitor does have the Piwik cookie but the idcookie and idvisit found in the cookie didn't match to any existing visit in the DB if ($isNewVisit) { + + // When a ping request is received more than 30 min after the last request/ping, + // we choose not to create a new visit. + if ($this->isPingRequest()) { + Common::printDebug("-> ping=1 request: we do _not_ create a new visit."); + return; + } + $this->handleNewVisit($visitor, $action, $visitIsConverted); if (!is_null($action)) { $action->record($visitor, 0, 0); @@ -389,7 +398,8 @@ class Visit implements VisitInterface protected function getSettingsObject() { if (is_null($this->userSettings)) { - $this->userSettings = new Settings($this->request, $this->getVisitorIp(), SettingsPiwik::isSameFingerprintAcrossWebsites()); + $this->userSettings = new Settings($this->request, $this->getVisitorIp(), + SettingsPiwik::isSameFingerprintAcrossWebsites()); } return $this->userSettings; @@ -404,8 +414,8 @@ class Visit implements VisitInterface $lastActionTime = $visitor->getVisitorColumn('visit_last_action_time'); return isset($lastActionTime) - && false !== $lastActionTime - && ($lastActionTime > ($this->request->getCurrentTimestamp() - Config::getInstance()->Tracker['visit_standard_length'])); + && false !== $lastActionTime + && ($lastActionTime > ($this->request->getCurrentTimestamp() - Config::getInstance()->Tracker['visit_standard_length'])); } /** @@ -421,16 +431,16 @@ class Visit implements VisitInterface return false; } - $idSite = $this->request->getIdSite(); + $idSite = $this->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 = $this->request->getCurrentTimestamp(); - $now = Date::factory((int) $now, $timezone); + $date = Date::factory((int)$lastActionTime, $timezone); + $now = $this->request->getCurrentTimestamp(); + $now = Date::factory((int)$now, $timezone); return $date->toString() !== $now->toString(); } @@ -467,8 +477,8 @@ class Visit implements VisitInterface */ protected function updateExistingVisit($valuesToUpdate) { - $idSite = $this->request->getIdSite(); - $idVisit = (int) $this->visitorInfo['idvisit']; + $idSite = $this->request->getIdSite(); + $idVisit = (int)$this->visitorInfo['idvisit']; $wasInserted = $this->getModel()->updateVisit($idSite, $idVisit, $valuesToUpdate); @@ -505,7 +515,7 @@ class Visit implements VisitInterface { $idVisitor = $this->getVisitorIdcookie($visitor); $visitorIp = $this->getVisitorIp(); - $configId = $this->getSettingsObject()->getConfigId(); + $configId = $this->getSettingsObject()->getConfigId(); $this->visitorInfo = array(); $visitor->clearVisitorInfo(); @@ -533,11 +543,13 @@ class Visit implements VisitInterface $valuesToUpdate = $this->setIdVisitorForExistingVisit($visitor, $valuesToUpdate); - $dimensions = $this->getAllVisitDimensions(); - $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onExistingVisit', $visitor, $action, $valuesToUpdate); + $dimensions = $this->getAllVisitDimensions(); + $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onExistingVisit', $visitor, $action, + $valuesToUpdate); if ($visitIsConverted) { - $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onConvertedVisit', $visitor, $action, $valuesToUpdate); + $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onConvertedVisit', $visitor, $action, + $valuesToUpdate); } // Custom Variables overwrite previous values on each page view @@ -597,7 +609,8 @@ class Visit implements VisitInterface $dimensionNames[] = $dimension->getColumnName(); } - Common::printDebug("Following dimensions have been collected from plugins: " . implode(", ", $dimensionNames)); + Common::printDebug("Following dimensions have been collected from plugins: " . implode(", ", + $dimensionNames)); } return self::$dimensions; @@ -625,7 +638,7 @@ class Visit implements VisitInterface // User ID takes precedence and overwrites idvisitor value $userId = $this->request->getForcedUserId(); if ($userId) { - $userIdHash = $this->request->getUserIdHashed($userId); + $userIdHash = $this->request->getUserIdHashed($userId); $binIdVisitor = Common::hex2bin($userIdHash); $visitor->setVisitorColumn('idvisitor', $binIdVisitor); $valuesToUpdate['idvisitor'] = $binIdVisitor; @@ -667,7 +680,8 @@ class Visit implements VisitInterface return true; } - $shouldForceNewVisit = $this->triggerPredicateHookOnDimensions($this->getAllVisitDimensions(), 'shouldForceNewVisit', $visitor, $action); + $shouldForceNewVisit = $this->triggerPredicateHookOnDimensions($this->getAllVisitDimensions(), + 'shouldForceNewVisit', $visitor, $action); if ($shouldForceNewVisit) { return true; } @@ -677,8 +691,8 @@ class Visit implements VisitInterface private function markArchivedReportsAsInvalidIfArchiveAlreadyFinished() { - $idSite = (int) $this->request->getIdSite(); - $time = $this->request->getCurrentTimestamp(); + $idSite = (int)$this->request->getIdSite(); + $time = $this->request->getCurrentTimestamp(); $timezone = $this->getTimezoneForSite($idSite); @@ -686,7 +700,7 @@ class Visit implements VisitInterface return; } - $date = Date::factory((int) $time, $timezone); + $date = Date::factory((int)$time, $timezone); if (!$date->isToday()) { // we don't have to handle in case date is in future as it is not allowed by tracker $invalidReport = new ArchiveInvalidator(); diff --git a/tests/PHPUnit/Integration/Tracker/PingRequestTest.php b/tests/PHPUnit/Integration/Tracker/PingRequestTest.php index 16865f937b..8a0ec00f3c 100644 --- a/tests/PHPUnit/Integration/Tracker/PingRequestTest.php +++ b/tests/PHPUnit/Integration/Tracker/PingRequestTest.php @@ -64,7 +64,7 @@ class PingRequestTest extends IntegrationTestCase $this->assertInitialVisitIsExtended($pingTime, self::FIRST_VISIT_TIME, $checkModifiedDimensions = true); } - public function test_PingAfterThirtyMinutes_CreatesNewVisit_AndCreatesNewAction() + public function test_PingAfterThirtyMinutes_DoesNotCreateNewVisit() { $tracker = $this->getTracker(); @@ -77,10 +77,10 @@ class PingRequestTest extends IntegrationTestCase $pingTime = '2012-01-05 00:40:00'; $this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = false); - $this->assertPingCreatedNewVisit(self::FIRST_VISIT_TIME, $pingTime, $checkModifiedDimensions = false); + $this->assertPingDidNotCreateNewVisit(self::FIRST_VISIT_TIME, $checkModifiedDimensions = false); } - public function test_PingAfterThirtyMinutes_AndChangedDimensionValues_CreatesNewVisit_AndUsesNewDimensionValues() + public function test_PingAfterThirtyMinutes_AndChangedDimensionValues_DoesNotCreateNewVisit() { $tracker = $this->getTracker(); @@ -93,7 +93,7 @@ class PingRequestTest extends IntegrationTestCase $pingTime = '2012-01-05 00:40:00'; $this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = true); - $this->assertPingCreatedNewVisit(self::FIRST_VISIT_TIME, $pingTime, $checkModifiedDimensions = true); + $this->assertPingDidNotCreateNewVisit(self::FIRST_VISIT_TIME, $checkModifiedDimensions = true); } private function getTracker() @@ -214,28 +214,16 @@ class PingRequestTest extends IntegrationTestCase $this->assertEquals(self::CHANGED_REGION, $region); } - private function assertPingCreatedNewVisit($expectedFirstVisitTime, $newVisitTime, $checkPropertiesModified) + private function assertPingDidNotCreateNewVisit($expectedFirstVisitTime, $checkPropertiesModified) { - $this->assertVisitCount(2); - $this->assertActionCount(2); + $this->assertVisitCount(1); + $this->assertActionCount(1); $firstVisitEndTime = $this->getVisitLastActionTime($idVisit = 1); $this->assertEquals($expectedFirstVisitTime, $firstVisitEndTime); $firstVisitActionTime = $this->getLatestActionTime($idVisit = 1); $this->assertEquals($expectedFirstVisitTime, $firstVisitActionTime); - - $secondVisitEndTime = $this->getVisitLastActionTime($idVisit = 2); - $this->assertEquals($newVisitTime, $secondVisitEndTime); - - $secondVisitActionTime = $this->getLatestActionTime($idVisit = 2); - $this->assertEquals($newVisitTime, $secondVisitActionTime); - - if ($checkPropertiesModified) { - $this->assertVisitPropertiesAreChanged($idVisit = 2, $checkUnchangeable = true); - } else { - $this->assertVisitPropertiesAreUnchanged($idVisit = 2); - } } protected static function configureFixture($fixture) |