public function enrichVisitWithLocation(&$visitorInfo, \Piwik\Tracker\Request $request) { require_once PIWIK_INCLUDE_PATH . "/plugins/UserCountry/LocationProvider.php"; $ipAddress = IP::N2P(Config::getInstance()->Tracker['use_anonymized_ip_for_visit_enrichment'] == 1 ? $visitorInfo['location_ip'] : $request->getIp()); $userInfo = array('lang' => $visitorInfo['location_browser_lang'], 'ip' => $ipAddress); $id = Common::getCurrentLocationProviderId(); $provider = LocationProvider::getProviderById($id); if ($provider === false) { $id = DefaultProvider::ID; $provider = LocationProvider::getProviderById($id); Common::printDebug("GEO: no current location provider sent, falling back to default '{$id}' one."); } $location = $provider->getLocation($userInfo); // if we can't find a location, use default provider if ($location === false) { $defaultId = DefaultProvider::ID; $provider = LocationProvider::getProviderById($defaultId); $location = $provider->getLocation($userInfo); Common::printDebug("GEO: couldn't find a location with Geo Module '{$id}', using Default '{$defaultId}' provider as fallback..."); $id = $defaultId; } Common::printDebug("GEO: Found IP {$ipAddress} location (provider '" . $id . "'): " . var_export($location, true)); if (empty($location['country_code'])) { // sanity check $location['country_code'] = \Piwik\Tracker\Visit::UNKNOWN_CODE; } // add optional location components $this->updateVisitInfoWithLocation($visitorInfo, $location); }
/** * Logs the provider in the log_visit table */ public function enrichVisitWithProviderInfo(&$visitorInfo, \Piwik\Tracker\Request $request) { // if provider info has already been set, abort if (!empty($visitorInfo['location_provider'])) { return; } $privacyConfig = new PrivacyManagerConfig(); $ip = IP::N2P($privacyConfig->useAnonymizedIpForVisitEnrichment ? $visitorInfo['location_ip'] : $request->getIp()); // 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; } $hostname = $this->getHost($ip); $hostnameExtension = $this->getCleanHostname($hostname); // add the provider value in the table log_visit $visitorInfo['location_provider'] = $hostnameExtension; $visitorInfo['location_provider'] = substr($visitorInfo['location_provider'], 0, 100); // improve the country using the provider extension if valid $hostnameDomain = substr($hostnameExtension, 1 + strrpos($hostnameExtension, '.')); if ($hostnameDomain == 'uk') { $hostnameDomain = 'gb'; } if (array_key_exists($hostnameDomain, Common::getCountriesList())) { $visitorInfo['location_country'] = $hostnameDomain; } }
function getIp() { if (isset($this->details['location_ip'])) { return IP::N2P($this->details['location_ip']); } return false; }
/** * Hook on Tracker.Visit.setVisitorIp to anomymize visitor IP addresses */ public function setVisitorIpAddress(&$ip) { if (!$this->isActiveInTracker()) { Common::printDebug("Visitor IP was _not_ anonymized: " . IP::N2P($ip)); return; } $originalIp = $ip; $ip = self::applyIPMask($ip, Config::getInstance()->Tracker['ip_address_mask_length']); Common::printDebug("Visitor IP (was: " . IP::N2P($originalIp) . ") has been anonymized: " . IP::N2P($ip)); }
/** * Hook on Tracker.Visit.setVisitorIp to anomymize visitor IP addresses */ public function setVisitorIpAddress(&$ip) { if (!$this->isActive()) { Common::printDebug("Visitor IP was _not_ anonymized: " . IP::N2P($ip)); return; } $originalIp = $ip; $privacyConfig = new Config(); $ip = self::applyIPMask($ip, $privacyConfig->ipAddressMaskLength); Common::printDebug("Visitor IP (was: " . IP::N2P($originalIp) . ") has been anonymized: " . IP::N2P($ip)); }
/** * @param Request $request * @param Visitor $visitor * @param Action|null $action * @return mixed */ public function onNewVisit(Request $request, Visitor $visitor, $action) { // 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 = IP::N2P($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; }
/** * Get the start and end IP addresses for an IP address range * * @param string $ipRange IP address range in presentation format * @return array|false Array( low, high ) IP addresses in presentation format; or false if error */ public function getIpsForRange($ipRange) { $range = IP::getIpsForRange($ipRange); if ($range === false) { return false; } return array(IP::N2P($range[0]), IP::N2P($range[1])); }
$rows = Db::fetchAll("SELECT idvisit, location_ip, " . implode(',', array_keys($logVisitFieldsToUpdate)) . "\n\t\t\t\t\t\tFROM " . Common::prefixTable('log_visit') . "\n\t\t\t\t\t\tLIMIT {$start}, {$limit}"); if (!count($rows)) { continue; } foreach ($rows as $row) { $fieldsToSet = array(); foreach ($logVisitFieldsToUpdate as $field => $ignore) { if (empty($fieldsToSet[$field])) { $fieldsToSet[] = $field; } } // skip if it already has a location if (empty($fieldsToSet)) { continue; } $ip = IP::N2P($row['location_ip']); $location = $provider->getLocation(array('ip' => $ip)); if (!empty($location[LocationProvider::COUNTRY_CODE_KEY])) { $location[LocationProvider::COUNTRY_CODE_KEY] = strtolower($location[LocationProvider::COUNTRY_CODE_KEY]); } $row['location_country'] = strtolower($row['location_country']); $columnsToSet = array(); $bind = array(); foreach ($logVisitFieldsToUpdate as $column => $locationKey) { if (!empty($location[$locationKey]) && $location[$locationKey] != $row[$column]) { $columnsToSet[] = $column . ' = ?'; $bind[] = $location[$locationKey]; } } if (empty($columnsToSet)) { continue;
private function printVisitorInformation() { $debugVisitInfo = $this->visitorInfo; $debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']); $debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']); $debugVisitInfo['location_ip'] = IP::N2P($debugVisitInfo['location_ip']); Common::printDebug($debugVisitInfo); }
/** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information * * @param Action $action * @param bool $visitIsConverted */ protected function handleNewVisit($action, $visitIsConverted) { Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")"); $this->visitorInfo = $this->getNewVisitorInformation($action); // Add Custom variable key,value to the visitor array $this->visitorInfo = array_merge($this->visitorInfo, $this->visitorCustomVariables); $this->visitorInfo['visit_goal_converted'] = $visitIsConverted ? 1 : 0; $this->visitorInfo['referer_name'] = substr($this->visitorInfo['referer_name'], 0, 70); $this->visitorInfo['referer_keyword'] = substr($this->visitorInfo['referer_keyword'], 0, 255); $this->visitorInfo['config_resolution'] = substr($this->visitorInfo['config_resolution'], 0, 9); /** * Triggered before a new [visit entity](/guides/persistence-and-the-mysql-backend#visits) is persisted. * * This event can be used to modify the visit entity or add new information to it before it is persisted. * The UserCountry plugin, for example, uses this event to add location information for each visit. * * @param array &$visit The visit entity. Read [this](/guides/persistence-and-the-mysql-backend#visits) to see * what information it contains. * @param \Piwik\Tracker\Request $request An object describing the tracking request being processed. */ Piwik::postEvent('Tracker.newVisitorInformation', array(&$this->visitorInfo, $this->request)); $this->request->overrideLocation($this->visitorInfo); $this->printVisitorInformation(); $idVisit = $this->insertNewVisit($this->visitorInfo); $this->visitorInfo['idvisit'] = $idVisit; $this->visitorInfo['visit_first_action_time'] = $this->request->getCurrentTimestamp(); $this->visitorInfo['visit_last_action_time'] = $this->request->getCurrentTimestamp(); }
/** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information * * @param Visitor $visitor * @param Action $action * @param bool $visitIsConverted */ protected function handleNewVisit($visitor, $action, $visitIsConverted) { Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")"); $this->visitorInfo = $this->getNewVisitorInformation($visitor); // Add Custom variable key,value to the visitor array $this->visitorInfo = array_merge($this->visitorInfo, $this->visitorCustomVariables); $visitor->clearVisitorInfo(); foreach ($this->visitorInfo as $key => $value) { $visitor->setVisitorColumn($key, $value); } $dimensions = $this->getAllVisitDimensions(); $this->triggerHookOnDimensions($dimensions, 'onNewVisit', $visitor, $action); if ($visitIsConverted) { $this->triggerHookOnDimensions($dimensions, 'onConvertedVisit', $visitor, $action); } /** * Triggered before a new [visit entity](/guides/persistence-and-the-mysql-backend#visits) is persisted. * * This event can be used to modify the visit entity or add new information to it before it is persisted. * The UserCountry plugin, for example, uses this event to add location information for each visit. * * @param array &$visit The visit entity. Read [this](/guides/persistence-and-the-mysql-backend#visits) to see * what information it contains. * @param \Piwik\Tracker\Request $request An object describing the tracking request being processed. */ Piwik::postEvent('Tracker.newVisitorInformation', array(&$this->visitorInfo, $this->request)); $this->printVisitorInformation(); $idVisit = $this->insertNewVisit($this->visitorInfo); $this->visitorInfo['idvisit'] = $idVisit; $this->visitorInfo['visit_first_action_time'] = $this->request->getCurrentTimestamp(); $this->visitorInfo['visit_last_action_time'] = $this->request->getCurrentTimestamp(); $visitor->setVisitorColumn('idvisit', $this->visitorInfo['idvisit']); $visitor->setVisitorColumn('visit_first_action_time', $this->visitorInfo['visit_first_action_time']); $visitor->setVisitorColumn('visit_last_action_time', $this->visitorInfo['visit_last_action_time']); }
/** * Checks if the visitor ip is in the excluded list * * @return bool */ protected function isVisitorIpExcluded() { $websiteAttributes = Cache::getCacheWebsiteAttributes($this->idSite); if (!empty($websiteAttributes['excluded_ips'])) { if (IP::isIpInRange($this->ip, $websiteAttributes['excluded_ips'])) { Common::printDebug('Visitor IP ' . IP::N2P($this->ip) . ' is excluded from being tracked'); return true; } } return false; }
private function getIpAddress($anonymizedIp, \Piwik\Tracker\Request $request) { $privacyConfig = new PrivacyManagerConfig(); $ip = $request->getIp(); if ($privacyConfig->useAnonymizedIpForVisitEnrichment) { $ip = $anonymizedIp; } $ipAddress = IP::N2P($ip); return $ipAddress; }