PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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 / DataConverters / SubtableConverter.php
matomo / classes / WpMatomo / WpStatistics / DataConverters Last commit date
BrowsersConverter.php 1 year ago DataConverterInterface.php 4 years ago FilterConverter.php 2 years ago NumberConverter.php 1 year ago PagesTitleConverter.php 1 year ago PagesUrlConverter.php 1 year ago PlatformConverter.php 4 years ago ReferrersConverter.php 4 years ago SearchEngineConverter.php 1 year ago SearchQueryConverter.php 1 year ago SubtableConverter.php 1 year ago UserCityConverter.php 4 years ago UserCountryConverter.php 4 years ago UserRegionConverter.php 4 years ago VisitorsConverter.php 1 year ago VisitsTimeConverter.php 7 months ago
SubtableConverter.php
72 lines
1 <?php
2
3 namespace WpMatomo\WpStatistics\DataConverters;
4
5 use Piwik\DataTable;
6 use WpMatomo\WpStatistics\Importers\Actions\RecordImporter;
7
8 /**
9 * @package WpMatomo
10 * @subpackage WpStatisticsImport
11 */
12 class SubtableConverter {
13
14 public static function aggregate_by_key( array $wp_statistics_data, $first_key, $second_key ) {
15 $data = [];
16 if ( count( $wp_statistics_data ) ) {
17 foreach ( $wp_statistics_data as $row ) {
18 if ( ! array_key_exists( $row[ $first_key ], $data ) ) {
19 $data[ $row[ $first_key ] ] = [
20 'label' => RecordImporter::get_label( $row, $first_key ),
21 'data' => [],
22 'nb_uniq_visitors' => 0,
23 'nb_visits' => 0,
24 ];
25 }
26 $data[ $row[ $first_key ] ]['data'][ $row[ $second_key ] ] = [
27 'label' => RecordImporter::get_label( $row, $second_key ),
28 'nb_uniq_visitors' => $row['nb'],
29 'nb_visits' => $row['nb'],
30 ];
31 $data[ $row[ $first_key ] ]['nb_visits'] += $row['nb'];
32 $data[ $row[ $first_key ] ]['nb_uniq_visitors'] += $row['nb'];
33 }
34 }
35
36 $data_table = new DataTable();
37 foreach ( $data as $key => $row ) {
38 $data = $row['data'];
39 unset( $row['data'] );
40 $top_level_row = self::add_row_to_table( $data_table, new DataTable\Row( array( 0 => $row ) ), $key );
41 foreach ( $data as $sub_key => $sub_row ) {
42 self::add_row_to_subtable( $top_level_row, new DataTable\Row( array( 0 => $sub_row ) ), $sub_key );
43 }
44 }
45 return $data_table;
46 }
47
48 protected static function add_row_to_subtable( DataTable\Row $top_level_row, DataTable\Row $row_to_add, $new_label ) {
49 $sub_table = $top_level_row->getSubtable();
50 if ( ! $sub_table ) {
51 $sub_table = new DataTable();
52 $top_level_row->setSubtable( $sub_table );
53 }
54
55 return self::add_row_to_table( $sub_table, $row_to_add, $new_label );
56 }
57
58 protected static function add_row_to_table( DataTable $record, DataTable\Row $row, $new_label ) {
59 $found_row = $record->getRowFromLabel( $new_label );
60 if ( empty( $found_row ) ) {
61 $found_row = clone $row;
62 $found_row->deleteMetadata();
63 $found_row->setColumn( 'label', $new_label );
64 $record->addRow( $found_row );
65 } else {
66 $found_row->sumRow( $row, $copy_metadata = false );
67 }
68
69 return $found_row;
70 }
71 }
72