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
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
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
201 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 Country_Statistics([ |
| 109 | 'start' => $five_minutes_ago, |
| 110 | 'convert_to_full_days' => false, |
| 111 | ]); |
| 112 | $country_data = array_slice($countries->fetch(), 0, 10); |
| 113 | $country_rows = array_map(function ($row, $index) { |
| 114 | return [ |
| 115 | 'id' => $row->country(), |
| 116 | 'position' => $index + 1, |
| 117 | 'title' => $row->country(), |
| 118 | 'views' => $row->views(), |
| 119 | 'flag' => $row->flag(), |
| 120 | ]; |
| 121 | }, $country_data, array_keys($country_data)); |
| 122 | |
| 123 | $campaigns = new Campaigns([ |
| 124 | 'start' => $five_minutes_ago, |
| 125 | 'convert_to_full_days' => false, |
| 126 | ]); |
| 127 | |
| 128 | $campaign_data = array_slice($campaigns->fetch(), 0, 10); |
| 129 | $campaign_rows = array_map(function ($row, $index) { |
| 130 | return [ |
| 131 | 'id' => $row->params(), |
| 132 | 'position' => $index + 1, |
| 133 | 'title' => $row->utm_campaign(), |
| 134 | 'views' => $row->views(), |
| 135 | ]; |
| 136 | }, $campaign_data, array_keys($campaign_data)); |
| 137 | |
| 138 | $visitor_message = $this->get_visitor_count_message( |
| 139 | $current_traffic->get_visitor_count() |
| 140 | ); |
| 141 | |
| 142 | $page_message = $this->get_count_message( |
| 143 | $current_traffic->get_page_count(), |
| 144 | __('Page', 'iawp'), |
| 145 | __('Pages', 'iawp') |
| 146 | ); |
| 147 | |
| 148 | $referrer_message = $this->get_count_message( |
| 149 | $current_traffic->get_referrer_count(), |
| 150 | __('Referrer', 'iawp'), |
| 151 | __('Referrers', 'iawp') |
| 152 | ); |
| 153 | |
| 154 | $country_message = $this->get_count_message( |
| 155 | $current_traffic->get_country_count(), |
| 156 | __('Country', 'iawp'), |
| 157 | __('Countries', 'iawp') |
| 158 | ); |
| 159 | |
| 160 | return [ |
| 161 | 'visitor_message' => $visitor_message, |
| 162 | 'page_message' => $page_message, |
| 163 | 'referrer_message' => $referrer_message, |
| 164 | 'country_message' => $country_message, |
| 165 | 'chart_data' => [ |
| 166 | 'minute_interval_visitors' => $visitors_by_minute->visitors, |
| 167 | 'minute_interval_views' => $visitors_by_minute->views, |
| 168 | 'minute_interval_labels_short' => $visitors_by_minute->interval_labels_short, |
| 169 | 'minute_interval_labels_full' => $visitors_by_minute->interval_labels_full, |
| 170 | 'second_interval_visitors' => $visitors_by_second->visitors, |
| 171 | 'second_interval_views' => $visitors_by_second->views, |
| 172 | 'second_interval_labels_short' => $visitors_by_second->interval_labels_short, |
| 173 | 'second_interval_labels_full' => $visitors_by_second->interval_labels_full, |
| 174 | ], |
| 175 | 'lists' => [ |
| 176 | 'pages' => [ |
| 177 | 'title' => __('Active Pages'), |
| 178 | 'entries' => $page_rows, |
| 179 | ], |
| 180 | 'referrers' => [ |
| 181 | 'title' => __('Active Referrers'), |
| 182 | 'entries' => $referrer_rows, |
| 183 | ], |
| 184 | 'countries' => [ |
| 185 | 'title' => __('Active Countries'), |
| 186 | 'entries' => $country_rows, |
| 187 | ], |
| 188 | 'campaigns' => [ |
| 189 | 'title' => __('Active Campaigns'), |
| 190 | 'entries' => $campaign_rows, |
| 191 | ], |
| 192 | ], |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | public function render_real_time_analytics() |
| 197 | { |
| 198 | echo IAWP()->templates()->render('real_time', $this->get_real_time_analytics()); |
| 199 | } |
| 200 | } |
| 201 |