PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.19.1
Independent Analytics – WordPress Analytics Plugin v1.19.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 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 / IAWP / Real_Time.php
independent-analytics / IAWP Last commit date
AJAX 3 years ago Migrations 3 years ago Models 3 years ago Queries 3 years ago Tables 3 years ago Utils 3 years ago Campaign_Builder.php 3 years ago Capability_Manager.php 3 years ago Chart.php 3 years ago Chart_Geo.php 3 years ago Chart_SVG.php 3 years ago Current_Resource.php 3 years ago Dashboard_Options.php 3 years ago Dashboard_Widget.php 3 years ago Email_Reports.php 3 years ago Filters.php 3 years ago Geo_Database.php 3 years ago Geo_Database_Download_Job.php 3 years ago Health_Check.php 3 years ago Independent_Analytics.php 3 years ago Known_Referrers.php 3 years ago PDF.php 3 years ago Query.php 3 years ago Quick_Stats.php 3 years ago REST_API.php 3 years ago Real_Time.php 3 years ago Settings.php 3 years ago Track_Resource_Changes.php 3 years ago View_Counter.php 3 years ago WP_Option_Cache_Bust.php 3 years ago
Real_Time.php
211 lines
1 <?php
2
3 namespace IAWP;
4
5 use IAWP\Queries\Campaigns;
6 use IAWP\Queries\Country_Statistics;
7 use IAWP\Queries\Current_Traffic_Finder;
8 use IAWP\Queries\Minute_Interval;
9 use IAWP\Queries\Referrers;
10 use IAWP\Queries\Resources;
11 use IAWP\Queries\Ten_Second_Interval;
12 use IAWP\Queries\Visitors_Over_Time_Finder;
13 use IAWP\Utils\Singleton;
14
15 class Real_Time
16 {
17 use Singleton;
18
19 public function __construct()
20 {
21 }
22
23 private function get_count_message(int $count, string $singular, string $plural): string
24 {
25 return number_format($count) . ' ' . _n($singular, $plural, $count);
26 }
27
28 private function get_views_count_message(int $count)
29 {
30 return $this->get_count_message(
31 $count,
32 __('View', 'iawp'),
33 __('Views', 'iawp')
34 );
35 }
36
37 private function get_visitor_count_message(int $count)
38 {
39 return $this->get_count_message(
40 $count,
41 __('Active Visitor', 'iawp'),
42 __('Active Visitors', 'iawp')
43 );
44 }
45
46 private function roundUpBySeconds(\DateTime $datetime, $precision_seconds): \DateTime
47 {
48 $datetime = clone $datetime;
49 $datetime->setTimestamp($precision_seconds * (int) ceil($datetime->getTimestamp() / $precision_seconds));
50
51 return $datetime;
52 }
53
54 public function get_real_time_analytics()
55 {
56 $thirty_minutes_ago = new \DateTime('-30 minutes');
57 $thirty_minutes_ago = $this->roundUpBySeconds($thirty_minutes_ago, 60);
58
59 $five_minutes_ago = new \DateTime('-5 minutes');
60 $five_minutes_ago = $this->roundUpBySeconds($five_minutes_ago, 10);
61
62 $now = new \DateTime();
63 $end_minutes = $this->roundUpBySeconds($now, 60);
64 $end_seconds = $this->roundUpBySeconds($now, 10);
65
66 $current_traffic_finder = new Current_Traffic_Finder([
67 'start' => $five_minutes_ago,
68 'convert_to_full_days' => false,
69 ]);
70 $current_traffic = $current_traffic_finder->fetch();
71
72 $visitors_by_minute_finder = new Visitors_Over_Time_Finder([
73 'interval' => new Minute_Interval(),
74 'start' => $thirty_minutes_ago,
75 'end' => $end_minutes,
76 'convert_to_full_days' => false,
77 ]);
78 $visitors_by_minute = $visitors_by_minute_finder->fetch();
79
80 $visitors_by_second_finder = new Visitors_Over_Time_Finder([
81 'interval' => new Ten_Second_Interval(),
82 'start' => $five_minutes_ago,
83 'end' => $end_seconds,
84 'convert_to_full_days' => false,
85 ]);
86 $visitors_by_second = $visitors_by_second_finder->fetch();
87
88 $pages = new Resources([
89 'start' => $five_minutes_ago,
90 'convert_to_full_days' => false,
91 ]);
92
93 $page_data = array_slice($pages->fetch(), 0, 10);
94 $page_rows = array_map(function ($row, $index) {
95 return [
96 'id' => $row->id(),
97 'position' => $index + 1,
98 'title' => $row->title(),
99 'views' => $row->views(),
100 'subtitle' => $row->most_popular_subtitle(),
101 ];
102 }, $page_data, array_keys($page_data));
103
104 $referrers = new Referrers([
105 'start' => $five_minutes_ago,
106 'convert_to_full_days' => false,
107 ]);
108 $referrer_data = array_slice($referrers->fetch(), 0, 10);
109 $referrer_rows = array_map(function ($row, $index) {
110 return [
111 'id' => $row->referrer(),
112 'position' => $index + 1,
113 'title' => $row->referrer(),
114 'views' => $row->views(),
115 ];
116 }, $referrer_data, array_keys($referrer_data));
117
118 $countries = new Country_Statistics([
119 'start' => $five_minutes_ago,
120 'convert_to_full_days' => false,
121 ]);
122 $country_data = array_slice($countries->fetch(), 0, 10);
123 $country_rows = array_map(function ($row, $index) {
124 return [
125 'id' => $row->country(),
126 'position' => $index + 1,
127 'title' => $row->country(),
128 'views' => $row->views(),
129 'flag' => $row->flag(),
130 ];
131 }, $country_data, array_keys($country_data));
132
133 $campaigns = new Campaigns([
134 'start' => $five_minutes_ago,
135 'convert_to_full_days' => false,
136 ]);
137
138 $campaign_data = array_slice($campaigns->fetch(), 0, 10);
139 $campaign_rows = array_map(function ($row, $index) {
140 return [
141 'id' => $row->params(),
142 'position' => $index + 1,
143 'title' => $row->utm_campaign(),
144 'views' => $row->views(),
145 ];
146 }, $campaign_data, array_keys($campaign_data));
147
148 $visitor_message = $this->get_visitor_count_message(
149 $current_traffic->get_visitor_count()
150 );
151
152 $page_message = $this->get_count_message(
153 $current_traffic->get_page_count(),
154 __('Page', 'iawp'),
155 __('Pages', 'iawp')
156 );
157
158 $referrer_message = $this->get_count_message(
159 $current_traffic->get_referrer_count(),
160 __('Referrer', 'iawp'),
161 __('Referrers', 'iawp')
162 );
163
164 $country_message = $this->get_count_message(
165 $current_traffic->get_country_count(),
166 __('Country', 'iawp'),
167 __('Countries', 'iawp')
168 );
169
170 return [
171 'visitor_message' => $visitor_message,
172 'page_message' => $page_message,
173 'referrer_message' => $referrer_message,
174 'country_message' => $country_message,
175 'chart_data' => [
176 'minute_interval_visitors' => $visitors_by_minute->visitors,
177 'minute_interval_views' => $visitors_by_minute->views,
178 'minute_interval_labels_short' => $visitors_by_minute->interval_labels_short,
179 'minute_interval_labels_full' => $visitors_by_minute->interval_labels_full,
180 'second_interval_visitors' => $visitors_by_second->visitors,
181 'second_interval_views' => $visitors_by_second->views,
182 'second_interval_labels_short' => $visitors_by_second->interval_labels_short,
183 'second_interval_labels_full' => $visitors_by_second->interval_labels_full,
184 ],
185 'lists' => [
186 'pages' => [
187 'title' => __('Active Pages'),
188 'entries' => $page_rows,
189 ],
190 'referrers' => [
191 'title' => __('Active Referrers'),
192 'entries' => $referrer_rows,
193 ],
194 'countries' => [
195 'title' => __('Active Countries'),
196 'entries' => $country_rows,
197 ],
198 'campaigns' => [
199 'title' => __('Active Campaigns'),
200 'entries' => $campaign_rows,
201 ],
202 ],
203 ];
204 }
205
206 public function render_real_time_analytics()
207 {
208 echo iawp()->templates()->render('real_time', $this->get_real_time_analytics());
209 }
210 }
211