ActionsInterface.php
4 years ago
DeviceDetectionImporter.php
3 weeks ago
PagesImporter.php
3 weeks ago
RecordImporter.php
3 weeks ago
ReferrersImporter.php
8 months ago
UserCountryImporter.php
8 months ago
VisitorsImporter.php
3 weeks ago
VisitsTimeImporter.php
1 year ago
ReferrersImporter.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WpMatomo\WpStatistics\Importers\Actions; |
| 4 | |
| 5 | use Piwik\Common; |
| 6 | use Piwik\DataTable; |
| 7 | use Piwik\Plugins\Referrers\Archiver; |
| 8 | use Piwik\Date; |
| 9 | use WP_Statistics\Models\VisitorsModel; |
| 10 | use WpMatomo\WpStatistics\DataConverters\ReferrersConverter; |
| 11 | use WpMatomo\WpStatistics\DataConverters\SearchEngineConverter; |
| 12 | |
| 13 | /** |
| 14 | * @package WpMatomo |
| 15 | * @subpackage WpStatisticsImport |
| 16 | * |
| 17 | * phpcs:disable WordPress.DB |
| 18 | */ |
| 19 | class ReferrersImporter extends RecordImporter implements ActionsInterface { |
| 20 | |
| 21 | const PLUGIN_NAME = 'Referrers'; |
| 22 | |
| 23 | public function import_records( Date $date ) { |
| 24 | $this->import_referrers( $date ); |
| 25 | $this->import_search_engines( $date ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Matomo expects some fixed values for the search engine. |
| 30 | * Convert them here |
| 31 | * |
| 32 | * @param array $wp_statistics_data |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | private function convert_search_engines( &$wp_statistics_data ) { |
| 37 | foreach ( $wp_statistics_data as $row => $line ) { |
| 38 | /* |
| 39 | * the list of search engine available from wpstatistics is fixed |
| 40 | */ |
| 41 | $wp_statistics_data[ $row ]['engine'] = $this->convert_search_engine( $line['engine'] ); |
| 42 | } |
| 43 | } |
| 44 | private function import_search_engines( Date $date ) { |
| 45 | if ( class_exists( '\WP_Statistics\Models\VisitorsModel' ) ) { |
| 46 | $search_engine_by_keyword = $this->get_search_engine_records_from_model( $date ); |
| 47 | } else { |
| 48 | $search_engine_by_keyword = $this->get_search_engine_records_from_table( $date ); |
| 49 | } |
| 50 | |
| 51 | $this->logger->debug( 'Import {nb_se} search engines...', [ 'nb_se' => $search_engine_by_keyword->getRowsCount() ] ); |
| 52 | $this->insert_record( Archiver::SEARCH_ENGINES_RECORD_NAME, $search_engine_by_keyword, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table ); |
| 53 | Common::destroy( $search_engine_by_keyword ); |
| 54 | } |
| 55 | /** |
| 56 | * @param Date $date |
| 57 | */ |
| 58 | private function import_referrers( Date $date ) { |
| 59 | $referrers = $this->get_referrers( $date ); |
| 60 | $referrers = ReferrersConverter::convert( $referrers ); |
| 61 | $this->logger->debug( 'Import {nb_referrers} referrers...', [ 'nb_referrers' => $referrers->getRowsCount() ] ); |
| 62 | $this->insert_record( Archiver::WEBSITES_RECORD_NAME, $referrers, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table ); |
| 63 | Common::destroy( $referrers ); |
| 64 | } |
| 65 | /** |
| 66 | * @param Date $date |
| 67 | * |
| 68 | * @see \WP_Statistics\Referred::GenerateReferSQL |
| 69 | * @return array |
| 70 | */ |
| 71 | public function get_referrers( Date $date ) { |
| 72 | $limit = 10000; |
| 73 | global $wpdb; |
| 74 | // Check Protocol Of domain |
| 75 | $domain_name = rtrim( preg_replace( '/^https?:\/\//', '', get_site_url() ), ' / ' ); |
| 76 | // Return SQL |
| 77 | $sql = $wpdb->prepare( |
| 78 | 'SELECT referred as `domain`, count(referred) as `number` FROM ' . $this->get_table_name( 'visitor' ) . ' WHERE referred <> \'\' AND `referred` NOT LIKE \'%s\' AND last_counter = \'%s\' GROUP BY domain LIMIT %d', |
| 79 | array( '%' . $wpdb->_real_escape( $domain_name ) . '%', $date->toString(), $limit ) |
| 80 | ); |
| 81 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param Date $day |
| 86 | * |
| 87 | * @return DataTable |
| 88 | */ |
| 89 | private function get_search_engine_records_from_table( Date $day ) { |
| 90 | global $wpdb; |
| 91 | $sql = 'select engine, count(visitor) AS nb from ' . $this->get_table_name( 'search' ) . " where last_counter = '" . $day->toString() . "' group by engine order by engine;"; |
| 92 | $wp_statistics_data = $wpdb->get_results( $sql, ARRAY_A ); |
| 93 | $this->convert_search_engines( $wp_statistics_data ); |
| 94 | $search_engine_by_keyword = SearchEngineConverter::convert( $wp_statistics_data ); |
| 95 | return $search_engine_by_keyword; |
| 96 | } |
| 97 | |
| 98 | private function get_search_engine_records_from_model( Date $day ) { |
| 99 | $visitors_model = new VisitorsModel(); |
| 100 | $search_engine_data = $visitors_model->getReferrers( |
| 101 | [ |
| 102 | 'date' => [ |
| 103 | 'from' => $day->toString(), |
| 104 | 'to' => $day->toString(), |
| 105 | ], |
| 106 | 'source_channel' => [ 'search', 'paid_search' ], |
| 107 | 'group_by' => [ 'source_name' ], |
| 108 | 'per_page' => false, |
| 109 | ] |
| 110 | ); |
| 111 | |
| 112 | $search_engine_data = array_map( |
| 113 | function ( $row ) { |
| 114 | return [ |
| 115 | 'engine' => $row->source_name, |
| 116 | 'nb' => $row->visitors, |
| 117 | ]; |
| 118 | }, |
| 119 | $search_engine_data |
| 120 | ); |
| 121 | |
| 122 | $this->convert_search_engines( $search_engine_data ); |
| 123 | |
| 124 | $search_engine_by_keyword = SearchEngineConverter::convert( $search_engine_data ); |
| 125 | return $search_engine_by_keyword; |
| 126 | } |
| 127 | |
| 128 | private function convert_search_engine( $engine ) { |
| 129 | return str_replace( |
| 130 | [ 'google', 'duckduckgo', 'bing', 'baidu', 'yahoo', 'yandex', 'startpage', 'qwant', 'ecosia', 'ask' ], |
| 131 | [ 'Google', 'DuckDuckGo', 'Bing', 'Baidu', 'Yahoo!', 'Yandex', 'StartPage', 'Qwant', 'Ecosia', 'Ask' ], |
| 132 | $engine |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 |