Initial commit: Atomaste website
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) or die( "you do not have access to this page!" );
|
||||
|
||||
if ( ! class_exists( "burst_goal_statistics" ) ) {
|
||||
class burst_goal_statistics {
|
||||
function __construct() {
|
||||
add_action( 'init', array( $this, 'init' ), 10, 3 );
|
||||
}
|
||||
|
||||
public function init() {
|
||||
}
|
||||
|
||||
public function get_goal_id( $goal_id ) {
|
||||
global $wpdb;
|
||||
$goal_id = (int) $goal_id ?: 0;
|
||||
if ( ! $goal_id ) {
|
||||
// get first active goal from db
|
||||
$goal_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->prefix}burst_goals WHERE status = 'active' ORDER BY ID LIMIT 1" );
|
||||
}
|
||||
if ( ! $goal_id ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $goal_id;
|
||||
}
|
||||
|
||||
public function get_live_goals_data( $args = [] ): int {
|
||||
global $wpdb;
|
||||
|
||||
$goal_id = $this->get_goal_id( $args['goal_id'] );
|
||||
$today = strtotime( 'today midnight' );
|
||||
require_once burst_path . 'goals/class-goal.php';
|
||||
$goal = new burst_goal($goal_id);
|
||||
$goal_url = $goal->url;
|
||||
$goal_url_sql = $goal_url === '' || $goal_url === '*' ? '' : $wpdb->prepare( 'AND statistics.page_url = %s', $goal_url );
|
||||
|
||||
$sql = $wpdb->prepare("SELECT COUNT(*)
|
||||
FROM {$wpdb->prefix}burst_statistics as statistics
|
||||
INNER JOIN {$wpdb->prefix}burst_goal_statistics as goals
|
||||
ON statistics.ID = goals.statistic_id
|
||||
WHERE statistics.bounce = 0 AND goals.goal_id = %d AND statistics.time > %d {$goal_url_sql}", $goal_id, $today );
|
||||
$val = $wpdb->get_var( $sql );
|
||||
|
||||
return (int) $val ?: 0;
|
||||
}
|
||||
|
||||
public function get_goals_data( $args = array() ): array {
|
||||
global $wpdb;
|
||||
|
||||
// Define default arguments
|
||||
$defaults = array(
|
||||
'date_start' => 0,
|
||||
'date_end' => 0,
|
||||
'url' => '',
|
||||
'goal_id' => 0,
|
||||
);
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
// Sanitize input
|
||||
$goal_id = (int) $this->get_goal_id( $args['goal_id'] );
|
||||
require_once burst_path . 'goals/class-goal.php';
|
||||
$goal = new burst_goal($goal_id);
|
||||
$goal_url = $goal->url;
|
||||
$goal_start = $goal->date_start;
|
||||
$goal_end = $goal->date_end;
|
||||
$goal_created =$goal->date_created;
|
||||
$status = $goal->status;
|
||||
$goal_type = $goal->type;
|
||||
$goal_conversion_metric = $goal->conversion_metric;
|
||||
|
||||
// Initialize data array
|
||||
$data = array();
|
||||
//this data is always empty, but is needed clientside to prevent errors (crashes when not available).
|
||||
$data['today'] = array( 'value' => 0, 'tooltip' => '' );
|
||||
$data['total'] = array( 'value' => 0, 'tooltip' => '' );
|
||||
$data['topPerformer'] = array(
|
||||
'title' => '-',
|
||||
'value' => 0,
|
||||
'tooltip' => __( 'Top performing page', 'burst-statistics' ),
|
||||
);
|
||||
// Conversion metric visitors
|
||||
if ( $goal_conversion_metric === 'pageviews' ) {
|
||||
$data['conversionMetric'] = array(
|
||||
'title' => __( 'Pageviews', 'burst-statistics' ),
|
||||
'value' => 0,
|
||||
'tooltip' => '',
|
||||
'icon' => 'pageviews'
|
||||
);
|
||||
$conversion_metric_select = 'COUNT(*)';
|
||||
} else if ( $goal_conversion_metric === 'sessions' ) {
|
||||
$data['conversionMetric'] = array(
|
||||
'title' => __( 'Sessions', 'burst-statistics' ),
|
||||
'value' => 0,
|
||||
'tooltip' => '',
|
||||
'icon' => 'sessions'
|
||||
);
|
||||
$conversion_metric_select = "COUNT(DISTINCT(statistics.session_id))";
|
||||
} else { // visitors
|
||||
$data['conversionMetric'] = array(
|
||||
'title' => __( 'Visitors', 'burst-statistics' ),
|
||||
'value' => 0,
|
||||
'tooltip' => '',
|
||||
'icon' => 'visitors'
|
||||
);
|
||||
$conversion_metric_select = "COUNT(DISTINCT(statistics.uid))";
|
||||
}
|
||||
$data['conversionPercentage'] = array(
|
||||
'title' => __( 'Conversion rate', 'burst-statistics' ),
|
||||
'value' => 0,
|
||||
'tooltip' => ''
|
||||
);
|
||||
$data['bestDevice'] = array(
|
||||
'title' => __( 'Not enough data', 'burst-statistics' ),
|
||||
'value' => 0,
|
||||
'tooltip' => __( 'Best performing device', 'burst-statistics' ),
|
||||
'icon' => 'desktop'
|
||||
);
|
||||
$data['dateCreated'] = $goal_created;
|
||||
$data['dateStart'] = $goal_start;
|
||||
$data['dateEnd'] = $goal_end;
|
||||
$data['status'] = $status;
|
||||
$data['goalId'] = $goal_id;
|
||||
|
||||
if ( $goal_id !== 0 ) {
|
||||
// Query to get total number of goal completions
|
||||
|
||||
$goal_end_sql = $goal_end > 0 ? $wpdb->prepare("AND statistics.time < %s", $goal_end) : '';
|
||||
$goal_url_sql = $goal_url === '' || $goal_url === '*' || $goal_type === 'visits' ? '' : $wpdb->prepare( 'AND statistics.page_url = %s', $goal_url );
|
||||
$total_sql = $wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}burst_statistics AS statistics
|
||||
INNER JOIN {$wpdb->prefix}burst_goal_statistics AS goals
|
||||
ON statistics.ID = goals.statistic_id
|
||||
WHERE statistics.bounce = 0 AND goals.goal_id = %s AND statistics.time > %s {$goal_end_sql} {$goal_url_sql}", $goal_id, $goal_start);
|
||||
|
||||
$data['total']['value'] = $wpdb->get_var( $total_sql );
|
||||
|
||||
// Query to get top performing page
|
||||
|
||||
$top_performer_sql = $wpdb->prepare("SELECT COUNT(*) AS value, statistics.page_url AS title FROM {$wpdb->prefix}burst_statistics AS statistics
|
||||
INNER JOIN {$wpdb->prefix}burst_goal_statistics AS goals
|
||||
ON statistics.ID = goals.statistic_id
|
||||
WHERE statistics.bounce = 0 AND goals.goal_id = %s AND statistics.time > %s {$goal_end_sql} {$goal_url_sql}
|
||||
GROUP BY statistics.page_url ORDER BY COUNT(*) DESC LIMIT 1", $goal_id, $goal_start);
|
||||
$top_performer_result = $wpdb->get_row( $top_performer_sql );
|
||||
if ( $top_performer_result ) {
|
||||
$data['topPerformer']['title'] = $top_performer_result->title;
|
||||
$data['topPerformer']['value'] = $top_performer_result->value;
|
||||
}
|
||||
|
||||
// Query to get total number of visitors, sessions or pageviews with get_sql_table
|
||||
$conversionMetric = $wpdb->prepare("SELECT {$conversion_metric_select} FROM {$wpdb->prefix}burst_statistics as statistics
|
||||
WHERE statistics.time > %s {$goal_end_sql} AND statistics.bounce = 0 {$goal_url_sql}", $goal_start);
|
||||
$data['conversionMetric']['value'] = $wpdb->get_var( $conversionMetric );
|
||||
|
||||
// Query to get best performing device
|
||||
$use_lookup_tables = BURST()->statistics->use_lookup_tables(); //during upgrade to new lookupt tables.
|
||||
$device_column = $use_lookup_tables ? 'device_id' : 'device';
|
||||
$device_sql = $wpdb->prepare("SELECT COUNT(*) AS value, statistics.$device_column AS device_id FROM {$wpdb->prefix}burst_statistics AS statistics
|
||||
INNER JOIN {$wpdb->prefix}burst_goal_statistics AS goals
|
||||
ON statistics.ID = goals.statistic_id
|
||||
WHERE statistics.bounce = 0 AND goals.goal_id = %s AND statistics.time > %s {$goal_end_sql} {$goal_url_sql}
|
||||
GROUP BY statistics.device_id ORDER BY value DESC LIMIT 4", $goal_id, $goal_start);
|
||||
$device_result = $wpdb->get_results( $device_sql );
|
||||
|
||||
$pageviews_per_device = $wpdb->prepare("SELECT COUNT(*) AS value, $device_column as device_id FROM {$wpdb->prefix}burst_statistics as statistics
|
||||
WHERE statistics.bounce = 0 AND statistics.time > %s {$goal_end_sql} {$goal_url_sql}
|
||||
GROUP BY statistics.device_id ORDER BY value DESC LIMIT 4", $goal_start);
|
||||
|
||||
|
||||
$pageviews_per_device_result = $wpdb->get_results($pageviews_per_device);
|
||||
|
||||
// calculate conversion rate and select the highest percentage
|
||||
$highest_percentage = 0;
|
||||
foreach ( $device_result as $device ) {
|
||||
foreach ( $pageviews_per_device_result as $pageviews_per_device ) {
|
||||
if ( $device->device_id === $pageviews_per_device->device_id ) {
|
||||
$device_id = $use_lookup_tables ? BURST()->frontend->get_lookup_table_id( $device->device_id, 'device' ) : $device->device_id;
|
||||
$percentage = round( ( $device->value / $pageviews_per_device->value ) * 100, 2 );
|
||||
if ( $percentage > $highest_percentage ) {
|
||||
$highest_percentage = $percentage;
|
||||
$data['bestDevice']['title'] = $this->get_device_name($device_id);
|
||||
$data['bestDevice']['icon'] = $device;
|
||||
$data['bestDevice']['value'] = $percentage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get translatable device name based on device type
|
||||
* @param $device
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_device_name(
|
||||
$device
|
||||
): ?string {
|
||||
switch ( $device ) {
|
||||
case 'desktop':
|
||||
$device_name = __( 'Desktop', 'burst-statistics' );
|
||||
break;
|
||||
case 'mobile':
|
||||
$device_name = __( 'Mobile', 'burst-statistics' );
|
||||
break;
|
||||
case 'tablet':
|
||||
$device_name = __( 'Tablet', 'burst-statistics' );
|
||||
break;
|
||||
case 'other':
|
||||
default:
|
||||
$device_name = __( 'Other', 'burst-statistics' );
|
||||
break;
|
||||
}
|
||||
|
||||
return $device_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Install goal statistic table
|
||||
* */
|
||||
|
||||
add_action( 'burst_install_tables', 'burst_install_goal_statistics_table', 10 );
|
||||
function burst_install_goal_statistics_table() {
|
||||
if ( get_option( 'burst_goal_stats_db_version' ) !== burst_version ) {
|
||||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||
|
||||
global $wpdb;
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$table_name = $wpdb->prefix . 'burst_goal_statistics';
|
||||
$sql = "CREATE TABLE $table_name (
|
||||
`ID` int NOT NULL AUTO_INCREMENT,
|
||||
`statistic_id` int NOT NULL,
|
||||
`goal_id` int NOT NULL,
|
||||
PRIMARY KEY (ID),
|
||||
KEY `statistic_id_index` (statistic_id),
|
||||
KEY `goal_id_index` (goal_id)
|
||||
) $charset_collate;";
|
||||
/**
|
||||
* We use b-tree index as it can be used for < or > operations, which is not possible for HASH
|
||||
*/
|
||||
dbDelta( $sql );
|
||||
update_option( 'burst_goal_stats_db_version', burst_version, false );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,488 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) or die();
|
||||
|
||||
if ( ! class_exists( 'burst_summary' ) ) {
|
||||
class burst_summary {
|
||||
function __construct() {
|
||||
add_action( 'burst_every_hour', array( $this, 'update_summary_table_today' ) );
|
||||
add_action( 'burst_weekly', array( $this, 'update_is_high_traffic' ) );
|
||||
add_action( 'burst_daily', array( $this, 'update_post_meta' ) );
|
||||
add_action( 'burst_upgrade_post_meta', array( $this, 'update_post_meta' ) );
|
||||
add_filter( 'burst_do_action', array( $this, 'refresh_data' ), 10, 3 );
|
||||
add_filter( 'burst_notices', array( $this, 'add_cron_warning'));
|
||||
|
||||
if ( defined( 'BURST_RESTART_SUMMARY_UPGRADE' ) && BURST_RESTART_SUMMARY_UPGRADE ) {
|
||||
$this->restart_update_summary_table_alltime();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $warnings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_cron_warning( $warnings ){
|
||||
//if this option is still here, don't add the warning just yet.
|
||||
if ( $this->cron_active() ) {
|
||||
return $warnings;
|
||||
}
|
||||
|
||||
if ( !$this->is_high_traffic() ) {
|
||||
return $warnings;
|
||||
}
|
||||
|
||||
$warnings['cron'] = array(
|
||||
'callback' => '_true_',
|
||||
'status' => 'all',
|
||||
'output' => array(
|
||||
'true' => array(
|
||||
'msg' => __( 'Because your cron has not been triggered more than 24 hours, Burst has stopped using the summary tables, which allow the dashboard to load faster.', 'burst-statistics' ),
|
||||
'icon' => 'warning',
|
||||
'url' => burst_get_website_url('/instructions/cron-error/', [
|
||||
'burst_source' => 'notices',
|
||||
'burst_content ' => 'cron-error'
|
||||
]),
|
||||
'dismissible' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
return $warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param string $action
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function refresh_data( array $output, string $action, $data ): array {
|
||||
if ( ! burst_user_can_manage() ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
if ( $action === 'refresh_data' ) {
|
||||
$this->update_summary_table();
|
||||
$output['success'] = true;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if all items are summary data
|
||||
* @param array $items
|
||||
* @param array $filters
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_summary_data( array $items, array $filters, int $start, int $end ): bool {
|
||||
if ( ! empty( $filters ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//if the difference between start and end is below x months, we don't use summary data.
|
||||
$three_months = 3 * MONTH_IN_SECONDS;
|
||||
$range = $end - $start;
|
||||
if ( $range < $three_months ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if end is today, we can't be sure that the data is complete, so we don't use summary data.
|
||||
$today_end = BURST()->statistics->convert_date_to_unix( date( 'Y-m-d' ) . ' 23:59:59' );
|
||||
if ( $end === $today_end ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$summary_items = [
|
||||
'page_url',
|
||||
'sessions',
|
||||
'pageviews',
|
||||
'bounce_rate',
|
||||
'visitors',
|
||||
'first_time_visitors',
|
||||
'bounces',
|
||||
'avg_time_on_page',
|
||||
];
|
||||
foreach ( $items as $item ) {
|
||||
if ( ! in_array( $item, $summary_items, true ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart the summary table update
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restart_update_summary_table_alltime() {
|
||||
global $wpdb;
|
||||
|
||||
$table_names = array( $wpdb->prefix . 'burst_summary' );
|
||||
|
||||
foreach ( $table_names as $table_name ) {
|
||||
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ) {
|
||||
$wpdb->query( "TRUNCATE TABLE $table_name" );
|
||||
}
|
||||
}
|
||||
|
||||
update_option( 'burst_db_upgrade_summary_table', true, false );
|
||||
delete_option( 'burst_summary_table_upgrade_days_offset' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the summary upgrade has been completed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function upgrade_completed(): bool {
|
||||
if ( !$this->is_high_traffic() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if option set to never use summary tables, return false for upgrade completed.
|
||||
if ( defined( 'BURST_DONT_USE_SUMMARY_TABLE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( burst_get_option('disable_summary') ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! get_option( 'burst_db_upgrade_summary_table' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Once a week, update the status of the site, if it is high traffic or not. If over 100K hits in the last month, it is considered high traffic.
|
||||
* In that case, summary tables will be used to speed up the dashboard.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function update_is_high_traffic(){
|
||||
global $wpdb;
|
||||
|
||||
$start_of_last_month = strtotime('first day of last month midnight');
|
||||
$end_of_last_month = strtotime('last day of last month 23:59:59');
|
||||
$sql = $wpdb->prepare("select count(*) from {$wpdb->prefix}burst_statistics where time>=%s and time<=%s", $start_of_last_month, $end_of_last_month);
|
||||
$count = (int) $wpdb->get_var($sql);
|
||||
$is_high_traffic = $count > apply_filters( 'burst_high_traffic_treshold', 100000 );
|
||||
update_option( 'burst_is_high_traffic_site', $is_high_traffic, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the site is considered high traffic
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_high_traffic(){
|
||||
return get_option( 'burst_is_high_traffic_site');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the cron has run the last 24 hours
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function cron_active(): bool {
|
||||
$now = time();
|
||||
$last_cron_hit = get_option( 'burst_last_cron_hit', 0 );
|
||||
$diff = $now - $last_cron_hit;
|
||||
|
||||
return $diff <= DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade the data to summary table, progressing over all days until the last one is completed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function upgrade_summary_table_alltime() {
|
||||
global $wpdb;
|
||||
$first_statistics_date_unix = $wpdb->get_var( "select min(time) from {$wpdb->prefix}burst_statistics" ); // 1644260876
|
||||
// convert unix to date and back to unix, to ensure that the date is at the start of the day, for comparison purposes
|
||||
$first_statistics_date = BURST()->statistics->convert_unix_to_date( $first_statistics_date_unix ); // 2022-02-07
|
||||
// calculate days offset from first_statistics_date to today
|
||||
$first_statistics_date_unix = strtotime( $first_statistics_date );
|
||||
$today = BURST()->statistics->convert_unix_to_date( strtotime( 'today' ) );
|
||||
$max_days_offset = ( strtotime( $today ) - $first_statistics_date_unix ) / DAY_IN_SECONDS;
|
||||
// round to integer
|
||||
$max_days_offset = round( $max_days_offset, 0 );
|
||||
// if the offset is negative, set it to 0
|
||||
$max_days_offset = $max_days_offset < 0 ? 0 : $max_days_offset;
|
||||
$current_days_offset = (int) get_option( 'burst_summary_table_upgrade_days_offset', 0 );
|
||||
// check if the oldest summary date is more recent than the oldest statistics date
|
||||
// we ensure that it will always run for today, by running if offset = 0.
|
||||
if ( $max_days_offset >= $current_days_offset ) {
|
||||
for ( $i = 0; $i < 30; $i++ ) {
|
||||
$success = $this->update_summary_table( $current_days_offset );
|
||||
// if failed, exit, and try again later
|
||||
if ( ! $success ) {
|
||||
return;
|
||||
}
|
||||
// if successful, increment days offset
|
||||
++$current_days_offset;
|
||||
}
|
||||
|
||||
update_option( 'burst_summary_table_upgrade_days_offset', $current_days_offset, false );
|
||||
//schedule next run
|
||||
wp_schedule_single_event(time() + MINUTE_IN_SECONDS , "burst_upgrade_iteration");
|
||||
|
||||
} else {
|
||||
// completed
|
||||
delete_option( 'burst_db_upgrade_summary_table' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run scheduled to update today's summary data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function update_summary_table_today() {
|
||||
if ( ! $this->cron_active() ) {
|
||||
burst_update_option('disable_summary', true);
|
||||
}
|
||||
update_option( 'burst_last_cron_hit', time(), false );
|
||||
// we want to update for yesterday at least once on the next day, to ensure completeness. If completed, continue with normal update process
|
||||
if ( ! $this->summary_table_updated_yesterday() ) {
|
||||
$this->update_summary_table( 1 );
|
||||
}
|
||||
|
||||
// update for today
|
||||
$this->update_summary_table();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if yesterday's summary was marked as completed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function summary_table_updated_yesterday(): bool {
|
||||
global $wpdb;
|
||||
$yesterday = BURST()->statistics->convert_unix_to_date( strtotime( 'yesterday' ) );
|
||||
$completed = $wpdb->get_var( $wpdb->prepare( "select completed from {$wpdb->prefix}burst_summary where date = %s", $yesterday ) );
|
||||
return (bool) $completed;
|
||||
}
|
||||
|
||||
|
||||
public function summary_sql( $date_start, $date_end, $select_array, $group_by = '', $order_by = '', $limit = '', $date_modifiers = false ) {
|
||||
$date_start = BURST()->statistics->convert_unix_to_date( $date_start );
|
||||
$date_end = BURST()->statistics->convert_unix_to_date( $date_end );
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$select = [];
|
||||
$select[] = $date_modifiers ? "DATE_FORMAT(date, '{$date_modifiers['sql_date_format']}') as period" : 'page_url';
|
||||
$sql_array = [
|
||||
'bounce_rate' => 'sum(bounces) / count(sessions) * 100 as bounce_rate',
|
||||
'pageviews' => 'sum(pageviews) as pageviews',
|
||||
'visitors' => 'sum(visitors) as visitors',
|
||||
'sessions' => 'sum(sessions) as sessions',
|
||||
'first_time_visitors' => 'sum(first_time_visitors) as first_time_visitors',
|
||||
'bounces' => 'sum(bounces) as bounces',
|
||||
'avg_time_on_page' => 'AVG(avg_time_on_page) as avg_time_on_page',
|
||||
];
|
||||
|
||||
foreach ( $select_array as $select_item ) {
|
||||
if ( isset( $sql_array[ $select_item ] ) ) {
|
||||
$select[] = $sql_array[ $select_item ];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$sql = implode( ', ', $select );
|
||||
$date_start = esc_sql( $date_start );
|
||||
$date_end = esc_sql( $date_end );
|
||||
$sql = "select $sql from {$wpdb->prefix}burst_summary where date>='$date_start' AND date<='$date_end'";
|
||||
|
||||
// page_urls include unique visitors, which overlap with the same unique visitors from other page_urls.
|
||||
// we can't just sum up all unique visitors from all page_urls, so if the page_url is not a selector, we get the day total.
|
||||
if ( ! in_array( 'page_url', $select_array, true ) ) {
|
||||
$sql .= " AND page_url='burst_day_total'";
|
||||
} else {
|
||||
$sql .= " AND page_url!='burst_day_total'";
|
||||
}
|
||||
|
||||
if ( ! empty( $group_by ) ) {
|
||||
$sql .= ' group by ' . $group_by;
|
||||
}
|
||||
|
||||
if ( ! empty( $order_by ) ) {
|
||||
$sql .= ' order by ' . $order_by;
|
||||
}
|
||||
|
||||
if ( ! empty( $limit ) ) {
|
||||
$sql .= ' ' . $limit;
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the summary table for one day.
|
||||
*
|
||||
* @param int $days_offset
|
||||
*/
|
||||
public function update_summary_table( int $days_offset = 0 ): bool {
|
||||
if ( get_transient( 'burst_updating_summary_table' ) ) {
|
||||
return false;
|
||||
}
|
||||
set_transient( 'burst_updating_summary_table', 5 * MINUTE_IN_SECONDS );
|
||||
global $wpdb;
|
||||
$today = BURST()->statistics->convert_unix_to_date( strtotime( 'today' ) );
|
||||
// deduct days offset in days
|
||||
if ( $days_offset > 0 ) {
|
||||
$today = BURST()->statistics->convert_unix_to_date( strtotime( $today . ' -' . $days_offset . ' days' ) );
|
||||
}
|
||||
// get start of today in unix
|
||||
$date_start = BURST()->statistics->convert_date_to_unix( $today . ' 00:00:00' );
|
||||
// get end of today in unix
|
||||
$date_end = BURST()->statistics->convert_date_to_unix( $today . ' 23:59:59' );
|
||||
// get today's date
|
||||
// get the summary from the statistics table
|
||||
$select_sql = BURST()->statistics->get_sql_table_raw(
|
||||
$date_start,
|
||||
$date_end,
|
||||
array(
|
||||
'pageviews',
|
||||
'visitors',
|
||||
'first_time_visitors',
|
||||
'page_url',
|
||||
'bounces',
|
||||
'sessions',
|
||||
'avg_time_on_page',
|
||||
),
|
||||
array(),
|
||||
'page_url'
|
||||
);
|
||||
// if this is the update for yesterday or before, mark it as completed
|
||||
$completed = $days_offset !== 0 ? 1 : 0;
|
||||
$update_sql = $wpdb->prepare(
|
||||
"INSERT INTO {$wpdb->prefix}burst_summary (date, page_url, sessions, pageviews, visitors, first_time_visitors, bounces, avg_time_on_page, completed)
|
||||
SELECT
|
||||
%s AS date,
|
||||
source.page_url,
|
||||
source.sessions,
|
||||
source.pageviews,
|
||||
source.visitors,
|
||||
source.first_time_visitors,
|
||||
source.bounces,
|
||||
source.avg_time_on_page,
|
||||
%s AS completed
|
||||
FROM (
|
||||
$select_sql
|
||||
) AS source
|
||||
ON DUPLICATE KEY UPDATE
|
||||
date = date,
|
||||
page_url = source.page_url,
|
||||
sessions = source.sessions,
|
||||
pageviews = source.pageviews,
|
||||
visitors = source.visitors,
|
||||
first_time_visitors = COALESCE(source.first_time_visitors, 0),
|
||||
bounces = source.bounces,
|
||||
avg_time_on_page = source.avg_time_on_page,
|
||||
completed = completed;",
|
||||
$today,
|
||||
$completed
|
||||
);
|
||||
$wpdb->query( $update_sql );
|
||||
|
||||
// we also create the day total for this day.
|
||||
$select_sql = BURST()->statistics->get_sql_table_raw(
|
||||
$date_start,
|
||||
$date_end,
|
||||
array(
|
||||
'pageviews',
|
||||
'visitors',
|
||||
'first_time_visitors',
|
||||
'bounces',
|
||||
'sessions',
|
||||
'avg_time_on_page',
|
||||
)
|
||||
);
|
||||
$update_sql = $wpdb->prepare(
|
||||
"INSERT INTO {$wpdb->prefix}burst_summary (date, page_url, sessions, pageviews, visitors, first_time_visitors, bounces, avg_time_on_page, completed)
|
||||
SELECT
|
||||
%s AS date,
|
||||
'burst_day_total' as page_url,
|
||||
source.sessions,
|
||||
source.pageviews,
|
||||
source.visitors,
|
||||
source.first_time_visitors,
|
||||
source.bounces,
|
||||
source.avg_time_on_page,
|
||||
%s AS completed
|
||||
FROM (
|
||||
$select_sql
|
||||
) AS source
|
||||
ON DUPLICATE KEY UPDATE
|
||||
date = date,
|
||||
page_url = page_url,
|
||||
sessions = source.sessions,
|
||||
pageviews = source.pageviews,
|
||||
visitors = source.visitors,
|
||||
first_time_visitors = COALESCE(source.first_time_visitors, 0),
|
||||
bounces = source.bounces,
|
||||
avg_time_on_page = COALESCE(source.avg_time_on_page, 0),
|
||||
completed = completed;",
|
||||
$today,
|
||||
$completed
|
||||
);
|
||||
|
||||
$wpdb->query( $update_sql );
|
||||
|
||||
delete_transient( 'burst_updating_summary_table' );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the summary table for one day.
|
||||
*
|
||||
* */
|
||||
public function update_post_meta( ) {
|
||||
if ( defined( 'BURST_HEADLESS' ) || burst_get_option( 'headless' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$days_offset = 1;
|
||||
$chunk = 50;
|
||||
global $wpdb;
|
||||
$today = BURST()->statistics->convert_unix_to_date( strtotime( 'today' ) );
|
||||
// deduct days offset in days
|
||||
if ( $days_offset > 0 ) {
|
||||
$today = BURST()->statistics->convert_unix_to_date( strtotime( $today . ' -' . $days_offset . ' days' ) );
|
||||
}
|
||||
$offset = (int) get_option('burst_post_meta_offset', 0);
|
||||
//if this is the update for yesterday, we also update the postmeta values for each post that has changed.
|
||||
|
||||
//get all posts that have received visits yesterday from the summary table
|
||||
$sql = "select * from {$wpdb->prefix}burst_summary where date = %s and page_url != 'burst_day_total' LIMIT $chunk OFFSET %d";
|
||||
$pages = $wpdb->get_results( $wpdb->prepare( $sql, $today, $offset ) );
|
||||
$pages = is_array($pages) ? $pages : [];
|
||||
$offset += $chunk;
|
||||
if ( count ( $pages ) === 0 ) {
|
||||
delete_option('burst_post_meta_offset');
|
||||
wp_clear_scheduled_hook("burst_upgrade_post_meta");
|
||||
} else {
|
||||
update_option('burst_post_meta_offset', $offset, false);
|
||||
wp_schedule_single_event(time() + MINUTE_IN_SECONDS , 'burst_upgrade_post_meta' );
|
||||
foreach ( $pages as $page ) {
|
||||
$url = home_url() . $page->page_url;
|
||||
$post_id = url_to_postid($url);
|
||||
if ( $post_id === 0 ) {
|
||||
continue;
|
||||
}
|
||||
$count = (int) get_post_meta($post_id, 'burst_total_pageviews_count', true);
|
||||
$count += (int) $page->pageviews;
|
||||
update_post_meta($post_id, 'burst_total_pageviews_count', $count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// Silence is golden.
|
||||
Reference in New Issue
Block a user