AJAX
5 months ago
Admin_Page
5 months ago
Click_Tracking
5 months ago
ColumnOptions
6 months ago
Cron
9 months ago
Custom_WordPress_Columns
8 months ago
Data_Pruning
5 months ago
Date_Picker
5 months ago
Date_Range
5 months ago
Ecommerce
5 months ago
Email_Reports
5 months ago
Examiner
11 months ago
Favicon
5 months ago
Form_Submissions
5 months ago
Integrations
6 months ago
Interval
1 year ago
Journey
5 months ago
Migrations
5 months ago
Models
5 months ago
Overview
5 months ago
Public_API
5 months ago
Rows
5 months ago
Statistics
5 months ago
Tables
5 months ago
Utils
5 months ago
Views
5 months ago
WooCommerceOrderMetaBox
5 months ago
Admin_Bar_Stats.php
5 months ago
Appearance.php
1 year ago
Campaign_Builder.php
5 months ago
Capability_Manager.php
1 year ago
Chart.php
9 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
1 year ago
Cron_Job.php
5 months ago
Cron_Manager.php
5 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
6 months ago
Dashboard_Widget.php
2 years ago
Database.php
8 months ago
Database_Manager.php
5 months ago
Empty_Report_Option.php
2 years ago
Env.php
6 months ago
Examiner_Config.php
11 months ago
FetchFaviconsJob.php
5 months ago
Filters.php
5 months ago
Geo_Database_Health_Check_Job.php
9 months ago
Geo_Database_Manager.php
6 months ago
Geoposition.php
1 year ago
Icon_Directory.php
6 months ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
10 months ago
Independent_Analytics.php
5 months ago
Interrupt.php
1 year ago
Known_Referrers.php
6 months ago
MainWP.php
1 year ago
Map.php
9 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
5 months ago
Patch.php
1 year ago
Payload_Validator.php
9 months ago
Plugin_Conflict_Detector.php
6 months ago
Plugin_Group.php
6 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
5 months ago
Quick_Stats.php
11 months ago
REST_API.php
6 months ago
Real_Time.php
5 months ago
Report.php
1 year ago
Report_Finder.php
6 months ago
Report_Options_Parser.php
9 months ago
Resource_Identifier.php
9 months ago
Settings.php
6 months ago
Sort_Configuration.php
9 months ago
Tables.php
8 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
6 months ago
Views_Over_Time_Finder.php
1 year ago
VisitorSaltRefreshInterval.php
6 months ago
WP_Option_Cache_Bust.php
2 years ago
Real_Time.php
96 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\Tables\Table_Campaigns; |
| 15 | use IAWP\Tables\Table_Devices; |
| 16 | use IAWP\Tables\Table_Geo; |
| 17 | use IAWP\Tables\Table_Pages; |
| 18 | use IAWP\Tables\Table_Referrers; |
| 19 | use IAWP\Utils\Singleton; |
| 20 | use IAWP\Utils\Timezone; |
| 21 | /** @internal */ |
| 22 | class Real_Time |
| 23 | { |
| 24 | use Singleton; |
| 25 | public function __construct() |
| 26 | { |
| 27 | } |
| 28 | public function get_real_time_analytics() |
| 29 | { |
| 30 | $thirty_minutes_ago = new DateTime('-30 minutes', Timezone::utc_timezone()); |
| 31 | $thirty_minutes_ago = $this->round_up_by_seconds($thirty_minutes_ago, 60); |
| 32 | $five_minutes_ago = new DateTime('-5 minutes', Timezone::utc_timezone()); |
| 33 | $five_minutes_ago = $this->round_up_by_seconds($five_minutes_ago, 10); |
| 34 | $now = new DateTime('now', Timezone::utc_timezone()); |
| 35 | $end_minutes = $this->round_up_by_seconds($now, 60); |
| 36 | $end_seconds = $this->round_up_by_seconds($now, 10); |
| 37 | $visitors_by_minute_date_range = new Exact_Date_Range($thirty_minutes_ago, $end_minutes, \false); |
| 38 | $visitors_by_minute_finder = new \IAWP\Views_Over_Time_Finder($visitors_by_minute_date_range, new Minute_Interval()); |
| 39 | $visitors_by_minute = $visitors_by_minute_finder->fetch(); |
| 40 | $visitors_by_second_date_range = new Exact_Date_Range($five_minutes_ago, $end_seconds, \false); |
| 41 | $visitors_by_second_finder = new \IAWP\Views_Over_Time_Finder($visitors_by_second_date_range, new Ten_Second_Interval()); |
| 42 | $visitors_by_second = $visitors_by_second_finder->fetch(); |
| 43 | $five_minute_date_range = new Exact_Date_Range($five_minutes_ago, new DateTime('now', Timezone::utc_timezone()), \false); |
| 44 | $current_traffic_finder = new \IAWP\Current_Traffic_Finder($five_minute_date_range); |
| 45 | $current_traffic = $current_traffic_finder->fetch(); |
| 46 | $pages_table = new Table_Pages(); |
| 47 | $pages = new Pages($five_minute_date_range, $pages_table->sanitize_sort_parameters(), 10); |
| 48 | $page_rows = \array_map(function ($row, $index) { |
| 49 | return ['id' => $row->id(), 'position' => $index + 1, 'title' => $row->title(), 'views' => $row->views(), 'subtitle' => $row->most_popular_subtitle()]; |
| 50 | }, $pages->rows(), \array_keys($pages->rows())); |
| 51 | $referrers_table = new Table_Referrers(); |
| 52 | $referrers = new Referrers($five_minute_date_range, $referrers_table->sanitize_sort_parameters(), 10); |
| 53 | $referrer_rows = \array_map(function ($row, $index) { |
| 54 | return ['id' => $row->referrer(), 'position' => $index + 1, 'title' => $row->referrer(), 'views' => $row->views()]; |
| 55 | }, $referrers->rows(), \array_keys($referrers->rows())); |
| 56 | $geo_table = new Table_Geo(); |
| 57 | $countries = new Countries($five_minute_date_range, $geo_table->sanitize_sort_parameters(), 10); |
| 58 | $country_rows = \array_map(function ($row, $index) { |
| 59 | return ['id' => $row->country(), 'position' => $index + 1, 'title' => $row->country(), 'views' => $row->views(), 'flag' => \IAWP\Icon_Directory_Factory::flags()->find($row->country_code())]; |
| 60 | }, $countries->rows(), \array_keys($countries->rows())); |
| 61 | $campaigns_table = new Table_Campaigns(); |
| 62 | $campaigns = new Campaigns($five_minute_date_range, $campaigns_table->sanitize_sort_parameters(), 10); |
| 63 | $campaign_rows = \array_map(function ($row, $index) { |
| 64 | return ['id' => $row->params(), 'position' => $index + 1, 'title' => $row->utm_campaign(), 'views' => $row->views()]; |
| 65 | }, $campaigns->rows(), \array_keys($campaigns->rows())); |
| 66 | $devices_table = new Table_Devices(); |
| 67 | $device_types = new Device_Types($five_minute_date_range, $devices_table->sanitize_sort_parameters(), 10); |
| 68 | $device_rows = \array_map(function ($row, $index) { |
| 69 | return ['id' => $row->device_type(), 'position' => $index + 1, 'title' => $row->device_type(), 'views' => $row->views()]; |
| 70 | }, $device_types->rows(), \array_keys($device_types->rows())); |
| 71 | $visitor_message = $this->get_visitor_count_message($current_traffic->get_visitor_count()); |
| 72 | $page_message = $this->get_count_message($current_traffic->get_page_count(), \__('Page', 'independent-analytics'), \__('Pages', 'independent-analytics')); |
| 73 | $referrer_message = $this->get_count_message($current_traffic->get_referrer_count(), \__('Referrer', 'independent-analytics'), \__('Referrers', 'independent-analytics')); |
| 74 | $country_message = $this->get_count_message($current_traffic->get_country_count(), \__('Country', 'independent-analytics'), \__('Countries', 'independent-analytics')); |
| 75 | return ['visitor_message' => $visitor_message, 'page_message' => $page_message, 'referrer_message' => $referrer_message, 'country_message' => $country_message, 'chart_data' => ['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_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]]]; |
| 76 | } |
| 77 | public function render_real_time_analytics() |
| 78 | { |
| 79 | echo \IAWPSCOPED\iawp_blade()->run('real-time', $this->get_real_time_analytics()); |
| 80 | } |
| 81 | private function get_count_message(int $count, string $singular, string $plural) : string |
| 82 | { |
| 83 | return \number_format_i18n($count) . ' ' . \_n($singular, $plural, $count); |
| 84 | } |
| 85 | private function get_visitor_count_message(int $count) : string |
| 86 | { |
| 87 | return $this->get_count_message($count, \__('Active Visitor', 'independent-analytics'), \__('Active Visitors', 'independent-analytics')); |
| 88 | } |
| 89 | private function round_up_by_seconds(DateTime $datetime, $precision_seconds) : DateTime |
| 90 | { |
| 91 | $datetime = clone $datetime; |
| 92 | $datetime->setTimestamp($precision_seconds * (int) \ceil($datetime->getTimestamp() / $precision_seconds)); |
| 93 | return $datetime; |
| 94 | } |
| 95 | } |
| 96 |