diff --git a/core/Plugin/ActionDimension.php b/core/Plugin/ActionDimension.php index adce8f3931b..f079d7da658 100644 --- a/core/Plugin/ActionDimension.php +++ b/core/Plugin/ActionDimension.php @@ -10,8 +10,10 @@ use Piwik\Common; use Piwik\Db; +use Piwik\Tracker\Action; use Piwik\Tracker\Request; use Piwik\Plugin\Manager as PluginManager; +use Piwik\Tracker\Visitor; /** * @api @@ -139,4 +141,9 @@ public static function getDimensions(\Piwik\Plugin $plugin) return $instances; } + public function onNewAction(Request $request, Visitor $visitor, Action $action) + { + return false; + } + } diff --git a/core/Plugin/VisitDimension.php b/core/Plugin/VisitDimension.php index eb99351062e..2c53c58d55d 100644 --- a/core/Plugin/VisitDimension.php +++ b/core/Plugin/VisitDimension.php @@ -48,18 +48,11 @@ public function hasImplementedEvent($method) public function install() { - if (empty($this->fieldName) || empty($this->fieldType)) { - return; - } - try { - if ($this->hasImplementedEvent('onNewVisit') - || $this->hasImplementedEvent('onExistingVisit') - || $this->hasImplementedEvent('onConvertedVisit') ) { + if ($this->isHandlingLogVisit()) { $sql = "ALTER TABLE `" . Common::prefixTable("log_visit") . "` ADD `$this->fieldName` $this->fieldType"; Db::exec($sql); } - } catch (\Exception $e) { if (!Db::get()->isErrNo($e, '1060')) { throw $e; @@ -67,11 +60,10 @@ public function install() } try { - if ($this->hasImplementedEvent('onRecordGoal')) { + if ($this->isHandlingLogConversion()) { $sql = "ALTER TABLE `" . Common::prefixTable("log_conversion") . "` ADD `$this->fieldName` $this->fieldType"; Db::exec($sql); } - } catch (\Exception $e) { if (!Db::get()->isErrNo($e, '1060')) { throw $e; @@ -79,6 +71,26 @@ public function install() } } + private function isHandlingLogVisit() + { + if (empty($this->fieldName) || empty($this->fieldType)) { + return false; + } + + return $this->hasImplementedEvent('onNewVisit') + || $this->hasImplementedEvent('onExistingVisit') + || $this->hasImplementedEvent('onConvertedVisit'); + } + + private function isHandlingLogConversion() + { + if (empty($this->fieldName) || empty($this->fieldType)) { + return false; + } + + return $this->hasImplementedEvent('onRecordGoal'); + } + public function uninstall() { if (empty($this->fieldName) || empty($this->fieldType)) { @@ -86,13 +98,10 @@ public function uninstall() } try { - if ($this->hasImplementedEvent('onNewVisit') - || $this->hasImplementedEvent('onExistingVisit') - || $this->hasImplementedEvent('onConvertedVisit') ) { + if ($this->isHandlingLogVisit()) { $sql = "ALTER TABLE `" . Common::prefixTable("log_visit") . "` DROP COLUMN `$this->fieldName`"; Db::exec($sql); } - } catch (\Exception $e) { if (!Db::get()->isErrNo($e, '1091')) { throw $e; @@ -100,11 +109,10 @@ public function uninstall() } try { - if ($this->hasImplementedEvent('onRecordGoal')) { + if ($this->isHandlingLogConversion()) { $sql = "ALTER TABLE `" . Common::prefixTable("log_conversion") . "` DROP COLUMN `$this->fieldName`"; Db::exec($sql); } - } catch (\Exception $e) { if (!Db::get()->isErrNo($e, '1091')) { throw $e; diff --git a/core/Tracker/Action.php b/core/Tracker/Action.php index 02067c21016..a3650a9ad4d 100644 --- a/core/Tracker/Action.php +++ b/core/Tracker/Action.php @@ -99,7 +99,7 @@ static private function getAllActions(Request $request) /** @var \Piwik\Tracker\Action $instance */ $instance = new $action($request); - if ($instance->shouldHandle($request)) { + if ($instance->shouldHandle()) { $instances[] = $instance; } } @@ -314,12 +314,10 @@ public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerAction $dimensions = ActionDimension::getAllDimensions(); foreach ($dimensions as $dimension) { - if (method_exists($dimension, 'onNewAction')) { - $value = $dimension->onNewAction($this->request, $this, $visitor); + $value = $dimension->onNewAction($this->request, $visitor, $this); - if ($value !== false) { - $visitAction[$dimension->getFieldName()] = $value; - } + if ($value !== false) { + $visitAction[$dimension->getFieldName()] = $value; } } diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index be3f415b529..57b4ecac3c3 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -81,10 +81,9 @@ public function setRequest(Request $request) public function handle() { // the IP is needed by isExcluded() and GoalManager->recordGoals() - $ip = $this->request->getIp(); - $this->visitorInfo['location_ip'] = $ip; + $this->visitorInfo['location_ip'] = $this->request->getIp(); - $excluded = new VisitExcluded($this->request, $ip); + $excluded = new VisitExcluded($this->request, $this->visitorInfo['location_ip']); if ($excluded->isExcluded()) { return; } @@ -186,7 +185,7 @@ public function handle() } // When the row wasn't found in the logs, and this is a pageview or // goal matching URL, we force a new visitor else { - $visitor->setIsVisitorKonwn(false); + $visitor->setIsVisitorKnown(false); } } } @@ -295,12 +294,15 @@ protected function handleNewVisit($visitor, $action, $visitIsConverted) { Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")"); - $idVisitor = $this->getVisitorIdcookie($visitor); - $this->visitorInfo = $this->getNewVisitorInformation($idVisitor); + $this->visitorInfo = $this->getNewVisitorInformation($visitor); // Add Custom variable key,value to the visitor array $this->visitorInfo = array_merge($this->visitorInfo, $this->visitorCustomVariables); + foreach ($this->visitorInfo as $key => $value) { + $visitor->setVisitorColumn($key, $value); + } + $dimensions = VisitDimension::getAllDimensions(); $this->triggerHookOnDimensions($dimensions, 'onNewVisit', $visitor, $action); @@ -485,10 +487,10 @@ protected function printVisitorInformation() Common::printDebug($debugVisitInfo); } - protected function getNewVisitorInformation($idVisitor) + protected function getNewVisitorInformation($visitor) { return array( - 'idvisitor' => $idVisitor, + 'idvisitor' => $this->getVisitorIdcookie($visitor), 'config_id' => $this->getSettingsObject()->getConfigId(), 'location_ip' => $this->getVisitorIp(), ); @@ -509,6 +511,7 @@ protected function getExistingVisitFieldsToUpdate($visitor, $action, $visitIsCon // Might update the idvisitor when it was forced or overwritten for this visit if (strlen($this->visitorInfo['idvisitor']) == Tracker::LENGTH_BINARY_ID) { $valuesToUpdate['idvisitor'] = $this->visitorInfo['idvisitor']; + $visitor->setVisitorColumn('idvisitor', $this->visitorInfo['idvisitor']); } $dimensions = VisitDimension::getAllDimensions(); diff --git a/core/Tracker/Visitor.php b/core/Tracker/Visitor.php index dbdcac29886..697697c6a52 100644 --- a/core/Tracker/Visitor.php +++ b/core/Tracker/Visitor.php @@ -39,7 +39,7 @@ public function __construct(Request $request, $configId, $visitorInfo = array(), */ public function recognize() { - $this->setIsVisitorKonwn(false); + $this->setIsVisitorKnown(false); $configId = $this->configId; @@ -188,7 +188,7 @@ public function recognize() } } - $this->setIsVisitorKonwn(true); + $this->setIsVisitorKnown(true); Common::printDebug("The visitor is known (idvisitor = " . bin2hex($this->visitorInfo['idvisitor']) . ", config_id = " . bin2hex($configId) . ", idvisit = {$this->visitorInfo['idvisit']}, @@ -323,7 +323,7 @@ public function isVisitorKnown() return $this->visitorKnown === true; } - public function setIsVisitorKonwn($isVisitorKnown) + public function setIsVisitorKnown($isVisitorKnown) { return $this->visitorKnown = $isVisitorKnown; } diff --git a/plugins/Actions/Columns/ServerTime.php b/plugins/Actions/Columns/ServerTime.php index 802799bb25c..8ca5d52470f 100644 --- a/plugins/Actions/Columns/ServerTime.php +++ b/plugins/Actions/Columns/ServerTime.php @@ -34,7 +34,7 @@ public function getName() return ''; } - public function onNewAction(Request $request, Action $action, Visitor $visitor) + public function onNewAction(Request $request, Visitor $visitor, Action $action) { $timestamp = $request->getCurrentTimestamp(); diff --git a/plugins/Actions/Columns/TimeSpentRefAction.php b/plugins/Actions/Columns/TimeSpentRefAction.php index 44a1094348e..ef79927eb4a 100644 --- a/plugins/Actions/Columns/TimeSpentRefAction.php +++ b/plugins/Actions/Columns/TimeSpentRefAction.php @@ -11,7 +11,7 @@ use Piwik\Plugin\ActionDimension; use Piwik\Tracker\Action; use Piwik\Tracker\Request; -use Piwik\Tracker; +use Piwik\Tracker\Visitor; class TimeSpentRefAction extends ActionDimension { @@ -23,7 +23,7 @@ public function getName() return ''; } - public function onNewAction(Request $request, Action $action, Tracker\Visitor $visitor) + public function onNewAction(Request $request, Visitor $visitor, Action $action) { $timeSpent = $visitor->getVisitorColumn('time_spent_ref_action');