PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / WpStatistics / Importers / Actions / UserCountryImporter.php
matomo / classes / WpMatomo / WpStatistics / Importers / Actions Last commit date
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