示例#1
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return mixed
  */
 public function onNewVisit(Request $request, Visitor $visitor, $action)
 {
     // Adding &dp=1 will disable the provider plugin, this is an "unofficial" parameter used to speed up log importer
     $disableProvider = $request->getParam('dp');
     if (!empty($disableProvider)) {
         return false;
     }
     // if provider info has already been set, abort
     $locationValue = $visitor->getVisitorColumn('location_provider');
     if (!empty($locationValue)) {
         return false;
     }
     $ip = $visitor->getVisitorColumn('location_ip');
     $privacyConfig = new PrivacyManagerConfig();
     if (!$privacyConfig->useAnonymizedIpForVisitEnrichment) {
         $ip = $request->getIp();
     }
     $ip = IPUtils::binaryToStringIP($ip);
     // In case the IP was anonymized, we should not continue since the DNS reverse lookup will fail and this will slow down tracking
     if (substr($ip, -2, 2) == '.0') {
         Common::printDebug("IP Was anonymized so we skip the Provider DNS reverse lookup...");
         return false;
     }
     $hostname = $this->getHost($ip);
     $hostnameExtension = ProviderPlugin::getCleanHostname($hostname);
     // add the provider value in the table log_visit
     $locationProvider = substr($hostnameExtension, 0, 100);
     return $locationProvider;
 }
示例#2
0
 protected function getUserInfo(Request $request, Visitor $visitor)
 {
     $ipAddress = $this->getIpAddress($visitor->getVisitorColumn('location_ip'), $request);
     $language = $visitor->getVisitorColumn('location_browser_lang');
     $userInfo = array('lang' => $language, 'ip' => $ipAddress);
     return $userInfo;
 }
 public function onNewAction(Request $request, Visitor $visitor, Action $action)
 {
     $timeSpent = $visitor->getVisitorColumn('time_spent_ref_action');
     if (empty($timeSpent)) {
         return 0;
     }
     return $timeSpent;
 }
示例#4
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return int
  */
 public function onExistingVisit(Request $request, Visitor $visitor, $action)
 {
     $request->setMetadata('Actions', $this->columnName, $visitor->getVisitorColumn($this->columnName));
     if (self::shouldCountInteraction($action)) {
         return $this->columnName . ' + 1';
     }
     return false;
 }
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return int
  */
 public function onExistingVisit(Request $request, Visitor $visitor, $action)
 {
     $goalBuyer = $visitor->getVisitorColumn($this->columnName);
     // Ecommerce buyer status
     $visitEcommerceStatus = $this->getBuyerType($request, $goalBuyer);
     if ($visitEcommerceStatus != self::TYPE_BUYER_NONE && $visitEcommerceStatus != $goalBuyer) {
         return $visitEcommerceStatus;
     }
     return false;
 }
示例#6
0
 public function onExistingVisit(Request $request, Visitor $visitor, $action)
 {
     $idAction = $visitor->getVisitorColumn('visit_entry_idaction_url');
     if (is_null($idAction) && !empty($action)) {
         $idAction = $action->getIdActionUrlForEntryAndExitIds();
         if (!empty($idAction)) {
             return $idAction;
         }
     }
     return false;
 }
示例#7
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action $action
  *
  * @return mixed|false
  */
 public function onNewAction(Request $request, Visitor $visitor, Action $action)
 {
     $shouldCount = VisitTotalInteractions::shouldCountInteraction($action);
     if ($shouldCount && $visitor->isNewVisit()) {
         return 1;
     } else {
         if ($shouldCount) {
             return VisitTotalInteractions::getCurrentInteractionPosition($request);
         }
     }
     return false;
 }
 /**
  *
  * @param Visitor $visitor            
  * @param Request $request            
  *
  * @return string
  */
 private function getIpAddress(Visitor $visitor, Request $request)
 {
     if ($this->ipToUse !== null) {
         return $this->ipToUse;
     }
     $privacyConfig = new PrivacyManagerConfig();
     $ip = $request->getIp();
     if ($privacyConfig->useAnonymizedIpForVisitEnrichment) {
         $ip = $visitor->getVisitorColumn('location_ip');
     }
     $ip = IPUtils::binaryToStringIP($ip);
     $this->ipToUse = $ip;
     return $ip;
 }
