Example #1
0
 /**
  *	Main algorithm to handle the visit.
  *
  *  Once we have the visitor information, we have to determine 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()
 {
     // the IP is needed by isExcluded() and GoalManager->recordGoals()
     $this->visitorInfo['location_ip'] = $this->ip;
     if ($this->isExcluded()) {
         return;
     }
     // Anonymize IP (after testing for IP exclusion)
     $ip = $this->ip;
     Piwik_PostEvent('Tracker.Visit.setVisitorIp', $ip);
     $this->visitorInfo['location_ip'] = $ip;
     $this->visitorCustomVariables = self::getCustomVariables($scope = 'visit', $this->request);
     if (!empty($this->visitorCustomVariables)) {
         $this->customVariablesSetFromRequest = true;
     }
     $this->goalManager = new Piwik_Tracker_GoalManager();
     $someGoalsConverted = $visitIsConverted = false;
     $idActionUrl = $idActionName = $actionType = false;
     $action = null;
     $this->goalManager->init($this->request);
     $requestIsManualGoalConversion = $this->goalManager->idGoal > 0;
     $requestIsEcommerce = $this->goalManager->requestIsEcommerce;
     if ($requestIsEcommerce) {
         $someGoalsConverted = true;
         // Mark the visit as Converted only if it is an order (not for a Cart update)
         if ($this->goalManager->isGoalAnOrder) {
             $visitIsConverted = true;
         }
     } elseif ($requestIsManualGoalConversion) {
         $someGoalsConverted = $this->goalManager->detectGoalId($this->idsite);
         $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) {
             printDebug('Invalid goal tracking request for goal id = ' . $this->goalManager->idGoal);
             unset($this->goalManager);
             return;
         }
     } else {
         $action = $this->newAction();
         $this->handleAction($action);
         $someGoalsConverted = $this->goalManager->detectGoalsMatchingUrl($this->idsite, $action);
         $visitIsConverted = $someGoalsConverted;
         $action->loadIdActionNameAndUrl();
         $idActionUrl = (int) $action->getIdActionUrl();
         $idActionName = (int) $action->getIdActionName();
         $actionType = $action->getActionType();
     }
     // the visitor and session
     $this->recognizeTheVisitor();
     $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit();
     if (!$isLastActionInTheSameVisit) {
         printDebug("Visitor detected, but last action was more than 30 minutes ago...");
     }
     // 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) {
         $idRefererActionUrl = $this->visitorInfo['visit_exit_idaction_url'];
         $idRefererActionName = $this->visitorInfo['visit_exit_idaction_name'];
         try {
             $this->handleKnownVisit($idActionUrl, $idActionName, $actionType, $visitIsConverted);
             if (!is_null($action)) {
                 $action->record($this->visitorInfo['idvisit'], $this->visitorInfo['idvisitor'], $idRefererActionUrl, $idRefererActionName, $this->visitorInfo['time_spent_ref_action']);
             }
         } catch (Piwik_Tracker_Visit_VisitorNotFoundInDatabase $e) {
             // There is an edge case when:
             // - two manual goal conversions happen in the same second
             // - which result in handleKnownVisit throwing the exception
             //   because the UPDATE didn't affect any rows (one row was found, but not updated since no field changed)
             // - the exception is caught here and will result in a new visit incorrectly
             // In this case, we cancel the current conversion to be recorded:
             if ($requestIsManualGoalConversion || $requestIsEcommerce) {
                 $someGoalsConverted = $visitIsConverted = false;
             } else {
                 $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($idActionUrl, $idActionName, $actionType, $visitIsConverted);
         if (!is_null($action)) {
             $action->record($this->visitorInfo['idvisit'], $this->visitorInfo['idvisitor'], 0, 0, 0);
         }
     }
     // update the cookie with the new visit information
     $this->setThirdPartyCookie();
     // record the goals if applicable
     if ($someGoalsConverted) {
         $refererTimestamp = Piwik_Common::getRequestVar('_refts', 0, 'int', $this->request);
         $refererUrl = Piwik_Common::getRequestVar('_ref', '', 'string', $this->request);
         $refererCampaignName = trim(urldecode(Piwik_Common::getRequestVar('_rcn', '', 'string', $this->request)));
         $refererCampaignKeyword = trim(urldecode(Piwik_Common::getRequestVar('_rck', '', 'string', $this->request)));
         $this->goalManager->recordGoals($this->idsite, $this->visitorInfo, $this->visitorCustomVariables, $action, $refererTimestamp, $refererUrl, $refererCampaignName, $refererCampaignKeyword, $this->getBrowserLanguage());
     }
     unset($this->goalManager);
     unset($action);
     $this->printCookie();
 }
Example #2
0
 /**
  *	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);
 }
Example #3
0
 /**
  *	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);
 }