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