示例#9
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return int
  */
 public function onConvertedVisit(Request $request, Visitor $visitor, $action)
 {
     if (!$visitor->isVisitorKnown()) {
         return false;
     }
     $totalTime = $visitor->getVisitorColumn('visit_total_time');
     // If a pageview and goal conversion in the same second, with previously a goal conversion recorded
     // the request would not "update" the row since all values are the same as previous
     // therefore the request below throws exception, instead we make sure the UPDATE will affect the row
     $totalTime = $totalTime + $request->getParam('idgoal');
     // +2 to offset idgoal=-1 and idgoal=0
     $totalTime = $totalTime + 2;
     return $this->cleanupVisitTotalTime($totalTime);
 }
 public function recordLogs(VisitProperties $visitProperties, Request $request)
 {
     /** @var Action $action */
     $action = $request->getMetadata('Actions', 'action');
     if ($action !== null && !$request->getMetadata('CoreHome', 'visitorNotFoundInDb')) {
         $idReferrerActionUrl = 0;
         $idReferrerActionName = 0;
         if (!$request->getMetadata('CoreHome', 'isNewVisit')) {
             $idReferrerActionUrl = $request->getMetadata('Actions', 'idReferrerActionUrl');
             $idReferrerActionName = $request->getMetadata('Actions', 'idReferrerActionName');
         }
         $visitor = Visitor::makeFromVisitProperties($visitProperties, $request);
         $action->record($visitor, $idReferrerActionUrl, $idReferrerActionName);
     }
 }
示例#11
0
 /**
  * This event is triggered when an any custom goal is converted. In this example we would store a the id of the
  * goal in the 'example_conversion_dimension' column if the visitor is known and nothing otherwise.
  * Return boolean false if you do not want to change the value in some cases. If you do not want to perform any
  * action on an ecommerce order at all it is recommended to just remove this method.
  *
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @param GoalManager $goalManager
  *
  * @return mixed|false
  */
 public function onGoalConversion(Request $request, Visitor $visitor, $action, GoalManager $goalManager)
 {
     $goalId = $goalManager->getGoalColumn('idgoal');
     if ($visitor->isVisitorKnown()) {
         return $goalId;
     }
     return false;
 }
 public function onExistingVisit(Request $request, Visitor $visitor, $action)
 {
     return $visitor->getVisitorColumn('custom_dimension_3') . 'extended';
 }
示例#13
0
 private function getGoalFromVisitor(VisitProperties $visitProperties, Request $request, $action)
 {
     $goal = array('idvisit' => $visitProperties->getProperty('idvisit'), 'idvisitor' => $visitProperties->getProperty('idvisitor'), 'server_time' => Date::getDatetimeFromTimestamp($visitProperties->getProperty('visit_last_action_time')));
     $visitDimensions = VisitDimension::getAllDimensions();
     $visit = Visitor::makeFromVisitProperties($visitProperties, $request);
     foreach ($visitDimensions as $dimension) {
         $value = $dimension->onAnyGoalConversion($request, $visit, $action);
         if (false !== $value) {
             $goal[$dimension->getColumnName()] = $value;
         }
     }
     return $goal;
 }
