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