/**
* Dashboard Analytics for Onboarding Steps Enhanced
*
* @package Onboarding_Steps_Enhanced
*/
/**
* Class for handling dashboard analytics functionality
*/
class Onboarding_Steps_Dashboard_Analytics {
/**
* Initialize the class
*/
public function __construct() {
// Register hooks
add_action('admin_menu', array($this, 'add_analytics_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_analytics_scripts'));
add_action('wp_ajax_get_onboarding_analytics', array($this, 'get_onboarding_analytics'));
add_action('wp_ajax_get_step_completion_data', array($this, 'get_step_completion_data'));
add_action('wp_ajax_get_conversion_funnel_data', array($this, 'get_conversion_funnel_data'));
add_action('wp_ajax_get_time_to_completion_data', array($this, 'get_time_to_completion_data'));
}
/**
* Add analytics menu
*/
public function add_analytics_menu() {
// This is handled by the main plugin class now
}
/**
* Enqueue analytics scripts
*/
public function enqueue_analytics_scripts($hook) {
// Only load on analytics page
if (strpos($hook, 'onboarding-steps-enhanced-analytics') === false) {
return;
}
// Enqueue Chart.js
wp_enqueue_script(
'chartjs',
'https://cdn.jsdelivr.net/npm/chart.js@3.7.1/dist/chart.min.js',
array(),
'3.7.1',
true
);
// Enqueue analytics CSS
wp_enqueue_style(
'onboarding-steps-analytics-style',
ONBOARDING_STEPS_ENHANCED_PLUGIN_URL . 'assets/css/admin/dashboard-analytics.css',
array(),
ONBOARDING_STEPS_ENHANCED_VERSION
);
// Enqueue analytics JavaScript
wp_enqueue_script(
'onboarding-steps-analytics-script',
ONBOARDING_STEPS_ENHANCED_PLUGIN_URL . 'assets/js/admin/dashboard-analytics.js',
array('jquery', 'chartjs'),
ONBOARDING_STEPS_ENHANCED_VERSION,
true
);
// Localize script with AJAX URL and nonce
wp_localize_script('onboarding-steps-analytics-script', 'onboardingAnalytics', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('onboarding_steps_analytics_nonce')
));
}
/**
* Render analytics page
*/
public function render_page() {
// Check if user has permission
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.', 'onboarding-steps-enhanced'));
}
// Get dashboard settings
$settings = get_option('onboarding_steps_dashboard_settings', array(
'show_completion_rate' => 1,
'show_conversion_funnel' => 1,
'show_time_metrics' => 1,
'show_user_segments' => 1,
'refresh_interval' => 60
));
// Output dashboard
echo '
';
echo '
Onboarding Analytics Dashboard
';
// Dashboard controls
echo '
';
echo '
';
echo '';
echo '';
echo '
';
echo '
';
echo '';
echo '';
echo '';
echo '
';
echo '
';
// Dashboard widgets
echo '
';
// Summary stats
echo '
';
echo '
Summary Statistics
';
echo '
';
echo '
Total Users
Loading...
';
echo '
Active Users
Loading...
';
echo '
Completion Rate
Loading...
';
echo '
Avg. Completion Time
Loading...
';
echo '
';
echo '
';
// Step completion chart
if ($settings['show_completion_rate']) {
echo '
';
echo '
Step Completion Rates
';
echo '
';
echo '';
echo '
';
echo '
';
}
// Conversion funnel
if ($settings['show_conversion_funnel']) {
echo '
';
echo '
Conversion Funnel
';
echo '
';
echo '';
echo '
';
echo '
';
}
// Time to completion
if ($settings['show_time_metrics']) {
echo '
';
echo '
Time to Completion
';
echo '
';
echo '';
echo '
';
echo '
';
}
// User segments
if ($settings['show_user_segments']) {
echo '
';
echo '
User Segments
';
echo '
';
echo '';
echo '
';
echo '
';
}
echo '
'; // End dashboard-widgets
echo '
'; // End wrap
}
/**
* AJAX handler for getting onboarding analytics
*/
public function get_onboarding_analytics() {
// Check nonce
check_ajax_referer('onboarding_steps_analytics_nonce', 'nonce');
// Check if user has permission
if (!current_user_can('manage_options')) {
wp_send_json_error('Permission denied');
return;
}
// Get date range
$days = isset($_POST['days']) ? intval($_POST['days']) : 30;
// Get summary stats
$stats = $this->get_summary_stats($days);
wp_send_json_success($stats);
}
/**
* AJAX handler for getting step completion data
*/
public function get_step_completion_data() {
// Check nonce
check_ajax_referer('onboarding_steps_analytics_nonce', 'nonce');
// Check if user has permission
if (!current_user_can('manage_options')) {
wp_send_json_error('Permission denied');
return;
}
// Get date range
$days = isset($_POST['days']) ? intval($_POST['days']) : 30;
// Get step completion data
$data = $this->get_step_completion_stats($days);
wp_send_json_success($data);
}
/**
* AJAX handler for getting conversion funnel data
*/
public function get_conversion_funnel_data() {
// Check nonce
check_ajax_referer('onboarding_steps_analytics_nonce', 'nonce');
// Check if user has permission
if (!current_user_can('manage_options')) {
wp_send_json_error('Permission denied');
return;
}
// Get date range
$days = isset($_POST['days']) ? intval($_POST['days']) : 30;
// Get conversion funnel data
$data = $this->get_conversion_funnel_stats($days);
wp_send_json_success($data);
}
/**
* AJAX handler for getting time to completion data
*/
public function get_time_to_completion_data() {
// Check nonce
check_ajax_referer('onboarding_steps_analytics_nonce', 'nonce');
// Check if user has permission
if (!current_user_can('manage_options')) {
wp_send_json_error('Permission denied');
return;
}
// Get date range
$days = isset($_POST['days']) ? intval($_POST['days']) : 30;
// Get time to completion data
$data = $this->get_time_to_completion_stats($days);
wp_send_json_success($data);
}
/**
* Get summary statistics
*
* @param int $days Number of days to include in stats
* @return array Summary statistics
*/
private function get_summary_stats($days) {
global $wpdb;
// Calculate date limit
$date_limit = '';
if ($days > 0) {
$date_limit = "AND created_at >= DATE_SUB(NOW(), INTERVAL $days DAY)";
}
// Get total users
$total_users = count_users();
// Get active users (users with any progress)
$progress_table = $wpdb->prefix . 'onboarding_progress';
$active_users = $wpdb->get_var("
SELECT COUNT(DISTINCT user_id)
FROM $progress_table
WHERE 1=1 $date_limit
");
// Get completion rate (users who completed all steps)
$completion_rate = 0;
if ($active_users > 0) {
// Get total number of steps
$total_steps = 6; // Assuming 6 main steps
// Get users who completed all steps
$completed_users = $wpdb->get_var("
SELECT COUNT(DISTINCT user_id)
FROM (
SELECT user_id, COUNT(DISTINCT SUBSTRING_INDEX(step_id, '-', 1)) as completed_main_steps
FROM $progress_table
WHERE status = 1 $date_limit
GROUP BY user_id
HAVING completed_main_steps = $total_steps
) as completed
");
$completion_rate = round(($completed_users / $active_users) * 100, 1);
}
// Get average completion time
$avg_completion_time = 'N/A';
$avg_days = $wpdb->get_var("
SELECT AVG(DATEDIFF(MAX(updated_at), MIN(created_at))) as avg_days
FROM $progress_table
WHERE status = 1 $date_limit
GROUP BY user_id
HAVING COUNT(DISTINCT SUBSTRING_INDEX(step_id, '-', 1)) = 6
");
if ($avg_days !== null) {
$avg_completion_time = round($avg_days, 1) . ' days';
}
return array(
'total_users' => $total_users['total_users'],
'active_users' => $active_users,
'completion_rate' => $completion_rate . '%',
'avg_completion_time' => $avg_completion_time
);
}
/**
* Get step completion statistics
*
* @param int $days Number of days to include in stats
* @return array Step completion statistics
*/
private function get_step_completion_stats($days) {
global $wpdb;
// Calculate date limit
$date_limit = '';
if ($days > 0) {
$date_limit = "AND created_at >= DATE_SUB(NOW(), INTERVAL $days DAY)";
}
// Get active users (users with any progress)
$progress_table = $wpdb->prefix . 'onboarding_progress';
$active_users = $wpdb->get_var("
SELECT COUNT(DISTINCT user_id)
FROM $progress_table
WHERE 1=1 $date_limit
");
if ($active_users == 0) {
return array(
'labels' => array(),
'data' => array()
);
}
// Get completion counts for each main step
$step_data = $wpdb->get_results("
SELECT
SUBSTRING_INDEX(step_id, '-', 1) as step,
COUNT(DISTINCT user_id) as completed_users
FROM $progress_table
WHERE status = 1 $date_limit
GROUP BY step
ORDER BY step ASC
");
$labels = array();
$data = array();
// Step names
$step_names = array(
'1' => 'Create Account',
'2' => 'Verify Identity',
'3' => 'Link Bank Account',
'4' => 'Fund Account',
'5' => 'Complete Profile',
'6' => 'Accept EULA'
);
// Process data
foreach ($step_data as $row) {
$step_number = $row->step;
$step_name = isset($step_names[$step_number]) ? $step_names[$step_number] : 'Step ' . $step_number;
$labels[] = $step_name;
$data[] = round(($row->completed_users / $active_users) * 100, 1);
}
return array(
'labels' => $labels,
'data' => $data
);
}
/**
* Get conversion funnel statistics
*
* @param int $days Number of days to include in stats
* @return array Conversion funnel statistics
*/
private function get_conversion_funnel_stats($days) {
global $wpdb;
// Calculate date limit
$date_limit = '';
if ($days > 0) {
$date_limit = "AND created_at >= DATE_SUB(NOW(), INTERVAL $days DAY)";
}
// Get active users (users with any progress)
$progress_table = $wpdb->prefix . 'onboarding_progress';
$active_users = $wpdb->get_var("
SELECT COUNT(DISTINCT user_id)
FROM $progress_table
WHERE 1=1 $date_limit
");
if ($active_users == 0) {
return array(
'labels' => array(),
'data' => array()
);
}
// Step names
$step_names = array(
'1' => 'Create Account',
'2' => 'Verify Identity',
'3' => 'Link Bank Account',
'4' => 'Fund Account',
'5' => 'Complete Profile',
'6' => 'Accept EULA'
);
$labels = array();
$data = array();
// Get funnel data for each step
for ($i = 1; $i <= 6; $i++) {
$step_number = $i;
$step_name = isset($step_names[$step_number]) ? $step_names[$step_number] : 'Step ' . $step_number;
// Get users who completed this step
$completed_users = $wpdb->get_var("
SELECT COUNT(DISTINCT user_id)
FROM $progress_table
WHERE SUBSTRING_INDEX(step_id, '-', 1) = '$step_number'
AND status = 1 $date_limit
");
$labels[] = $step_name;
$data[] = $completed_users;
}
return array(
'labels' => $labels,
'data' => $data
);
}
/**
* Get time to completion statistics
*
* @param int $days Number of days to include in stats
* @return array Time to completion statistics
*/
private function get_time_to_completion_stats($days) {
global $wpdb;
// Calculate date limit
$date_limit = '';
if ($days > 0) {
$date_limit = "AND created_at >= DATE_SUB(NOW(), INTERVAL $days DAY)";
}
// Step names
$step_names = array(
'1' => 'Create Account',
'2' => 'Verify Identity',
'3' => 'Link Bank Account',
'4' => 'Fund Account',
'5' => 'Complete Profile',
'6' => 'Accept EULA'
);
$labels = array();
$data = array();
// Get time to completion for each step
$progress_table = $wpdb->prefix . 'onboarding_progress';
for ($i = 1; $i <= 6; $i++) {
$step_number = $i;
$step_name = isset($step_names[$step_number]) ? $step_names[$step_number] : 'Step ' . $step_number;
// Get average time to completion for this step
$avg_hours = $wpdb->get_var("
SELECT AVG(TIMESTAMPDIFF(HOUR, created_at, updated_at)) as avg_hours
FROM $progress_table
WHERE SUBSTRING_INDEX(step_id, '-', 1) = '$step_number'
AND status = 1
AND created_at != updated_at $date_limit
");
$labels[] = $step_name;
$data[] = $avg_hours !== null ? round($avg_hours, 1) : 0;
}
return array(
'labels' => $labels,
'data' => $data
);
}
}