Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@googlemail.com>2014-10-04 13:28:23 +0400
committerThomas Steur <thomas.steur@googlemail.com>2014-10-04 13:28:23 +0400
commitf05a25d60acb670d4292c67a10d0b408643ed3df (patch)
tree19438be0d2c12a71e80c8fe752e77be1f792802d /core/Tracker/Visitor.php
parent1a7dbc5ff6ab987338fa644f1c4c7a07602c10f6 (diff)
moved some queries within core to model files
This is only a start. Especially the Tracker model contains quite a bunch of different models. Ideally, one day, the tracker is refactored and all the goal tracker stuff including the queries are in the goal plugins, all the ecommerce stuff is in an ecommerce plugin etc. Haven't moved some of the LogAggregator queries.
Diffstat (limited to 'core/Tracker/Visitor.php')
-rw-r--r--core/Tracker/Visitor.php141
1 files changed, 32 insertions, 109 deletions
diff --git a/core/Tracker/Visitor.php b/core/Tracker/Visitor.php
index ec5dd6d279..afec61849e 100644
--- a/core/Tracker/Visitor.php
+++ b/core/Tracker/Visitor.php
@@ -25,9 +25,9 @@ class Visitor
public function __construct(Request $request, $configId, $visitorInfo = array(), $customVariables = null)
{
$this->request = $request;
+ $this->configId = $configId;
$this->visitorInfo = $visitorInfo;
$this->customVariables = $customVariables;
- $this->configId = $configId;
}
/**
@@ -41,120 +41,35 @@ class Visitor
{
$this->setIsVisitorKnown(false);
- $configId = $this->configId;
-
+ $configId = $this->configId;
+ $idSite = $this->request->getIdSite();
$idVisitor = $this->request->getVisitorId();
+
$isVisitorIdToLookup = !empty($idVisitor);
if ($isVisitorIdToLookup) {
$this->visitorInfo['idvisitor'] = $idVisitor;
- Common::printDebug("Matching visitors with: visitorId=" . bin2hex($this->visitorInfo['idvisitor']) . " OR configId=" . bin2hex($configId));
+ Common::printDebug("Matching visitors with: visitorId=" . bin2hex($idVisitor) . " OR configId=" . bin2hex($configId));
} else {
Common::printDebug("Visitor doesn't have the piwik cookie...");
}
- $selectCustomVariables = '';
- // No custom var were found in the request, so let's copy the previous one in a potential conversion later
+ $numCustomVarsToRead = 0;
if (!$this->customVariables) {
- $maxCustomVariables = CustomVariables::getMaxCustomVariables();
-
- for ($index = 1; $index <= $maxCustomVariables; $index++) {
- $selectCustomVariables .= ', custom_var_k' . $index . ', custom_var_v' . $index;
- }
+ // No custom var were found in the request, so let's copy the previous one in a potential conversion later
+ $numCustomVarsToRead = CustomVariables::getMaxCustomVariables();
}
- $persistedVisitAttributes = self::getVisitFieldsPersist();
- array_unshift($persistedVisitAttributes, 'visit_first_action_time');
- array_unshift($persistedVisitAttributes, 'visit_last_action_time');
- $persistedVisitAttributes = array_unique($persistedVisitAttributes);
-
- $selectFields = implode(", ", $persistedVisitAttributes);
-
- $select = "SELECT
- $selectFields
- $selectCustomVariables
- ";
- $from = "FROM " . Common::prefixTable('log_visit');
-
+ $persistedVisitAttributes = $this->getVisitFieldsPersist();
+ $shouldMatchOneFieldOnly = $this->shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup);
list($timeLookBack, $timeLookAhead) = $this->getWindowLookupThisVisit();
- $shouldMatchOneFieldOnly = $this->shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup);
-
- // Two use cases:
- // 1) there is no visitor ID so we try to match only on config_id (heuristics)
- // Possible causes of no visitor ID: no browser cookie support, direct Tracking API request without visitor ID passed,
- // importing server access logs with import_logs.py, etc.
- // In this case we use config_id heuristics to try find the visitor in tahhhe past. There is a risk to assign
- // this page view to the wrong visitor, but this is better than creating artificial visits.
- // 2) there is a visitor ID and we trust it (config setting trust_visitors_cookies, OR it was set using &cid= in tracking API),
- // and in these cases, we force to look up this visitor id
- $whereCommon = "visit_last_action_time >= ? AND visit_last_action_time <= ? AND idsite = ?";
- $bindSql = array(
- $timeLookBack,
- $timeLookAhead,
- $this->request->getIdSite()
- );
-
- if ($shouldMatchOneFieldOnly) {
- if ($isVisitorIdToLookup) {
- $whereCommon .= ' AND idvisitor = ?';
- $bindSql[] = $this->visitorInfo['idvisitor'];
- } else {
- $whereCommon .= ' AND config_id = ?';
- $bindSql[] = $configId;
- }
-
- $sql = "$select
- $from
- WHERE " . $whereCommon . "
- ORDER BY visit_last_action_time DESC
- LIMIT 1";
- } // We have a config_id AND a visitor_id. We match on either of these.
- // Why do we also match on config_id?
- // we do not trust the visitor ID only. Indeed, some browsers, or browser addons,
- // cause the visitor id from the 1st party cookie to be different on each page view!
- // It is not acceptable to create a new visit every time such browser does a page view,
- // so we also backup by searching for matching config_id.
- // We use a UNION here so that each sql query uses its own INDEX
- else {
- // will use INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time)
- $where = ' AND config_id = ?
- AND user_id IS NULL ';
- $bindSql[] = $configId;
- $sqlConfigId = "$select ,
- 0 as priority
- $from
- WHERE $whereCommon $where
- ORDER BY visit_last_action_time DESC
- LIMIT 1
- ";
- // will use INDEX index_idsite_idvisitor (idsite, idvisitor)
- $bindSql[] = $timeLookBack;
- $bindSql[] = $timeLookAhead;
- $bindSql[] = $this->request->getIdSite();
- $where = ' AND idvisitor = ?';
- $bindSql[] = $this->visitorInfo['idvisitor'];
- $sqlVisitorId = "$select ,
- 1 as priority
- $from
- WHERE $whereCommon $where
- ORDER BY visit_last_action_time DESC
- LIMIT 1
- ";
-
- // We join both queries and favor the one matching the visitor_id if it did match
- $sql = " ( $sqlConfigId )
- UNION
- ( $sqlVisitorId )
- ORDER BY priority DESC
- LIMIT 1";
- }
-
- $visitRow = Tracker::getDatabase()->fetch($sql, $bindSql);
+ $model = $this->getModel();
+ $visitRow = $model->findVisitor($idSite, $configId, $idVisitor, $persistedVisitAttributes, $numCustomVarsToRead, $shouldMatchOneFieldOnly, $isVisitorIdToLookup, $timeLookBack, $timeLookAhead);
$isNewVisitForced = $this->request->getParam('new_visit');
$isNewVisitForced = !empty($isNewVisitForced);
- $enforceNewVisit = $isNewVisitForced || Config::getInstance()->Debug['tracker_always_new_visitor'];
+ $enforceNewVisit = $isNewVisitForced || Config::getInstance()->Debug['tracker_always_new_visitor'];
if (!$enforceNewVisit
&& $visitRow
@@ -162,17 +77,16 @@ class Visitor
) {
// These values will be used throughout the request
- foreach($persistedVisitAttributes as $field) {
+ foreach ($persistedVisitAttributes as $field) {
$this->visitorInfo[$field] = $visitRow[$field];
}
- $this->visitorInfo['visit_last_action_time'] = strtotime($visitRow['visit_last_action_time']);
+ $this->visitorInfo['visit_last_action_time'] = strtotime($visitRow['visit_last_action_time']);
$this->visitorInfo['visit_first_action_time'] = strtotime($visitRow['visit_first_action_time']);
// Custom Variables copied from Visit in potential later conversion
- if (!empty($selectCustomVariables)) {
- $maxCustomVariables = CustomVariables::getMaxCustomVariables();
- for ($i = 1; $i <= $maxCustomVariables; $i++) {
+ if (!empty($numCustomVarsToRead)) {
+ for ($i = 1; $i <= $numCustomVarsToRead; $i++) {
if (isset($visitRow['custom_var_k' . $i])
&& strlen($visitRow['custom_var_k' . $i])
) {
@@ -193,7 +107,6 @@ class Visitor
last action = " . date("r", $this->visitorInfo['visit_last_action_time']) . ",
first action = " . date("r", $this->visitorInfo['visit_first_action_time']) . ",
visit_goal_buyer' = " . $this->visitorInfo['visit_goal_buyer'] . ")");
- //Common::printDebug($this->visitorInfo);
} else {
Common::printDebug("The visitor was not matched with an existing visitor...");
}
@@ -228,7 +141,8 @@ class Visitor
protected function shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup)
{
$isForcedUserIdMustMatch = (false !== $this->request->getForcedUserId());
- if($isForcedUserIdMustMatch) {
+
+ if ($isForcedUserIdMustMatch) {
// if &iud was set, we must try and match both idvisitor and config_id
return false;
}
@@ -236,18 +150,18 @@ class Visitor
// This setting would be enabled for Intranet websites, to ensure that visitors using all the same computer config, same IP
// are not counted as 1 visitor. In this case, we want to enforce and trust the visitor ID from the cookie.
$trustCookiesOnly = Config::getInstance()->Tracker['trust_visitors_cookies'];
- if($isVisitorIdToLookup && $trustCookiesOnly) {
+ if ($isVisitorIdToLookup && $trustCookiesOnly) {
return true;
}
// If a &cid= was set, we force to select this visitor (or create a new one)
$isForcedVisitorIdMustMatch = ($this->request->getForcedVisitorId() != null);
- if($isForcedVisitorIdMustMatch) {
+ if ($isForcedVisitorIdMustMatch) {
return true;
}
- if( !$isVisitorIdToLookup ) {
+ if (!$isVisitorIdToLookup ) {
return true;
}
@@ -257,7 +171,7 @@ class Visitor
/**
* @return array
*/
- public static function getVisitFieldsPersist()
+ private function getVisitFieldsPersist()
{
$fields = array(
'idvisitor',
@@ -307,6 +221,10 @@ class Visitor
*/
Piwik::postEvent('Tracker.getVisitFieldsToPersist', array(&$fields));
+ array_unshift($fields, 'visit_first_action_time');
+ array_unshift($fields, 'visit_last_action_time');
+ $fields = array_unique($fields);
+
return $fields;
}
@@ -343,4 +261,9 @@ class Visitor
{
return $this->visitorKnown = $isVisitorKnown;
}
+
+ private function getModel()
+ {
+ return new Model();
+ }
}