Returns the visitor country based on the Browser 'accepted language'
information, but provides a hook for geolocation via IP address.
public static getCountry ( string $lang, boolean $enableLanguageToCountryGuess, string $ip ) : string | ||
$lang | string | browser lang |
$enableLanguageToCountryGuess | boolean | If set to true, some assumption will be made and detection guessed more often, but accuracy could be affected |
$ip | string | |
return | string | 2 letter ISO code |
public function index() { Piwik::checkUserIsNotAnonymous(); $view = new View('@MobileMessaging/index'); $view->isSuperUser = Piwik::hasUserSuperUserAccess(); $mobileMessagingAPI = API::getInstance(); $view->delegatedManagement = $mobileMessagingAPI->getDelegatedManagement(); $view->credentialSupplied = $mobileMessagingAPI->areSMSAPICredentialProvided(); $view->accountManagedByCurrentUser = $view->isSuperUser || $view->delegatedManagement; $view->strHelpAddPhone = Piwik::translate('MobileMessaging_Settings_PhoneNumbers_HelpAdd', array(Piwik::translate('General_Settings'), Piwik::translate('MobileMessaging_SettingsMenu'))); if ($view->credentialSupplied && $view->accountManagedByCurrentUser) { $view->provider = $mobileMessagingAPI->getSMSProvider(); $view->creditLeft = $mobileMessagingAPI->getCreditLeft(); } $view->smsProviders = SMSProvider::$availableSMSProviders; // construct the list of countries from the lang files $countries = array(); foreach (Common::getCountriesList() as $countryCode => $continentCode) { if (isset(CountryCallingCodes::$countryCallingCodes[$countryCode])) { $countries[$countryCode] = array('countryName' => \Piwik\Plugins\UserCountry\countryTranslate($countryCode), 'countryCallingCode' => CountryCallingCodes::$countryCallingCodes[$countryCode]); } } $view->countries = $countries; $view->defaultCountry = Common::getCountry(LanguagesManager::getLanguageCodeForCurrentUser(), true, IP::getIpFromHeader()); $view->phoneNumbers = $mobileMessagingAPI->getPhoneNumbers(); $this->setBasicVariablesView($view); return $view->render(); }
/** * Guesses a visitor's location using a visitor's browser language. * * @param array $info Contains 'ip' & 'lang' keys. * @return array Contains the guessed country code mapped to LocationProvider::COUNTRY_CODE_KEY. */ public function getLocation($info) { $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess']; if (empty($info['lang'])) { $info['lang'] = Common::getBrowserLanguage(); } $country = Common::getCountry($info['lang'], $enableLanguageToCountryGuess, $info['ip']); $location = array(parent::COUNTRY_CODE_KEY => $country); $this->completeLocationResult($location); return $location; }
private function setManageVariables(View $view) { $view->isSuperUser = Piwik::hasUserSuperUserAccess(); $mobileMessagingAPI = API::getInstance(); $view->delegatedManagement = $mobileMessagingAPI->getDelegatedManagement(); $view->credentialSupplied = $mobileMessagingAPI->areSMSAPICredentialProvided(); $view->accountManagedByCurrentUser = $view->isSuperUser || $view->delegatedManagement; $view->strHelpAddPhone = $this->translator->translate('MobileMessaging_Settings_PhoneNumbers_HelpAdd', array($this->translator->translate('General_Settings'), $this->translator->translate('MobileMessaging_SettingsMenu'))); $view->creditLeft = 0; $currentProvider = ''; if ($view->credentialSupplied && $view->accountManagedByCurrentUser) { $currentProvider = $mobileMessagingAPI->getSMSProvider(); $view->creditLeft = $mobileMessagingAPI->getCreditLeft(); } $view->delegateManagementOptions = array(array('key' => '0', 'value' => Piwik::translate('General_No'), 'description' => Piwik::translate('General_Default') . '. ' . Piwik::translate('MobileMessaging_Settings_LetUsersManageAPICredential_No_Help')), array('key' => '1', 'value' => Piwik::translate('General_Yes'), 'description' => Piwik::translate('MobileMessaging_Settings_LetUsersManageAPICredential_Yes_Help'))); $providers = array(); $providerOptions = array(); foreach (SMSProvider::findAvailableSmsProviders() as $provider) { if (empty($currentProvider)) { $currentProvider = $provider->getId(); } $providers[$provider->getId()] = $provider->getDescription(); $providerOptions[$provider->getId()] = $provider->getId(); } $view->provider = $currentProvider; $view->smsProviders = $providers; $view->smsProviderOptions = $providerOptions; $defaultCountry = Common::getCountry(LanguagesManager::getLanguageCodeForCurrentUser(), true, IP::getIpFromHeader()); $view->defaultCallingCode = ''; // construct the list of countries from the lang files $countries = array(array('key' => '', 'value' => '')); foreach ($this->regionDataProvider->getCountryList() as $countryCode => $continentCode) { if (isset(CountryCallingCodes::$countryCallingCodes[$countryCode])) { if ($countryCode == $defaultCountry) { $view->defaultCallingCode = CountryCallingCodes::$countryCallingCodes[$countryCode]; } $countries[] = array('key' => CountryCallingCodes::$countryCallingCodes[$countryCode], 'value' => \Piwik\Plugins\UserCountry\countryTranslate($countryCode)); } } $view->countries = $countries; $view->phoneNumbers = $mobileMessagingAPI->getPhoneNumbers(); $this->setBasicVariablesView($view); }
/** * Records one or several goals matched in this request. * * @param int $idSite * @param array $visitorInformation * @param array $visitCustomVariables * @param Action $action */ public function recordGoals($idSite, $visitorInformation, $visitCustomVariables, $action) { $referrerTimestamp = $this->request->getParam('_refts'); $referrerUrl = $this->request->getParam('_ref'); $referrerCampaignName = trim(urldecode($this->request->getParam('_rcn'))); $referrerCampaignKeyword = trim(urldecode($this->request->getParam('_rck'))); $browserLanguage = $this->request->getBrowserLanguage(); $location_country = isset($visitorInformation['location_country']) ? $visitorInformation['location_country'] : Common::getCountry($browserLanguage, $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess'], $visitorInformation['location_ip']); $goal = array('idvisit' => $visitorInformation['idvisit'], 'idsite' => $idSite, 'idvisitor' => $visitorInformation['idvisitor'], 'server_time' => Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']), 'location_country' => $location_country, 'visitor_returning' => $visitorInformation['visitor_returning'], 'visitor_days_since_first' => $visitorInformation['visitor_days_since_first'], 'visitor_days_since_order' => $visitorInformation['visitor_days_since_order'], 'visitor_count_visits' => $visitorInformation['visitor_count_visits']); $extraLocationCols = array('location_region', 'location_city', 'location_latitude', 'location_longitude'); foreach ($extraLocationCols as $col) { if (isset($visitorInformation[$col])) { $goal[$col] = $visitorInformation[$col]; } } // Copy Custom Variables from Visit row to the Goal conversion // Otherwise, set the Custom Variables found in the cookie sent with this request $goal += $visitCustomVariables; $maxCustomVariables = CustomVariables::getMaxCustomVariables(); for ($i = 1; $i <= $maxCustomVariables; $i++) { if (isset($visitorInformation['custom_var_k' . $i]) && strlen($visitorInformation['custom_var_k' . $i])) { $goal['custom_var_k' . $i] = $visitorInformation['custom_var_k' . $i]; } if (isset($visitorInformation['custom_var_v' . $i]) && strlen($visitorInformation['custom_var_v' . $i])) { $goal['custom_var_v' . $i] = $visitorInformation['custom_var_v' . $i]; } } // 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 = $visitorInformation['referer_type']; $name = $visitorInformation['referer_name']; $keyword = $visitorInformation['referer_keyword']; $time = $visitorInformation['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)) { $referrer = new Referrer(); $referrer = $referrer->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); $goal += array('referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword, 'referer_visit_server_date' => date("Y-m-d", $time)); // some goals are converted, so must be ecommerce Order or Cart Update if ($this->requestIsEcommerce) { $this->recordEcommerceGoal($goal, $visitorInformation); } else { $this->recordStandardGoals($goal, $action, $visitorInformation); } }
/** * @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; }