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 |