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 |