示例#14
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()
     $ip = $this->request->getIp();
     $this->visitorInfo['location_ip'] = $ip;
     $excluded = new VisitExcluded($this->request, $ip);
     if ($excluded->isExcluded()) {
         return;
     }
     /**
      * Triggered after visits are tested for exclusion so plugins can modify the IP address
      * persisted with a visit.
      * 
      * This event is primarily used by the **PrivacyManager** plugin to anonymize IP addresses.
      * 
      * @param string &$ip The visitor's IP address.
      */
     Piwik::postEvent('Tracker.setVisitorIp', array(&$this->visitorInfo['location_ip']));
     $this->visitorCustomVariables = $this->request->getCustomVariables($scope = 'visit');
     if (!empty($this->visitorCustomVariables)) {
         Common::printDebug("Visit level Custom Variables: ");
         Common::printDebug($this->visitorCustomVariables);
     }
     $this->goalManager = new GoalManager($this->request);
     $visitIsConverted = false;
     $action = null;
     $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->request->getIdSite());
         $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) {
             throw new \Exception('Invalid goal tracking request for goal id = ' . $this->goalManager->idGoal);
         }
     } else {
         $action = Action::factory($this->request);
         $action->writeDebugInfo();
         $someGoalsConverted = $this->goalManager->detectGoalsMatchingUrl($this->request->getIdSite(), $action);
         $visitIsConverted = $someGoalsConverted;
         $action->loadIdsFromLogActionTable();
     }
     /***
      * Visitor recognition
      */
     $visitor = new Visitor($this->request, $this->getSettingsObject(), $this->visitorInfo, $this->visitorCustomVariables);
     $visitor->recognize();
     $this->visitorKnown = $visitor->isVisitorKnown();
     $this->visitorInfo = $visitor->getVisitorInfo();
     $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit();
     if (!$isLastActionInTheSameVisit) {
         Common::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) {
         $idReferrerActionUrl = $this->visitorInfo['visit_exit_idaction_url'];
         $idReferrerActionName = $this->visitorInfo['visit_exit_idaction_name'];
         try {
             $this->handleExistingVisit($action, $visitIsConverted);
             if (!is_null($action)) {
                 $action->record($this->visitorInfo['idvisit'], $this->visitorInfo['idvisitor'], $idReferrerActionUrl, $idReferrerActionName, $this->visitorInfo['time_spent_ref_action']);
             }
         } catch (VisitorNotFoundInDb $e) {
             // There is an edge case when:
             // - two manual goal conversions happen in the same second
             // - which result in handleExistingVisit 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($action, $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->request->setThirdPartyCookie($this->visitorInfo['idvisitor']);
     // record the goals if applicable
     if ($someGoalsConverted) {
         $this->goalManager->recordGoals($this->request->getIdSite(), $this->visitorInfo, $this->visitorCustomVariables, $action);
     }
     unset($this->goalManager);
     unset($action);
 }
示例#15
0
文件: Country.php 项目: piwik/piwik
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return mixed
  */
 public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
 {
     $country = $visitor->getVisitorColumn($this->columnName);
     if (isset($country) && false !== $country) {
         return $country;
     }
     $browserLanguage = $request->getBrowserLanguage();
     $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess'];
     $locationIp = $visitor->getVisitorColumn('location_ip');
     $country = Common::getCountry($browserLanguage, $enableLanguageToCountryGuess, $locationIp);
     return $country;
 }
示例#16
0
 private function makeVisitorFacade()
 {
     return Visitor::makeFromVisitProperties($this->visitProperties, $this->request);
 }
示例#17
0
 private function setVisitorColumn(Visitor $visitor, $key, $value)
 {
     $this->visitorInfo[$key] = $value;
     $visitor->setVisitorColumn($key, $value);
 }
示例#18
0
文件: Base.php 项目: diosmosis/piwik
 protected function doesLastActionHaveSameReferrer(Visitor $visitor, $referrerType)
 {
     return $visitor->getVisitorColumn('referer_type') == $referrerType;
 }
示例#19
0
 public function popout()
 {
     header("Access-Control-Allow-Origin: *");
     $params = UrlHelper::getArrayFromQueryString($_SERVER['QUERY_STRING']);
     $request = new Tracker\Request($params);
     // the IP is needed by isExcluded() and GoalManager->recordGoals()
     $ip = $request->getIp();
     $visitorInfo['location_ip'] = $ip;
     /**
      * Triggered after visits are tested for exclusion so plugins can modify the IP address
      * persisted with a visit.
      *
      * This event is primarily used by the **PrivacyManager** plugin to anonymize IP addresses.
      *
      * @param string &$ip The visitor's IP address.
      */
     Piwik::postEvent('Tracker.setVisitorIp', array(&$visitorInfo['location_ip']));
     /***
      * Visitor recognition
      */
     $settings = new Tracker\Settings($request, $visitorInfo['location_ip']);
     $visitor = new Visitor($request, $settings->getConfigId(), $visitorInfo);
     $visitor->recognize();
     $visitorInfo = $visitor->getVisitorInfo();
     if (!isset($visitorInfo['location_browser_lang'])) {
         return "Who are you ?";
     }
     $idSite = Common::getRequestVar('idsite', null, 'int');
     $conversation = new ChatConversation($idSite, bin2hex($visitorInfo['idvisitor']));
     /***
      * Segment recognition
      */
     foreach (ChatAutomaticMessage::getAll($idSite) as $autoMsg) {
         $segment = ChatSegment::get($autoMsg['segmentID']);
         $fetchSegment = new Segment($segment['definition'], array($idSite));
         $query = $fetchSegment->getSelectQuery("idvisitor", "log_visit", "log_visit.idvisitor = ?", array($visitorInfo['idvisitor']));
         $rows = Db::fetchAll($query['sql'], $query['bind']);
         if (count($rows) == 0) {
             continue;
         }
         if ($autoMsg['segmentID'] != $segment['idsegment']) {
             continue;
         }
         $getAlreadyReceivedMsg = $conversation->getAutomaticMessageReceivedById($autoMsg['id']);
         if (count($getAlreadyReceivedMsg) > 0) {
             // If the AutoMsg is a "one shot"
             if ($autoMsg['frequency'] == 0) {
                 continue;
             }
             if ($autoMsg['frequency'] != 0) {
                 // Now, we gonna try to define when the last AutoMsg received has been sent
                 list($freqTime, $freqScale) = explode('|', $autoMsg['frequency']);
                 if ($freqScale == "w") {
                     $dayMultiplier = 7;
                 } elseif ($freqScale == "m") {
                     $dayMultiplier = 30;
                 } else {
                     $dayMultiplier = 1;
                 }
                 $secToWait = 3600 * 24 * $freqTime * $dayMultiplier;
                 // Is it older than the time range needed to wait ?
                 if ($getAlreadyReceivedMsg[0]['microtime'] + $secToWait > microtime(true)) {
                     continue;
                 }
             }
         }
         $conversation->sendMessage($autoMsg['message'], $autoMsg['transmitter'], $autoMsg['id']);
     }
     $view = new View('@Chat/popout.twig');
     $view->idvisitor = bin2hex($visitorInfo['idvisitor']);
     $view->idsite = $idSite;
     $view->timeLimit = time() - 2 * 60 * 60;
     $view->isStaffOnline = ChatPiwikUser::isStaffOnline();
     $view->siteUrl = ChatSite::getMainUrl($idSite);
     $view->lang = $visitorInfo['location_browser_lang'];
     return $view->render();
 }
示例#20
0
 /**
  * Records in the DB the association between the visit and this action.
  *
  * @param int $idReferrerActionUrl is the ID of the last action done by the current visit.
  * @param $idReferrerActionName
  * @param Visitor $visitor
  */
 public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerActionName)
 {
     $this->loadIdsFromLogActionTable();
     $visitAction = array('idvisit' => $visitor->getVisitorColumn('idvisit'), 'idsite' => $this->request->getIdSite(), 'idvisitor' => $visitor->getVisitorColumn('idvisitor'), 'idaction_url' => $this->getIdActionUrl(), 'idaction_url_ref' => $idReferrerActionUrl, 'idaction_name_ref' => $idReferrerActionName);
     $dimensions = ActionDimension::getAllDimensions();
     foreach ($dimensions as $dimension) {
         $value = $dimension->onNewAction($this->request, $visitor, $this);
         if ($value !== false) {
             $visitAction[$dimension->getColumnName()] = $value;
         }
     }
     // idaction_name is NULLable. we only set it when applicable
     if ($this->isActionHasActionName()) {
         $visitAction['idaction_name'] = (int) $this->getIdActionName();
     }
     foreach ($this->actionIdsCached as $field => $idAction) {
         $visitAction[$field] = $idAction === false ? 0 : $idAction;
     }
     $customValue = $this->getCustomFloatValue();
     if (!empty($customValue)) {
         $visitAction[self::DB_COLUMN_CUSTOM_FLOAT] = $customValue;
     }
     $customVariables = $this->getCustomVariables();
     if (!empty($customVariables)) {
         Common::printDebug("Page level Custom Variables: ");
         Common::printDebug($customVariables);
     }
     $visitAction = array_merge($visitAction, $customVariables);
     $fields = implode(", ", array_keys($visitAction));
     $bind = array_values($visitAction);
     $values = Common::getSqlStringFieldsArray($visitAction);
     $sql = "INSERT INTO " . Common::prefixTable('log_link_visit_action') . " ({$fields}) VALUES ({$values})";
     Tracker::getDatabase()->query($sql, $bind);
     $this->idLinkVisitAction = Tracker::getDatabase()->lastInsertId();
     $visitAction['idlink_va'] = $this->idLinkVisitAction;
     Common::printDebug("Inserted new action:");
     Common::printDebug($visitAction);
     /**
      * Triggered after successfully persisting a [visit action entity](/guides/persistence-and-the-mysql-backend#visit-actions).
      *
      * @param Action $tracker Action The Action tracker instance.
      * @param array $visitAction The visit action entity that was persisted. Read
      *                           [this](/guides/persistence-and-the-mysql-backend#visit-actions) to see what it contains.
      */
     Piwik::postEvent('Tracker.recordAction', array($trackerAction = $this, $visitAction));
 }
