/** * Main algorith to handle the visit. * * Once we have the visitor information, we have to define if the visit is a new or a known visit. * * 1) When the last action was done more than 30min ago, * or if the visitor is new, then this is a new visit. * * 2) If the last action is less than 30min ago, then the same visit is going on. * Because the visit goes on, we can get the time spent during the last action. * * NB: * - In the case of a new visit, then the time spent * during the last action of the previous visit is unknown. * * - In the case of a new visit but with a known visitor, * we can set the 'returning visitor' flag. * * In all the cases we set a cookie to the visitor with the new information. */ public function handle() { if ($this->isExcluded()) { return; } $goalManager = new Piwik_Tracker_GoalManager(); $someGoalsConverted = false; $actionId = 0; $action = null; $idGoal = Piwik_Common::getRequestVar('idgoal', 0, 'int', $this->request); // this request is from the JS call to piwikTracker.trackGoal() if ($idGoal > 0) { $someGoalsConverted = $goalManager->detectGoalId($this->idsite, $idGoal, $this->request); // if we find a idgoal in the URL, but then the goal is not valid, this is most likely a fake request if (!$someGoalsConverted) { return; } } else { $action = $this->newAction(); $this->handleAction($action); $someGoalsConverted = $goalManager->detectGoalsMatchingUrl($this->idsite, $action); $actionId = $action->getIdAction(); } // the visitor and session $this->recognizeTheVisitor(); $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit(); // Known visit when: // - the visitor has the Piwik cookie with the idcookie ID used by Piwik to match the visitor // OR // - the visitor doesn't have the Piwik cookie but could be match using heuristics @see recognizeTheVisitor() // AND // - the last page view for this visitor was less than 30 minutes ago @see isLastActionInTheSameVisit() if ($this->isVisitorKnown() && $isLastActionInTheSameVisit) { $idActionReferer = $this->visitorInfo['visit_exit_idaction']; try { $this->handleKnownVisit($actionId, $someGoalsConverted); if (!is_null($action)) { $action->record($this->visitorInfo['idvisit'], $idActionReferer, $this->visitorInfo['time_spent_ref_action']); } } catch (Piwik_Tracker_Visit_VisitorNotFoundInDatabase $e) { printDebug($e->getMessage()); $this->visitorKnown = false; } } // New visit when: // - the visitor has the Piwik cookie but the last action was performed more than 30 min ago @see isLastActionInTheSameVisit() // - 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 (!$this->isVisitorKnown() || !$isLastActionInTheSameVisit) { $this->handleNewVisit($actionId, $someGoalsConverted); if (empty($this->visitorInfo['idvisit'])) { $this->visitorInfo['idvisit'] = 0; } if (!is_null($action)) { $action->record($this->visitorInfo['idvisit'], 0, 0); } } // update the cookie with the new visit information $this->updateCookie(); // record the goals if applicable if ($someGoalsConverted) { $goalManager->setCookie($this->cookie); $goalManager->recordGoals($this->visitorInfo, $action); } unset($goalManager); unset($action); }
/** * Main algorith to handle the visit. * * Once we have the visitor information, we have to define if the visit is a new or a known visit. * * 1) When the last action was done more than 30min ago, * or if the visitor is new, then this is a new visit. * * 2) If the last action is less than 30min ago, then the same visit is going on. * Because the visit goes on, we can get the time spent during the last action. * * NB: * - In the case of a new visit, then the time spent * during the last action of the previous visit is unknown. * * - In the case of a new visit but with a known visitor, * we can set the 'returning visitor' flag. * * In all the cases we set a cookie to the visitor with the new information. */ public function handle() { if ($this->isExcluded()) { return; } $action = $this->newAction(); $action->setIdSite($this->idsite); $action->setRequest($this->request); $action->init(); if ($this->detectActionIsOutlinkOnAliasHost($action)) { printDebug("The outlink's URL host is one of the known host for this website. We don't record this click."); return; } $actionId = $action->getIdAction(); if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) { switch ($action->getActionType()) { case Piwik_Tracker_Action::TYPE_ACTION: $type = "normal page view"; break; case Piwik_Tracker_Action::TYPE_DOWNLOAD: $type = "download"; break; case Piwik_Tracker_Action::TYPE_OUTLINK: $type = "outlink"; break; } printDebug("Detected action <u>{$type}</u>, \n\t\t\t\t\t\tAction name: " . $action->getActionName() . ", \n\t\t\t\t\t\tAction URL = " . $action->getActionUrl()); } // goal matched? $goalManager = new Piwik_Tracker_GoalManager($action); $someGoalsConverted = false; if ($goalManager->detectGoals($this->idsite)) { $someGoalsConverted = true; } // the visitor and session $this->recognizeTheVisitor(); $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit(); // Known visit when: // - the visitor has the Piwik cookie with the idcookie ID used by Piwik to match the visitor // OR // - the visitor doesn't have the Piwik cookie but could be match using heuristics @see recognizeTheVisitor() // AND // - the last page view for this visitor was less than 30 minutes ago @see isLastActionInTheSameVisit() if ($this->isVisitorKnown() && $isLastActionInTheSameVisit) { $idActionReferer = $this->visitorInfo['visit_exit_idaction']; try { $this->handleKnownVisit($actionId, $someGoalsConverted); $action->record($this->visitorInfo['idvisit'], $idActionReferer, $this->visitorInfo['time_spent_ref_action']); } catch (Piwik_Tracker_Visit_VisitorNotFoundInDatabase $e) { printDebug($e->getMessage()); $this->visitorKnown = false; } } // New visit when: // - the visitor has the Piwik cookie but the last action was performed more than 30 min ago @see isLastActionInTheSameVisit() // - 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 (!$this->isVisitorKnown() || !$isLastActionInTheSameVisit) { $this->handleNewVisit($actionId, $someGoalsConverted); $action->record($this->visitorInfo['idvisit'], 0, 0); } // update the cookie with the new visit information $this->updateCookie(); // record the goals if applicable if ($someGoalsConverted) { $goalManager->setCookie($this->cookie); $goalManager->recordGoals($this->visitorInfo); } unset($goalManager); unset($action); }