PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.10
Independent Analytics – WordPress Analytics Plugin v1.10
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / inc / queries / geos.php
independent-analytics / inc / queries Last commit date
geos.php 3 years ago range_query.php 3 years ago referrers.php 3 years ago resources.php 3 years ago views.php 3 years ago
geos.php
126 lines
1 <?php
2
3 namespace IAWP;
4
5 class Geos extends Range_Query
6 {
7 private $results;
8 private $include_sub_and_city;
9 private $ids;
10
11 public function __construct($start = null, $end = null, $include_sub_and_city = true, $ids = null)
12 {
13 parent::__construct($start, $end);
14 $this->include_sub_and_city = $include_sub_and_city;
15 $this->ids = $ids;
16 }
17
18 public function get_geos()
19 {
20 if (is_null($this->results)) {
21 $this->results = $this->query();
22 }
23
24 return $this->results;
25 }
26
27 public function get_geo_as_csv()
28 {
29 $referrers = $this->get_geos();
30 $arr = [['Continent', 'Country', 'Subdivision', 'City', 'Views', 'Visitors']];
31
32 foreach ($referrers as $referrer) {
33 $arr[] = [
34 $referrer->continent(),
35 $referrer->country(),
36 $referrer->subdivision(),
37 $referrer->city(),
38 $referrer->views(),
39 $referrer->visitors(),
40 ];
41 }
42
43 return Array_To_CSV::array2csv($arr);
44 }
45
46 private function query()
47 {
48 global $wpdb;
49 $views_table = DB::views_table();
50 $visitors_table = DB::visitors_table();
51 $include_sub_and_city = $this->include_sub_and_city ? 'true' : 'false';
52 $ids = $this->ids;
53
54 if (is_null($ids)) {
55 $skip_in = 1;
56 $ids = [0];
57 $in = '%s';
58 } elseif (count($ids) == 0) {
59 $skip_in = 0;
60 $ids = [0];
61 $in = '%s';
62 } else {
63 $skip_in = 0;
64 $in = implode(',', array_fill(0, count($ids), '%s'));
65 }
66
67 $geo_traffic_query = $wpdb->prepare(
68 "
69 SELECT
70 country_code,
71 IF( $include_sub_and_city = true, city, null) as city,
72 IF( $include_sub_and_city = true, subdivision, null) as subdivision,
73 country,
74 continent,
75 COUNT(*) AS views,
76 COUNT(DISTINCT visitor_id) AS visitors,
77 GROUP_CONCAT(DISTINCT $views_table.visitor_id) as visitor_ids
78 FROM
79 $views_table
80 LEFT JOIN $visitors_table ON $views_table.visitor_id = $visitors_table.id
81 WHERE
82 country_code IS NOT NULL
83 AND viewed_at BETWEEN %s AND %s
84 AND (%d = 1 OR $views_table.visitor_id IN ($in))
85 GROUP BY
86 continent,
87 country,
88 country_code,
89 IF( $include_sub_and_city = true, subdivision, null),
90 IF( $include_sub_and_city = true, city, null)
91 ORDER BY
92 views DESC,
93 city
94 ",
95 $this->formatted_start(),
96 $this->formatted_end(),
97 $skip_in,
98 ...$ids
99 );
100
101 $geo_traffic = $wpdb->get_results($geo_traffic_query);
102
103 return self::rows_to_geos($geo_traffic);
104 }
105
106 private function rows_to_geos($rows)
107 {
108 return array_map(function ($row) {
109 $visitor_ids = array_map(function ($id) {
110 return intval($id);
111 }, explode(',', $row->visitor_ids));
112
113 return new Geo(
114 $row->continent,
115 $row->country,
116 $row->country_code,
117 $row->subdivision,
118 $row->city,
119 intval($row->views),
120 intval($row->visitors),
121 $visitor_ids
122 );
123 }, $rows);
124 }
125 }
126