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 / DeviceDetectionImporter.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
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