/** * @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; }
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; }
/** * @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; }
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; }
/** * @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); }
/** * * @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; }
/** * @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; }
/** * 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 }
/** * Returns true if the last action was done during the last 30 minutes * @return bool */ protected function isLastActionInTheSameVisit(Visitor $visitor) { $lastActionTime = $visitor->getVisitorColumn('visit_last_action_time'); return isset($lastActionTime) && false !== $lastActionTime && $lastActionTime > $this->request->getCurrentTimestamp() - Config::getInstance()->Tracker['visit_standard_length']; }
/** * 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)); }
public function onExistingVisit(Request $request, Visitor $visitor, $action) { return $visitor->getVisitorColumn('custom_dimension_3') . 'extended'; }
/** * Returns true if the last action was not today. * @param Visitor $visitor * @return bool */ private function wasLastActionNotToday(Visitor $visitor) { $lastActionTime = $visitor->getVisitorColumn('visit_last_action_time'); if (empty($lastActionTime)) { return false; } $idSite = $this->request->getIdSite(); $timezone = $this->getTimezoneForSite($idSite); if (empty($timezone)) { throw new UnexpectedWebsiteFoundException('An unexpected website was found, check idSite in the request'); } $date = Date::factory((int) $lastActionTime, $timezone); $now = $this->request->getCurrentTimestamp(); $now = Date::factory((int) $now, $timezone); return $date->toString() !== $now->toString(); }
protected function doesLastActionHaveSameReferrer(Visitor $visitor, $referrerType) { return $visitor->getVisitorColumn('referer_type') == $referrerType; }
/** * @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; }
/** * 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)); }
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')); }
/** * @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); }
/** * 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); }