示例#21
0
 public function test_handleExistingVisitWithConversion_shouldTriggerDimensions()
 {
     $request = new Request(array());
     $visitProperties = new Visit\VisitProperties();
     $visitor = new Visitor($visitProperties);
     $visit = new FakeTrackerVisit($request, $visitProperties);
     $visit->handleNewVisit(false);
     $visit->handleExistingVisit(true);
     $info = $visit->getVisitorInfo();
     $this->assertEquals('existing1', $info['custom_dimension_1']);
     $this->assertEquals('onNew2', $info['custom_dimension_2']);
     // on converted visit returns false and should be ignored
     $this->assertEquals('converted3', $info['custom_dimension_3']);
     // a conversion should overwrite an existing value
     $this->assertEquals('existing3extended', $info['custom_dimension_4']);
     $this->assertEquals('converted3', $visitor->getVisitorColumn('custom_dimension_3'));
 }
示例#22
0
 /**
  * Records in the DB the association between the visit and this action.
  *
  * @param int $idReferrerActionUrl is the ID of the last action done by the current visit.
  * @param $idReferrerActionName
  * @param Visitor $visitor
  */
 public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerActionName)
 {
     $this->loadIdsFromLogActionTable();
     $visitAction = array('idvisit' => $visitor->getVisitorColumn('idvisit'), 'idsite' => $this->request->getIdSite(), 'idvisitor' => $visitor->getVisitorColumn('idvisitor'), 'idaction_url' => $this->getIdActionUrl(), 'idaction_url_ref' => $idReferrerActionUrl, 'idaction_name_ref' => $idReferrerActionName);
     /** @var ActionDimension[] $dimensions */
     $dimensions = ActionDimension::getAllDimensions();
     foreach ($dimensions as $dimension) {
         $value = $dimension->onNewAction($this->request, $visitor, $this);
         if ($value !== false) {
             if (is_float($value)) {
                 $value = Common::forceDotAsSeparatorForDecimalPoint($value);
             }
             $visitAction[$dimension->getColumnName()] = $value;
         }
     }
     // idaction_name is NULLable. we only set it when applicable
     if ($this->isActionHasActionName()) {
         $visitAction['idaction_name'] = (int) $this->getIdActionName();
     }
     foreach ($this->actionIdsCached as $field => $idAction) {
         $visitAction[$field] = $idAction === false ? 0 : $idAction;
     }
     $customValue = $this->getCustomFloatValue();
     if (!empty($customValue)) {
         $visitAction[self::DB_COLUMN_CUSTOM_FLOAT] = Common::forceDotAsSeparatorForDecimalPoint($customValue);
     }
     $visitAction = array_merge($visitAction, $this->customFields);
     $this->idLinkVisitAction = $this->getModel()->createAction($visitAction);
     $visitAction['idlink_va'] = $this->idLinkVisitAction;
     Common::printDebug("Inserted new action:");
     $visitActionDebug = $visitAction;
     $visitActionDebug['idvisitor'] = bin2hex($visitActionDebug['idvisitor']);
     Common::printDebug($visitActionDebug);
     /**
      * Triggered after successfully persisting a [visit action entity](/guides/persistence-and-the-mysql-backend#visit-actions).
      *
      * This event is deprecated, use [Dimensions](http://developer.piwik.org/guides/dimensions) instead.
      *
      * @param Action $tracker Action The Action tracker instance.
      * @param array $visitAction The visit action entity that was persisted. Read
      *                           [this](/guides/persistence-and-the-mysql-backend#visit-actions) to see what it contains.
      * @deprecated
      */
     Piwik::postEvent('Tracker.recordAction', array($trackerAction = $this, $visitAction));
 }
