AJAX
1 year ago
Admin_Page
1 year ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
1 year ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
1 year ago
Email_Reports
1 year ago
Filter_Lists
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Filters.php
1 year ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
1 year ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
1 year ago
Interrupt.php
1 year ago
Known_Referrers.php
2 years ago
Patch.php
1 year ago
Plugin_Conflict_Detector.php
1 year ago
Plugin_Group.php
1 year ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
1 year ago
Quick_Stats.php
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
Real_Time.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP\Date_Range\Exact_Date_Range; |
| 7 | use IAWP\Interval\Minute_Interval; |
| 8 | use IAWP\Interval\Ten_Second_Interval; |
| 9 | use IAWP\Rows\Campaigns; |
| 10 | use IAWP\Rows\Countries; |
| 11 | use IAWP\Rows\Device_Types; |
| 12 | use IAWP\Rows\Pages; |
| 13 | use IAWP\Rows\Referrers; |
| 14 | use IAWP\Utils\Singleton; |
| 15 | /** @internal */ |
| 16 | class Real_Time |
| 17 | { |
| 18 | use Singleton; |
| 19 | public function __construct() |
| 20 | { |
| 21 | } |
| 22 | public function get_real_time_analytics() |
| 23 | { |
| 24 | $thirty_minutes_ago = new DateTime('-30 minutes'); |
| 25 | $thirty_minutes_ago = $this->round_up_by_seconds($thirty_minutes_ago, 60); |
| 26 | $five_minutes_ago = new DateTime('-5 minutes'); |
| 27 | $five_minutes_ago = $this->round_up_by_seconds($five_minutes_ago, 10); |
| 28 | $now = new DateTime(); |
| 29 | $end_minutes = $this->round_up_by_seconds($now, 60); |
| 30 | $end_seconds = $this->round_up_by_seconds($now, 10); |
| 31 | $visitors_by_minute_date_range = new Exact_Date_Range($thirty_minutes_ago, $end_minutes, \false); |
| 32 | $visitors_by_minute_finder = new \IAWP\Visitors_Over_Time_Finder($visitors_by_minute_date_range, new Minute_Interval()); |
| 33 | $visitors_by_minute = $visitors_by_minute_finder->fetch(); |
| 34 | $visitors_by_second_date_range = new Exact_Date_Range($five_minutes_ago, $end_seconds, \false); |
| 35 | $visitors_by_second_finder = new \IAWP\Visitors_Over_Time_Finder($visitors_by_second_date_range, new Ten_Second_Interval()); |
| 36 | $visitors_by_second = $visitors_by_second_finder->fetch(); |
| 37 | $five_minute_date_range = new Exact_Date_Range($five_minutes_ago, new DateTime(), \false); |
| 38 | $current_traffic_finder = new \IAWP\Current_Traffic_Finder($five_minute_date_range); |
| 39 | $current_traffic = $current_traffic_finder->fetch(); |
| 40 | $pages = new Pages($five_minute_date_range, 10); |
| 41 | $page_rows = \array_map(function ($row, $index) { |
| 42 | return ['id' => $row->id(), 'position' => $index + 1, 'title' => $row->title(), 'views' => $row->views(), 'subtitle' => $row->most_popular_subtitle()]; |
| 43 | }, $pages->rows(), \array_keys($pages->rows())); |
| 44 | $referrers = new Referrers($five_minute_date_range, 10); |
| 45 | $referrer_rows = \array_map(function ($row, $index) { |
| 46 | return ['id' => $row->referrer(), 'position' => $index + 1, 'title' => $row->referrer(), 'views' => $row->views()]; |
| 47 | }, $referrers->rows(), \array_keys($referrers->rows())); |
| 48 | $countries = new Countries($five_minute_date_range, 10); |
| 49 | $country_rows = \array_map(function ($row, $index) { |
| 50 | return ['id' => $row->country(), 'position' => $index + 1, 'title' => $row->country(), 'views' => $row->views(), 'flag' => \IAWP\Icon_Directory_Factory::flags()->find($row->country_code())]; |
| 51 | }, $countries->rows(), \array_keys($countries->rows())); |
| 52 | $campaigns = new Campaigns($five_minute_date_range, 10); |
| 53 | $campaign_rows = \array_map(function ($row, $index) { |
| 54 | return ['id' => $row->params(), 'position' => $index + 1, 'title' => $row->utm_campaign(), 'views' => $row->views()]; |
| 55 | }, $campaigns->rows(), \array_keys($campaigns->rows())); |
| 56 | $device_types = new Device_Types($five_minute_date_range, 10); |
| 57 | $device_rows = \array_map(function ($row, $index) { |
| 58 | return ['id' => $row->device_type(), 'position' => $index + 1, 'title' => $row->device_type(), 'views' => $row->views()]; |
| 59 | }, $device_types->rows(), \array_keys($device_types->rows())); |
| 60 | $visitor_message = $this->get_visitor_count_message($current_traffic->get_visitor_count()); |
| 61 | $page_message = $this->get_count_message($current_traffic->get_page_count(), \__('Page', 'independent-analytics'), \__('Pages', 'independent-analytics')); |
| 62 | $referrer_message = $this->get_count_message($current_traffic->get_referrer_count(), \__('Referrer', 'independent-analytics'), \__('Referrers', 'independent-analytics')); |
| 63 | $country_message = $this->get_count_message($current_traffic->get_country_count(), \__('Country', 'independent-analytics'), \__('Countries', 'independent-analytics')); |
| 64 | 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', 'independent-analytics'), 'entries' => $page_rows], 'referrers' => ['title' => \__('Active Referrers', 'independent-analytics'), 'entries' => $referrer_rows], 'countries' => ['title' => \__('Active Countries', 'independent-analytics'), 'entries' => $country_rows], 'campaigns' => ['title' => \__('Active Campaigns', 'independent-analytics'), 'entries' => $campaign_rows], 'device_types' => ['title' => \__('Device Types', 'independent-analytics'), 'entries' => $device_rows]]]; |
| 65 | } |
| 66 | public function render_real_time_analytics() |
| 67 | { |
| 68 | echo \IAWPSCOPED\iawp_blade()->run('real_time', $this->get_real_time_analytics()); |
| 69 | } |
| 70 | private function get_count_message(int $count, string $singular, string $plural) : string |
| 71 | { |
| 72 | return \number_format_i18n($count) . ' ' . \_n($singular, $plural, $count); |
| 73 | } |
| 74 | private function get_visitor_count_message(int $count) : string |
| 75 | { |
| 76 | return $this->get_count_message($count, \__('Active Visitor', 'independent-analytics'), \__('Active Visitors', 'independent-analytics')); |
| 77 | } |
| 78 | private function round_up_by_seconds(DateTime $datetime, $precision_seconds) : DateTime |
| 79 | { |
| 80 | $datetime = clone $datetime; |
| 81 | $datetime->setTimestamp($precision_seconds * (int) \ceil($datetime->getTimestamp() / $precision_seconds)); |
| 82 | return $datetime; |
| 83 | } |
| 84 | } |
| 85 |