ActionsInterface.php
4 years ago
DeviceDetectionImporter.php
2 weeks ago
PagesImporter.php
2 weeks ago
RecordImporter.php
2 weeks ago
ReferrersImporter.php
8 months ago
UserCountryImporter.php
8 months ago
VisitorsImporter.php
2 weeks ago
VisitsTimeImporter.php
1 year ago
UserCountryImporter.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WpMatomo\WpStatistics\Importers\Actions; |
| 4 | |
| 5 | use Piwik\Common; |
| 6 | use Piwik\Config as PiwikConfig; |
| 7 | use Piwik\Date; |
| 8 | use Psr\Log\LoggerInterface; |
| 9 | use WP_STATISTICS\GeoIP; |
| 10 | use WpMatomo\WpStatistics\DataConverters\UserCityConverter; |
| 11 | use WpMatomo\WpStatistics\DataConverters\UserCountryConverter; |
| 12 | use WpMatomo\WpStatistics\DataConverters\UserRegionConverter; |
| 13 | use WpMatomo\WpStatistics\Geoip2; |
| 14 | use Piwik\Plugins\UserCountry\Archiver; |
| 15 | |
| 16 | /** |
| 17 | * reprocess geo localisation data as we can use a different (more complete) database |
| 18 | * |
| 19 | * @package WpMatomo |
| 20 | * @subpackage WpStatisticsImport |
| 21 | */ |
| 22 | class UserCountryImporter extends RecordImporter implements ActionsInterface { |
| 23 | |
| 24 | const PLUGIN_NAME = 'UserCountry'; |
| 25 | |
| 26 | const CITY_PATTERN = '/[a-z ]+,([a-z ]+)/i'; |
| 27 | |
| 28 | protected $visitors = null; |
| 29 | |
| 30 | /** |
| 31 | * @var Geoip2 |
| 32 | */ |
| 33 | private $geoip; |
| 34 | |
| 35 | public function __construct( LoggerInterface $logger ) { |
| 36 | parent::__construct( $logger ); |
| 37 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 38 | $this->maximum_rows_in_data_table_level_zero = @PiwikConfig::getInstance()->General['datatable_archiving_maximum_rows_standard']; |
| 39 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 40 | $this->maximum_rows_in_sub_data_table = @PiwikConfig::getInstance()->General['datatable_archiving_maximum_rows_standard']; |
| 41 | } |
| 42 | public function import_records( Date $date ) { |
| 43 | $this->geoip = Geoip2::getInstance(); |
| 44 | $this->visitors = $this->get_visitors( $date ); |
| 45 | if ( method_exists( GeoIP::class, 'active' ) && ! GeoIP::active( 'city' ) ) { |
| 46 | // fix if geoip city if is not enabled |
| 47 | $nb_visitors = count( $this->visitors ); |
| 48 | for ( $i = 0; $i < $nb_visitors; $i++ ) { |
| 49 | $this->visitors[ $i ]['city'] = ''; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | $this->import_countries(); |
| 54 | $this->import_regions(); |
| 55 | $this->import_cities(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Extract the region from the wpstatistics label |
| 60 | * |
| 61 | * @param array $visitor the wpstatistics visitor data |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | private function getRegion( array $visitor ) { |
| 66 | return $visitor['region']; |
| 67 | } |
| 68 | |
| 69 | private function import_regions() { |
| 70 | foreach ( $this->visitors as $id => $visitor ) { |
| 71 | $this->visitors[ $id ]['matomo_region'] = $this->geoip->get_matomo_region_code( $visitor['matomo_country'], $this->getRegion( $visitor ) ); |
| 72 | } |
| 73 | $regions = UserRegionConverter::convert( $this->visitors ); |
| 74 | $this->logger->debug( 'Import {nb_regions} regions...', [ 'nb_regions' => $regions->getRowsCount() ] ); |
| 75 | $this->insert_record( Archiver::REGION_RECORD_NAME, $regions, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table ); |
| 76 | Common::destroy( $regions ); |
| 77 | } |
| 78 | |
| 79 | private function import_cities() { |
| 80 | // apply the country name to normalize with the matomo data |
| 81 | foreach ( $this->visitors as $id => $visitor ) { |
| 82 | $this->visitors[ $id ]['matomo_city'] = $this->geoip->get_matomo_city_code( $visitor['city'], $visitor['matomo_region'] ); |
| 83 | } |
| 84 | $cities = UserCityConverter::convert( $this->visitors ); |
| 85 | $this->logger->debug( 'Import {nb_cities} cities...', [ 'nb_cities' => $cities->getRowsCount() ] ); |
| 86 | $this->insert_record( Archiver::CITY_RECORD_NAME, $cities, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table ); |
| 87 | Common::destroy( $cities ); |
| 88 | } |
| 89 | |
| 90 | private function import_countries() { |
| 91 | foreach ( $this->visitors as $id => $visitor ) { |
| 92 | $this->visitors[ $id ]['matomo_country'] = $visitor['location']; |
| 93 | } |
| 94 | |
| 95 | $countries = UserCountryConverter::convert( $this->visitors ); |
| 96 | $this->logger->debug( 'Import {nb_countries} countries...', [ 'nb_countries' => $countries->getRowsCount() ] ); |
| 97 | $this->insert_record( Archiver::COUNTRY_RECORD_NAME, $countries, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table ); |
| 98 | $this->insert_numeric_records( [ Archiver::DISTINCT_COUNTRIES_METRIC => $countries->getRowsCount() ] ); |
| 99 | Common::destroy( $countries ); |
| 100 | } |
| 101 | } |
| 102 |