示例#23
0
文件: Action.php 项目: piwik/piwik
 /**
  * Records in the DB the association between the visit and this action.
  *
  * @param int $idReferrerActionUrl is the ID of the last action done by the current visit.
  * @param $idReferrerActionName
  * @param Visitor $visitor
  */
 public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerActionName)
 {
     $this->loadIdsFromLogActionTable();
     $visitAction = array('idvisit' => $visitor->getVisitorColumn('idvisit'), 'idsite' => $this->request->getIdSite(), 'idvisitor' => $visitor->getVisitorColumn('idvisitor'), 'idaction_url' => $this->getIdActionUrl(), 'idaction_url_ref' => $idReferrerActionUrl, 'idaction_name_ref' => $idReferrerActionName);
     /** @var ActionDimension[] $dimensions */
     $dimensions = ActionDimension::getAllDimensions();
     foreach ($dimensions as $dimension) {
         $value = $dimension->onNewAction($this->request, $visitor, $this);
         if ($value !== false) {
             if (is_float($value)) {
                 $value = Common::forceDotAsSeparatorForDecimalPoint($value);
             }
             $visitAction[$dimension->getColumnName()] = $value;
         }
     }
     // idaction_name is NULLable. we only set it when applicable
     if ($this->isActionHasActionName()) {
         $visitAction['idaction_name'] = (int) $this->getIdActionName();
     }
     foreach ($this->actionIdsCached as $field => $idAction) {
         $visitAction[$field] = $idAction === false ? 0 : $idAction;
     }
     $customValue = $this->getCustomFloatValue();
     if (!empty($customValue)) {
         $visitAction[self::DB_COLUMN_CUSTOM_FLOAT] = Common::forceDotAsSeparatorForDecimalPoint($customValue);
     }
     $visitAction = array_merge($visitAction, $this->customFields);
     $this->idLinkVisitAction = $this->getModel()->createAction($visitAction);
     $visitAction['idlink_va'] = $this->idLinkVisitAction;
     Common::printDebug("Inserted new action:");
     $visitActionDebug = $visitAction;
     $visitActionDebug['idvisitor'] = bin2hex($visitActionDebug['idvisitor']);
     Common::printDebug($visitActionDebug);
 }
 /**
  * This event is executed shortly after "onNewVisit" or "onExistingVisit" in case the visitor converted a goal.
  * In this example we give the user 5 extra points for this achievement. Usually this event is not needed and you
  * can simply remove this method therefore. An example would be for instance to persist the last converted
  * action url. Return boolean false if you do not want to change the current value.
  *
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  *
  * @return mixed|false
  */
 public function onConvertedVisit(Request $request, Visitor $visitor, $action)
 {
     return $visitor->getVisitorColumn($this->columnName) + 5;
     // give this visitor 5 extra achievement points
 }
