diff --git a/ExcludeCountries.php b/ExcludeCountries.php index df07c94..1b609df 100644 --- a/ExcludeCountries.php +++ b/ExcludeCountries.php @@ -10,11 +10,27 @@ namespace Piwik\Plugins\ExcludeCountries; use Piwik\Common; use Piwik\Container\StaticContainer; +use Piwik\Piwik; +use Piwik\Plugin; use Piwik\Plugins\UserCountry\LocationProvider; +use Piwik\Settings\Setting; use Piwik\Tracker\Request; -class ExcludeCountries extends \Piwik\Plugin +class ExcludeCountries extends Plugin { + /** + * inspired by plugins/UserCountry/Columns/Country.php + */ + public static function listCountries() { + $regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider'); + $countryList = $regionDataProvider->getCountryList(); + array_walk($countryList, function (&$item, $key) { + $item = Piwik::translate('Intl_Country_' . strtoupper($key)); + }); + asort($countryList); //order by localized name + return $countryList; + } + public function registerEvents() { return [ "Tracker.isExcludedVisit" => "checkExcludedCountry", @@ -24,13 +40,35 @@ class ExcludeCountries extends \Piwik\Plugin public function checkExcludedCountry(&$excluded, Request $request) { $logger = StaticContainer::getContainer()->get("Psr\Log\LoggerInterface"); $provider = LocationProvider::getProviderById(Common::getCurrentLocationProviderId()); + $settings = new SystemSettings(); + $location = $provider->getLocation(["ip" => $request->getIp()]); - $countryCode = $location[LocationProvider::COUNTRY_CODE_KEY]; - if (strtolower($countryCode) !== "de") { - $logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")"); - $excluded = true; + $countryCode = strtolower($location[LocationProvider::COUNTRY_CODE_KEY]); + + $excludeBool = $settings->excludeBool->getValue(); + + if ($excludeBool) { + $countries = $this->settingToCountryCodes($settings->excludedCountries); + if (in_array($countryCode, $countries)) { + $logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")"); + $excluded = true; + } + } else { + $countries = $this->settingToCountryCodes($settings->includedCountries); + if (!in_array($countryCode, $countries)) { + $logger->debug("request excluded by ExcludeCountries plugin (" . $countryCode . ")"); + $excluded = true; + } } } + private function settingToCountryCodes(Setting $setting) { + $codes = []; + foreach ($setting->getValue() as $value) { + $codes[] = $value["languageCode"]; + } + return $codes; + } + } diff --git a/SystemSettings.php b/SystemSettings.php new file mode 100644 index 0000000..134795b --- /dev/null +++ b/SystemSettings.php @@ -0,0 +1,73 @@ +metric->getValue(); + * $settings->description->getValue(); + */ +class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings +{ + /** @var Setting */ + public $excludeBool; + + /** @var Setting */ + public $excludedCountries; + + /** @var Setting */ + public $includedCountries; + + protected function init() { + $this->excludeBool = $this->createExcludeBoolSetting(); + $this->excludedCountries = $this->createExcludedCountriesSetting(); + $this->includedCountries = $this->createIncludedCountriesSetting(); + + } + + private function createExcludeBoolSetting() { + return $this->makeSetting('excludeBool', $default = true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) { + $field->title = 'Exclude Countries'; + $field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX; + $field->description = 'If enabled, visitors from the selected countries are not tracked. If disabled, only visitors from the selected countries are tracked'; + }); + } + + private function createExcludedCountriesSetting() { + return $this->makeSetting('excludedCountries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) { + $countries = ExcludeCountries::listCountries(); + $field->title = 'Excluded Countries'; + $field->description = "Don't track users from these countries."; + $field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE; + $field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT); + $field1->availableValues = $countries; + $field->uiControlAttributes['field1'] = $field1->toArray(); + $field->condition = "excludeBool == true"; + }); + } + + private function createIncludedCountriesSetting() { + return $this->makeSetting('includedCountries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) { + $countries = ExcludeCountries::listCountries(); + $field->title = 'Included Countries'; + $field->description = "Only track users from these countries."; + $field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE; + $field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT); + $field1->availableValues = $countries; + $field->uiControlAttributes['field1'] = $field1->toArray(); + $field->condition = "excludeBool == false"; + }); + } +}