diff options
author | diosmosis <benaka@piwik.pro> | 2015-08-06 14:54:34 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-08-07 17:15:43 +0300 |
commit | 2f91d3c83a834b0e451d3e58fa246ef0a87740fb (patch) | |
tree | 4c650ff22651921af10f84344557bf78106f4952 /core/Tracker | |
parent | 0f67ba1ae63db2ccef642ef215002379c3ee8bfe (diff) |
Add public methods to VisitProperties for property access and use instead of direct access.
Diffstat (limited to 'core/Tracker')
-rw-r--r-- | core/Tracker/GoalManager.php | 19 | ||||
-rw-r--r-- | core/Tracker/Visit.php | 57 | ||||
-rw-r--r-- | core/Tracker/Visit/VisitProperties.php | 54 | ||||
-rw-r--r-- | core/Tracker/Visitor.php | 6 | ||||
-rw-r--r-- | core/Tracker/VisitorRecognizer.php | 22 |
5 files changed, 105 insertions, 53 deletions
diff --git a/core/Tracker/GoalManager.php b/core/Tracker/GoalManager.php index 8b3c86734f..880e5c91b4 100644 --- a/core/Tracker/GoalManager.php +++ b/core/Tracker/GoalManager.php @@ -205,7 +205,7 @@ class GoalManager */ public function recordGoals(VisitProperties $visitProperties, Request $request) { - $visitorInformation = $visitProperties->visitorInfo; + $visitorInformation = $visitProperties->getProperties(); $visitCustomVariables = $visitProperties->getRequestMetadata('CustomVariables', 'visitCustomVariables'); /** @var Action $action */ @@ -315,9 +315,10 @@ class GoalManager $conversion['items'] = $itemsCount; if ($isThereExistingCartInVisit) { - $recorded = $this->getModel()->updateConversion($visitProperties->visitorInfo['idvisit'], self::IDGOAL_CART, $conversion); + $recorded = $this->getModel()->updateConversion( + $visitProperties->getProperty('idvisit'), self::IDGOAL_CART, $conversion); } else { - $recorded = $this->insertNewConversion($conversion, $visitProperties->visitorInfo, $request); + $recorded = $this->insertNewConversion($conversion, $visitProperties->getProperties(), $request); } if ($recorded) { @@ -335,7 +336,7 @@ class GoalManager * @param array $visitInformation The visit entity that we are tracking a conversion for. See what * information it contains [here](/guides/persistence-and-the-mysql-backend#visits). */ - Piwik::postEvent('Tracker.recordEcommerceGoal', array($conversion, $visitProperties->visitorInfo)); + Piwik::postEvent('Tracker.recordEcommerceGoal', array($conversion, $visitProperties->getProperties())); } /** @@ -665,12 +666,12 @@ class GoalManager // If multiple Goal conversions per visit, set a cache buster $conversion['buster'] = $convertedGoal['allow_multiple'] == 0 ? '0' - : $visitProperties->visitorInfo['visit_last_action_time']; + : $visitProperties->getProperty('visit_last_action_time'); $conversionDimensions = ConversionDimension::getAllDimensions(); $conversion = $this->triggerHookOnDimensions($request, $conversionDimensions, 'onGoalConversion', $visitor, $action, $conversion); - $this->insertNewConversion($conversion, $visitProperties->visitorInfo, $request); + $this->insertNewConversion($conversion, $visitProperties->getProperties(), $request); /** * Triggered after successfully recording a non-ecommerce conversion. @@ -791,9 +792,9 @@ class GoalManager private function getGoalFromVisitor(VisitProperties $visitProperties, Request $request, $action) { $goal = array( - 'idvisit' => $visitProperties->visitorInfo['idvisit'], - 'idvisitor' => $visitProperties->visitorInfo['idvisitor'], - 'server_time' => Date::getDatetimeFromTimestamp($visitProperties->visitorInfo['visit_last_action_time']) + 'idvisit' => $visitProperties->getProperty('idvisit'), + 'idvisitor' => $visitProperties->getProperty('idvisitor'), + 'server_time' => Date::getDatetimeFromTimestamp($visitProperties->getProperty('visit_last_action_time')), ); $visitDimensions = VisitDimension::getAllDimensions(); diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index 6b2883bf2b..da1ef85862 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -161,7 +161,7 @@ class Visit implements VisitInterface } // update the cookie with the new visit information - $this->request->setThirdPartyCookie($this->visitProperties->visitorInfo['idvisitor']); + $this->request->setThirdPartyCookie($this->visitProperties->getProperty('idvisitor')); foreach ($this->requestProcessors as $processor) { Common::printDebug("Executing " . get_class($processor) . "::recordLogs()..."); @@ -188,13 +188,13 @@ class Visit implements VisitInterface Common::printDebug("Visit is known (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")"); // TODO it should be its own dimension - $this->visitProperties->visitorInfo['time_spent_ref_action'] = $this->getTimeSpentReferrerAction(); + $this->visitProperties->setProperty('time_spent_ref_action', $this->getTimeSpentReferrerAction()); $valuesToUpdate = $this->getExistingVisitFieldsToUpdate($visitIsConverted); // update visitorInfo foreach ($valuesToUpdate as $name => $value) { - $this->visitProperties->visitorInfo[$name] = $value; + $this->visitProperties->setProperty($name, $value); } /** @@ -208,7 +208,7 @@ class Visit implements VisitInterface * @param array $visit The entire visit entity. Read [this](/guides/persistence-and-the-mysql-backend#visits) * to see what it contains. */ - Piwik::postEvent('Tracker.existingVisitInformation', array(&$valuesToUpdate, $this->visitProperties->visitorInfo)); + Piwik::postEvent('Tracker.existingVisitInformation', array(&$valuesToUpdate, $this->visitProperties->getProperties())); foreach ($this->requestProcessors as $processor) { $processor->onExistingVisit($valuesToUpdate, $this->visitProperties, $this->request); @@ -216,7 +216,7 @@ class Visit implements VisitInterface $this->updateExistingVisit($valuesToUpdate); - $this->visitProperties->visitorInfo['visit_last_action_time'] = $this->request->getCurrentTimestamp(); + $this->visitProperties->setProperty('visit_last_action_time', $this->request->getCurrentTimestamp()); } /** @@ -224,7 +224,8 @@ class Visit implements VisitInterface */ protected function getTimeSpentReferrerAction() { - $timeSpent = $this->request->getCurrentTimestamp() - $this->visitProperties->visitorInfo['visit_last_action_time']; + $timeSpent = $this->request->getCurrentTimestamp() - + $this->visitProperties->getProperty('visit_last_action_time'); if ($timeSpent < 0) { $timeSpent = 0; } @@ -270,7 +271,7 @@ class Visit implements VisitInterface * what information it contains. * @param \Piwik\Tracker\Request $request An object describing the tracking request being processed. */ - Piwik::postEvent('Tracker.newVisitorInformation', array(&$this->visitProperties->visitorInfo, $this->request)); + Piwik::postEvent('Tracker.newVisitorInformation', array($this->visitProperties->getProperties(), $this->request)); foreach ($this->requestProcessors as $processor) { $processor->onNewVisit($this->visitProperties, $this->request); @@ -278,11 +279,11 @@ class Visit implements VisitInterface $this->printVisitorInformation(); - $idVisit = $this->insertNewVisit($this->visitProperties->visitorInfo); + $idVisit = $this->insertNewVisit($this->visitProperties->getProperties()); - $this->visitProperties->visitorInfo['idvisit'] = $idVisit; - $this->visitProperties->visitorInfo['visit_first_action_time'] = $this->request->getCurrentTimestamp(); - $this->visitProperties->visitorInfo['visit_last_action_time'] = $this->request->getCurrentTimestamp(); + $this->visitProperties->setProperty('idvisit', $idVisit); + $this->visitProperties->setProperty('visit_first_action_time', $this->request->getCurrentTimestamp()); + $this->visitProperties->setProperty('visit_last_action_time', $this->request->getCurrentTimestamp()); } private function getModel() @@ -299,14 +300,15 @@ class Visit implements VisitInterface { $isKnown = $this->visitProperties->getRequestMetadata('CoreHome', 'isVisitorKnown'); if ($isKnown) { - return $this->visitProperties->visitorInfo['idvisitor']; + return $this->visitProperties->getProperty('idvisitor'); } // If the visitor had a first party ID cookie, then we use this value - if (!empty($this->visitProperties->visitorInfo['idvisitor']) - && Tracker::LENGTH_BINARY_ID == strlen($this->visitProperties->visitorInfo['idvisitor']) + $idVisitor = $this->visitProperties->getProperty('idvisitor'); + if (!empty($idVisitor) + && Tracker::LENGTH_BINARY_ID == strlen($this->visitProperties->getProperty('idvisitor')) ) { - return $this->visitProperties->visitorInfo['idvisitor']; + return $this->visitProperties->getProperty('idvisitor'); } return Common::hex2bin($this->generateUniqueVisitorId()); @@ -327,7 +329,7 @@ class Visit implements VisitInterface */ protected function getVisitorIp() { - return $this->visitProperties->visitorInfo['location_ip']; + return $this->visitProperties->getProperty('location_ip'); } /** @@ -373,7 +375,7 @@ class Visit implements VisitInterface protected function updateExistingVisit($valuesToUpdate) { $idSite = $this->request->getIdSite(); - $idVisit = (int)$this->visitProperties->visitorInfo['idvisit']; + $idVisit = (int)$this->visitProperties->getProperty('idvisit'); $wasInserted = $this->getModel()->updateVisit($idSite, $idVisit, $valuesToUpdate); @@ -386,14 +388,15 @@ class Visit implements VisitInterface Common::printDebug('Updated existing visit: ' . var_export($valuesToUpdate, true)); } else { throw new VisitorNotFoundInDb( - "The visitor with idvisitor=" . bin2hex($this->visitProperties->visitorInfo['idvisitor']) . " and idvisit=" . @$this->visitProperties->visitorInfo['idvisit'] + "The visitor with idvisitor=" . bin2hex($this->visitProperties->getProperty('idvisitor')) + . " and idvisit=" . @$this->visitProperties->getProperty('idvisit') . " wasn't found in the DB, we fallback to a new visitor"); } } private function printVisitorInformation() { - $debugVisitInfo = $this->visitProperties->visitorInfo; + $debugVisitInfo = $this->visitProperties->getProperties(); $debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']); $debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']); $debugVisitInfo['location_ip'] = IPUtils::binaryToStringIP($debugVisitInfo['location_ip']); @@ -406,11 +409,11 @@ class Visit implements VisitInterface $visitorIp = $this->getVisitorIp(); $configId = $this->visitProperties->getRequestMetadata('CoreHome', 'visitorId'); - $this->visitProperties->visitorInfo = array(); + $this->visitProperties->clearProperties(); - $this->visitProperties->visitorInfo['idvisitor'] = $idVisitor; - $this->visitProperties->visitorInfo['config_id'] = $configId; - $this->visitProperties->visitorInfo['location_ip'] = $visitorIp; + $this->visitProperties->setProperty('idvisitor', $idVisitor); + $this->visitProperties->setProperty('config_id', $configId); + $this->visitProperties->setProperty('location_ip', $visitorIp); } /** @@ -466,7 +469,7 @@ class Visit implements VisitInterface if ($valuesToUpdate !== null) { $valuesToUpdate[$fieldName] = $value; } else { - $this->visitProperties->visitorInfo[$fieldName] = $value; + $this->visitProperties->setProperty($fieldName, $value); } } } @@ -519,8 +522,8 @@ class Visit implements VisitInterface private function setIdVisitorForExistingVisit($valuesToUpdate) { // Might update the idvisitor when it was forced or overwritten for this visit - if (strlen($this->visitProperties->visitorInfo['idvisitor']) == Tracker::LENGTH_BINARY_ID) { - $binIdVisitor = $this->visitProperties->visitorInfo['idvisitor']; + if (strlen($this->visitProperties->getProperty('idvisitor')) == Tracker::LENGTH_BINARY_ID) { + $binIdVisitor = $this->visitProperties->getProperty('idvisitor'); $valuesToUpdate['idvisitor'] = $binIdVisitor; } @@ -529,7 +532,7 @@ class Visit implements VisitInterface if ($userId) { $userIdHash = $this->request->getUserIdHashed($userId); $binIdVisitor = Common::hex2bin($userIdHash); - $this->visitProperties->visitorInfo['idvisitor'] = $binIdVisitor; + $this->visitProperties->setProperty('idvisitor', $binIdVisitor); $valuesToUpdate['idvisitor'] = $binIdVisitor; } return $valuesToUpdate; diff --git a/core/Tracker/Visit/VisitProperties.php b/core/Tracker/Visit/VisitProperties.php index 7cc5133de7..30b3168f4c 100644 --- a/core/Tracker/Visit/VisitProperties.php +++ b/core/Tracker/Visit/VisitProperties.php @@ -10,8 +10,6 @@ namespace Piwik\Tracker\Visit; /** * Holds temporary data for tracking requests. - * - * RequestProcessors */ class VisitProperties { @@ -21,7 +19,7 @@ class VisitProperties * * @var array */ - public $visitorInfo = array(); + private $visitInfo = array(); /** * Stores plugin specific tracking request metadata. RequestProcessors can store @@ -55,4 +53,54 @@ class VisitProperties { return @$this->requestMetadata[$pluginName][$key]; } + + /** + * Returns a visit property, or `null` if none is set. + * + * @param string $name The property name. + * @return mixed + */ + public function getProperty($name) + { + return isset($this->visitInfo[$name]) ? $this->visitInfo[$name] : null; + } + + /** + * Returns all visit properties by reference. + * + * @return array + */ + public function &getProperties() + { + return $this->visitInfo; + } + + /** + * Sets a visit property. + * + * @param string $name The property name. + * @param mixed $value The property value. + */ + public function setProperty($name, $value) + { + $this->visitInfo[$name] = $value; + } + + /** + * Unsets all visit properties. + */ + public function clearProperties() + { + $this->visitInfo = array(); + } + + /** + * Sets all visit properties. + * + * @param array $properties + */ + public function setProperties($properties) + { + $this->visitInfo = $properties; + } } diff --git a/core/Tracker/Visitor.php b/core/Tracker/Visitor.php index cdd4ce7693..833e5c87ed 100644 --- a/core/Tracker/Visitor.php +++ b/core/Tracker/Visitor.php @@ -32,13 +32,13 @@ class Visitor public function setVisitorColumn($column, $value) { - $this->visitProperties->visitorInfo[$column] = $value; + $this->visitProperties->setProperty($column, $value); } public function getVisitorColumn($column) { - if (array_key_exists($column, $this->visitProperties->visitorInfo)) { - return $this->visitProperties->visitorInfo[$column]; + if (array_key_exists($column, $this->visitProperties->getProperties())) { + return $this->visitProperties->getProperty($column); } return false; diff --git a/core/Tracker/VisitorRecognizer.php b/core/Tracker/VisitorRecognizer.php index a76ad2c400..4f63017e43 100644 --- a/core/Tracker/VisitorRecognizer.php +++ b/core/Tracker/VisitorRecognizer.php @@ -85,7 +85,7 @@ class VisitorRecognizer $isVisitorIdToLookup = !empty($idVisitor); if ($isVisitorIdToLookup) { - $visitProperties->visitorInfo['idvisitor'] = $idVisitor; + $visitProperties->setProperty('idvisitor', $idVisitor); Common::printDebug("Matching visitors with: visitorId=" . bin2hex($idVisitor) . " OR configId=" . bin2hex($configId)); } else { Common::printDebug("Visitor doesn't have the piwik cookie..."); @@ -109,11 +109,11 @@ class VisitorRecognizer // These values will be used throughout the request foreach ($persistedVisitAttributes as $field) { - $visitProperties->visitorInfo[$field] = $visitRow[$field]; + $visitProperties->setProperty($field, $visitRow[$field]); } - $visitProperties->visitorInfo['visit_last_action_time'] = strtotime($visitRow['visit_last_action_time']); - $visitProperties->visitorInfo['visit_first_action_time'] = strtotime($visitRow['visit_first_action_time']); + $visitProperties->setProperty('visit_last_action_time', strtotime($visitRow['visit_last_action_time'])); + $visitProperties->setProperty('visit_first_action_time', strtotime($visitRow['visit_first_action_time'])); // Custom Variables copied from Visit in potential later conversion if (!empty($numCustomVarsToRead)) { @@ -121,22 +121,22 @@ class VisitorRecognizer if (isset($visitRow['custom_var_k' . $i]) && strlen($visitRow['custom_var_k' . $i]) ) { - $visitProperties->visitorInfo['custom_var_k' . $i] = $visitRow['custom_var_k' . $i]; + $visitProperties->setProperty('custom_var_k' . $i, $visitRow['custom_var_k' . $i]); } if (isset($visitRow['custom_var_v' . $i]) && strlen($visitRow['custom_var_v' . $i]) ) { - $visitProperties->visitorInfo['custom_var_v' . $i] = $visitRow['custom_var_v' . $i]; + $visitProperties->setProperty('custom_var_v' . $i, $visitRow['custom_var_v' . $i]); } } } - Common::printDebug("The visitor is known (idvisitor = " . bin2hex($visitProperties->visitorInfo['idvisitor']) . ", + Common::printDebug("The visitor is known (idvisitor = " . bin2hex($visitProperties->getProperty('idvisitor')) . ", config_id = " . bin2hex($configId) . ", - idvisit = {$visitProperties->visitorInfo['idvisit']}, - last action = " . date("r", $visitProperties->visitorInfo['visit_last_action_time']) . ", - first action = " . date("r", $visitProperties->visitorInfo['visit_first_action_time']) . ", - visit_goal_buyer' = " . $visitProperties->visitorInfo['visit_goal_buyer'] . ")"); + idvisit = {$visitProperties->getProperty('idvisit')}, + last action = " . date("r", $visitProperties->getProperty('visit_last_action_time')) . ", + first action = " . date("r", $visitProperties->getProperty('visit_first_action_time')) . ", + visit_goal_buyer' = " . $visitProperties->getProperty('visit_goal_buyer') . ")"); return true; } else { |