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
DeviceDetectionImporter.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WpMatomo\WpStatistics\Importers\Actions; |
| 4 | |
| 5 | use DeviceDetector\Parser\Client\Browser; |
| 6 | use DeviceDetector\Parser\OperatingSystem; |
| 7 | use Piwik\Common; |
| 8 | use Piwik\Plugins\DevicesDetection\Archiver; |
| 9 | use Piwik\Date; |
| 10 | use WpMatomo\WpStatistics\DataConverters\BrowsersConverter; |
| 11 | use WpMatomo\WpStatistics\DataConverters\PlatformConverter; |
| 12 | /** |
| 13 | * @package WpMatomo |
| 14 | * @subpackage WpStatisticsImport |
| 15 | */ |
| 16 | class DeviceDetectionImporter extends RecordImporter implements ActionsInterface { |
| 17 | |
| 18 | const PLUGIN_NAME = 'DevicesDetection'; |
| 19 | |
| 20 | public function import_records( Date $date ) { |
| 21 | $this->importBrowsers( $date ); |
| 22 | $this->importPlateform( $date ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param Date $date |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | private function importBrowsers( Date $date ) { |
| 31 | $devices = $this->get_visitors( $date ); |
| 32 | if ( array_key_exists( 'no_data', $devices ) && $devices['no_data'] ) { |
| 33 | $devices = array(); |
| 34 | } |
| 35 | $this->convertBrowsersInMatomo( $devices ); |
| 36 | $devices = BrowsersConverter::convert( $devices ); |
| 37 | $this->logger->debug( 'Import {nb_browsers} browsers...', [ 'nb_browsers' => $devices->getRowsCount() ] ); |
| 38 | $this->insert_record( Archiver::BROWSER_RECORD_NAME, $devices ); |
| 39 | Common::destroy( $devices ); |
| 40 | } |
| 41 | |
| 42 | private function convertPlatformsInMatomo( &$visitors ) { |
| 43 | // convert codification |
| 44 | $platform_ids = array_keys( OperatingSystem::getAvailableOperatingSystems() ); |
| 45 | $platform_names = array_values( OperatingSystem::getAvailableOperatingSystems() ); |
| 46 | // we do not have the version with wpstatistics, so set an empty version |
| 47 | array_walk( |
| 48 | $platform_ids, |
| 49 | function ( &$item1 ) { |
| 50 | $item1 = $item1 . ';'; |
| 51 | } |
| 52 | ); |
| 53 | $platform_ids = array_merge( $platform_ids, [ 'MAC;OS X' ] ); |
| 54 | $platform_names = array_merge( $platform_names, [ 'OS X' ] ); |
| 55 | foreach ( $visitors as $id => $visitor ) { |
| 56 | $platform = $this->get_platform( $visitor ); |
| 57 | if ( in_array( $platform, $platform_names, true ) ) { |
| 58 | $visitors[ $id ]['platform'] = str_replace( $platform_names, $platform_ids, $platform ); |
| 59 | } else { |
| 60 | $visitors[ $id ]['platform'] = 'UNK;UNK'; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private function get_platform( $visitor ) { |
| 66 | if ( isset( $visitor['os'] ) ) { |
| 67 | return $visitor['os']['name']; |
| 68 | } |
| 69 | |
| 70 | return $visitor['platform']; |
| 71 | } |
| 72 | |
| 73 | private function convertBrowsersInMatomo( &$devices ) { |
| 74 | // convert codification |
| 75 | $device_ids = array_keys( Browser::getAvailableBrowsers() ); |
| 76 | $device_names = array_values( Browser::getAvailableBrowsers() ); |
| 77 | // we do not have the version with wpstatistics, so set an empty version |
| 78 | $device_ids = array_merge( [ '', '', 'FM', 'MS', 'SB', 'IM' ], $device_ids ); |
| 79 | $device_names = array_merge( [ 'Microsoft Office', 'Unknown', 'Firefox Mobile', 'Silk', 'Samsung Internet', 'Mobile Internet Explorer' ], $device_names ); |
| 80 | foreach ( $devices as $id => $device ) { |
| 81 | if ( in_array( $device['agent'], $device_names, true ) ) { |
| 82 | $devices[ $id ]['agent'] = str_replace( $device_names, $device_ids, $device['agent'] ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | /** |
| 87 | * @param Date $date |
| 88 | */ |
| 89 | private function importPlateform( Date $date ) { |
| 90 | $platforms = $this->get_visitors( $date ); |
| 91 | $this->convertPlatformsInMatomo( $platforms ); |
| 92 | $platforms = PlatformConverter::convert( $platforms ); |
| 93 | $this->logger->debug( 'Import {nb_platform} platforms...', [ 'nb_platform' => $platforms->getRowsCount() ] ); |
| 94 | $this->insert_record( Archiver::OS_VERSION_RECORD_NAME, $platforms ); |
| 95 | Common::destroy( $platforms ); |
| 96 | } |
| 97 | } |
| 98 |