PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.22.1
Independent Analytics – WordPress Analytics Plugin v1.22.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 WooCommerce_Order.php 3 years ago
Real_Time.php
80 lines
1 <?php
2
3 namespace IAWP_SCOPED\IAWP;
4
5 use IAWP_SCOPED\IAWP\Queries\Campaigns;
6 use IAWP_SCOPED\IAWP\Queries\Country_Statistics;
7 use IAWP_SCOPED\IAWP\Queries\Current_Traffic_Finder;
8 use IAWP_SCOPED\IAWP\Queries\Minute_Interval;
9 use IAWP_SCOPED\IAWP\Queries\Referrers;
10 use IAWP_SCOPED\IAWP\Queries\Resources;
11 use IAWP_SCOPED\IAWP\Queries\Ten_Second_Interval;
12 use IAWP_SCOPED\IAWP\Queries\Visitors_Over_Time_Finder;
13 use IAWP_SCOPED\IAWP\Utils\Singleton;
14 class Real_Time
15 {
16 use Singleton;
17 public function __construct()
18 {
19 }
20 private function get_count_message(int $count, string $singular, string $plural) : string
21 {
22 return \number_format($count) . ' ' . \_n($singular, $plural, $count);
23 }
24 private function get_visitor_count_message(int $count) : string
25 {
26 return $this->get_count_message($count, \__('Active Visitor', 'iawp'), \__('Active Visitors', 'iawp'));
27 }
28 private function roundUpBySeconds(\DateTime $datetime, $precision_seconds) : \DateTime
29 {
30 $datetime = clone $datetime;
31 $datetime->setTimestamp($precision_seconds * (int) \ceil($datetime->getTimestamp() / $precision_seconds));
32 return $datetime;
33 }
34 public function get_real_time_analytics()
35 {
36 $thirty_minutes_ago = new \DateTime('-30 minutes');
37 $thirty_minutes_ago = $this->roundUpBySeconds($thirty_minutes_ago, 60);
38 $five_minutes_ago = new \DateTime('-5 minutes');
39 $five_minutes_ago = $this->roundUpBySeconds($five_minutes_ago, 10);
40 $now = new \DateTime();
41 $end_minutes = $this->roundUpBySeconds($now, 60);
42 $end_seconds = $this->roundUpBySeconds($now, 10);
43 $current_traffic_finder = new Current_Traffic_Finder(['start' => $five_minutes_ago, 'convert_to_full_days' => \false]);
44 $current_traffic = $current_traffic_finder->fetch();
45 $visitors_by_minute_finder = new Visitors_Over_Time_Finder(['interval' => new Minute_Interval(), 'start' => $thirty_minutes_ago, 'end' => $end_minutes, 'convert_to_full_days' => \false]);
46 $visitors_by_minute = $visitors_by_minute_finder->fetch();
47 $visitors_by_second_finder = new Visitors_Over_Time_Finder(['interval' => new Ten_Second_Interval(), 'start' => $five_minutes_ago, 'end' => $end_seconds, 'convert_to_full_days' => \false]);
48 $visitors_by_second = $visitors_by_second_finder->fetch();
49 $pages = new Resources(['start' => $five_minutes_ago, 'convert_to_full_days' => \false]);
50 $page_data = \array_slice($pages->fetch(), 0, 10);
51 $page_rows = \array_map(function ($row, $index) {
52 return ['id' => $row->id(), 'position' => $index + 1, 'title' => $row->title(), 'views' => $row->views(), 'subtitle' => $row->most_popular_subtitle()];
53 }, $page_data, \array_keys($page_data));
54 $referrers = new Referrers(['start' => $five_minutes_ago, 'convert_to_full_days' => \false]);
55 $referrer_data = \array_slice($referrers->fetch(), 0, 10);
56 $referrer_rows = \array_map(function ($row, $index) {
57 return ['id' => $row->referrer(), 'position' => $index + 1, 'title' => $row->referrer(), 'views' => $row->views()];
58 }, $referrer_data, \array_keys($referrer_data));
59 $countries = new Country_Statistics(['start' => $five_minutes_ago, 'convert_to_full_days' => \false]);
60 $country_data = \array_slice($countries->fetch(), 0, 10);
61 $country_rows = \array_map(function ($row, $index) {
62 return ['id' => $row->country(), 'position' => $index + 1, 'title' => $row->country(), 'views' => $row->views(), 'flag' => $row->flag()];
63 }, $country_data, \array_keys($country_data));
64 $campaigns = new Campaigns(['start' => $five_minutes_ago, 'convert_to_full_days' => \false]);
65 $campaign_data = \array_slice($campaigns->fetch(), 0, 10);
66 $campaign_rows = \array_map(function ($row, $index) {
67 return ['id' => $row->params(), 'position' => $index + 1, 'title' => $row->utm_campaign(), 'views' => $row->views()];
68 }, $campaign_data, \array_keys($campaign_data));
69 $visitor_message = $this->get_visitor_count_message($current_traffic->get_visitor_count());
70 $page_message = $this->get_count_message($current_traffic->get_page_count(), \__('Page', 'iawp'), \__('Pages', 'iawp'));
71 $referrer_message = $this->get_count_message($current_traffic->get_referrer_count(), \__('Referrer', 'iawp'), \__('Referrers', 'iawp'));
72 $country_message = $this->get_count_message($current_traffic->get_country_count(), \__('Country', 'iawp'), \__('Countries', 'iawp'));
73 return ['visitor_message' => $visitor_message, 'page_message' => $page_message, 'referrer_message' => $referrer_message, 'country_message' => $country_message, 'chart_data' => ['minute_interval_visitors' => $visitors_by_minute->visitors, 'minute_interval_views' => $visitors_by_minute->views, 'minute_interval_labels_short' => $visitors_by_minute->interval_labels_short, 'minute_interval_labels_full' => $visitors_by_minute->interval_labels_full, 'second_interval_visitors' => $visitors_by_second->visitors, 'second_interval_views' => $visitors_by_second->views, 'second_interval_labels_short' => $visitors_by_second->interval_labels_short, 'second_interval_labels_full' => $visitors_by_second->interval_labels_full], 'lists' => ['pages' => ['title' => \__('Active Pages'), 'entries' => $page_rows], 'referrers' => ['title' => \__('Active Referrers'), 'entries' => $referrer_rows], 'countries' => ['title' => \__('Active Countries'), 'entries' => $country_rows], 'campaigns' => ['title' => \__('Active Campaigns'), 'entries' => $campaign_rows]]];
74 }
75 public function render_real_time_analytics()
76 {
77 echo \IAWP_SCOPED\iawp()->templates()->render('real_time', $this->get_real_time_analytics());
78 }
79 }
80