示例#25
0
文件: Visit.php 项目: hichnik/piwik
 /**
  * Determines if the tracker if the current action should be treated as the start of a new visit or
  * an action in an existing visit.
  *
  * @param Visitor $visitor The current visit/visitor information.
  * @param Action|null $action The current action being tracked.
  * @return bool
  */
 public function isVisitNew(Visitor $visitor, Action $action = null)
 {
     if (!$visitor->isVisitorKnown()) {
         return true;
     }
     $isLastActionInTheSameVisit = $this->isLastActionInTheSameVisit($visitor);
     if (!$isLastActionInTheSameVisit) {
         Common::printDebug("Visitor detected, but last action was more than 30 minutes ago...");
         return true;
     }
     $wasLastActionYesterday = $this->wasLastActionNotToday($visitor);
     if ($wasLastActionYesterday) {
         Common::printDebug("Visitor detected, but last action was yesterday...");
         return true;
     }
     $shouldForceNewVisit = $this->triggerPredicateHookOnDimensions($this->getAllVisitDimensions(), 'shouldForceNewVisit', $visitor, $action);
     if ($shouldForceNewVisit) {
         return true;
     }
     return false;
 }
示例#26
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return mixed
  */
 public function getValueForRecordGoal(Request $request, Visitor $visitor)
 {
     $referrerTimestamp = $request->getParam('_refts');
     $referrerUrl = $request->getParam('_ref');
     $referrerCampaignName = trim(urldecode($request->getParam('_rcn')));
     $referrerCampaignKeyword = trim(urldecode($request->getParam('_rck')));
     // Attributing the correct Referrer to this conversion.
     // Priority order is as follows:
     // 0) In some cases, the campaign is not passed from the JS so we look it up from the current visit
     // 1) Campaign name/kwd parsed in the JS
     // 2) Referrer URL stored in the _ref cookie
     // 3) If no info from the cookie, attribute to the current visit referrer
     // 3) Default values: current referrer
     $type = $visitor->getVisitorColumn('referer_type');
     $name = $visitor->getVisitorColumn('referer_name');
     $keyword = $visitor->getVisitorColumn('referer_keyword');
     $time = $visitor->getVisitorColumn('visit_first_action_time');
     // 0) In some (unknown!?) cases the campaign is not found in the attribution cookie, but the URL ref was found.
     //    In this case we look up if the current visit is credited to a campaign and will credit this campaign rather than the URL ref (since campaigns have higher priority)
     if (empty($referrerCampaignName) && $type == Common::REFERRER_TYPE_CAMPAIGN && !empty($name)) {
         // Use default values per above
     } elseif (!empty($referrerCampaignName)) {
         $type = Common::REFERRER_TYPE_CAMPAIGN;
         $name = $referrerCampaignName;
         $keyword = $referrerCampaignKeyword;
         $time = $referrerTimestamp;
     } elseif (!empty($referrerUrl)) {
         $idSite = $request->getIdSite();
         $referrer = $this->getReferrerInformation($referrerUrl, $currentUrl = '', $idSite);
         // if the parsed referrer is interesting enough, ie. website or search engine
         if (in_array($referrer['referer_type'], array(Common::REFERRER_TYPE_SEARCH_ENGINE, Common::REFERRER_TYPE_WEBSITE))) {
             $type = $referrer['referer_type'];
             $name = $referrer['referer_name'];
             $keyword = $referrer['referer_keyword'];
             $time = $referrerTimestamp;
         }
     }
     $this->setCampaignValuesToLowercase($type, $name, $keyword);
     $fields = array('referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword);
     if (array_key_exists($this->columnName, $fields)) {
         return $fields[$this->columnName];
     }
     return false;
 }
示例#27
0
 /**
  * Gather fields=>values that needs to be updated for the existing visit in log_visit
  *
  * @param Visitor $visitor
  * @param Action|null $action
  * @param $visitIsConverted
  * @return array
  */
 protected function getExistingVisitFieldsToUpdate($visitor, $action, $visitIsConverted)
 {
     $valuesToUpdate = array();
     // 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 = $this->getAllVisitDimensions();
     $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onExistingVisit', $visitor, $action, $valuesToUpdate);
     if ($visitIsConverted) {
         $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onConvertedVisit', $visitor, $action, $valuesToUpdate);
     }
     // Custom Variables overwrite previous values on each page view
     $valuesToUpdate = array_merge($valuesToUpdate, $this->visitorCustomVariables);
     return $valuesToUpdate;
 }
示例#28
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return mixed
  */
 public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
 {
     return $visitor->getVisitorColumn($this->columnName);
 }
 private function makeVisitorAndAction($lastActionTimestamp, $currentActionTime, $isVisitorKnown = false)
 {
     $idsite = API::getInstance()->addSite("name", "http://piwik.net/");
     list($visit, $request) = $this->prepareVisitWithRequest(array('idsite' => $idsite), $currentActionTime);
     $visitor = new Visitor($request, 'configid', array('visit_last_action_time' => Date::factory($lastActionTimestamp)->getTimestamp()));
     $visitor->setIsVisitorKnown($isVisitorKnown);
     $action = new ActionPageview($request);
     return array($visit, $visitor, $action);
 }