<?php
/**
* Flow
*
* @package cartflows
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Analytics reports class.
*/
class Cartflows_Pro_Analytics_Reports {
/**
* Member Variable
*
* @var object instance
*/
private static $instance;
/**
* Flow orders
*
* @var array flow_orders
*/
private static $flow_orders = array();
/**
* Flow gross sell
*
* @var int flow_gross
*/
private static $flow_gross = 0;
/**
* Flow visits
*
* @var array flow_visits
*/
private static $flow_visits = array();
/**
* Steps data
*
* @var array step_data
*/
private static $step_data = array();
/**
* Earnings for flow
*
* @var array flow_earnings
*/
private static $flow_earnings = array();
/**
* Report interval
*
* @var int report_interval
*/
private static $report_interval = 30;
/**
* Initiator
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor function that initializes required actions and hooks
*/
public function __construct() {
$this->load_analytics();
}
/**
*
* Analytics
*
* @return void
*/
public function load_analytics() {
add_action( 'cartflows_add_flow_metabox', array( $this, 'add_analytics_metabox' ) );
$flow_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0;
$analytics = wcf()->options->get_flow_meta_value( $flow_id, 'wcf-enable-analytics' );
if ( 'no' === $analytics ) {
return;
}
add_action( 'admin_enqueue_scripts', array( $this, 'load_analytics_scripts' ), 20 );
add_action( 'wp_ajax_cartflows_set_visit_data', array( $this, 'set_visits_data' ) );
add_action( 'admin_footer', array( $this, 'render_analytics_stat' ) );
add_action( 'wp_ajax_wcf_reset_flow_analytics', array( $this, 'reset_flow_analytics' ) );
add_filter( 'cartflows_admin_js_localize', array( $this, 'add_localize_vars_for_analytics' ), 10, 1 );
}
/**
*
* Localize variables for analytics.
*
* @param array $localize vars.
*/
public function add_localize_vars_for_analytics( $localize ) {
$localize['wcf_reset_analytics_nonce'] = wp_create_nonce( 'wcf_reset_flow_analytics' );
$localize['confirm_msg_for_analytics'] = __( 'Are you sure you want to reset the analytics for this flow?', 'cartflows-pro' );
$localize['succesful_msg_for_analytics'] = __( 'Flow analytics data has been reset successfully.', 'cartflows-pro' );
$flow_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0;
$localize['flow_id'] = $flow_id;
return $localize;
}
/**
*
* Reset ANalytics.
*
* @return void
*/
public function reset_flow_analytics() {
check_ajax_referer( 'wcf_reset_flow_analytics', 'security' );
global $wpdb;
$visit_db = $wpdb->prefix . CARTFLOWS_PRO_VISITS_TABLE;
$visits_meta_db = $wpdb->prefix . CARTFLOWS_PRO_VISITS_META_TABLE;
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : 0;
if ( 0 !== $flow_id ) {
$get_steps = wcf()->flow->get_steps( $flow_id );
$step_ids = implode( ',', wp_list_pluck( $get_steps, 'id' ) );
$wpdb->query( "DELETE FROM {$visit_db} WHERE step_id IN(" . $step_ids . ')' ); //phpcs:ignore
$reset_date = current_time( 'Y-m-d H:i:s' );
update_post_meta( $flow_id, 'wcf-analytics-reset-date', $reset_date );
wp_send_json_success( true );
}
wp_send_json_error( false );
}
/**
*
* Add Analytics Metabox
*
* @return void
*/
public function add_analytics_metabox() {
add_meta_box(
'wcf-analytics-settings', // Id.
__( 'Analytics', 'cartflows-pro' ), // Title.
array( $this, 'analytics_metabox_markup' ), // Callback.
CARTFLOWS_FLOW_POST_TYPE, // Post_type.
'side', // Context.
'high' // Priority.
);
}
/**
* Analytics Metabox Markup
*
* @param array $post post data.
* @return void
*/
public function analytics_metabox_markup( $post ) {
?>
<div class="wcf-flow-sandbox-table wcf-general-metabox-wrap widefat">
<div class="wcf-flow-enable-analytics">
<?php
$meta_intance = Cartflows_Flow_Meta::get_instance();
$meta_data = $meta_intance->get_current_post_meta( $post->ID );
foreach ( $meta_data as $key => $value ) {
$options[ $key ] = $meta_data[ $key ]['default'];
}
echo wcf()->meta->get_checkbox_field(
array(
'name' => 'wcf-enable-analytics',
'value' => $options['wcf-enable-analytics'],
'after' => esc_html__( 'Enable Flow Analytics', 'cartflows-pro' ),
)
);
?>
</div>
<div class="wcf-flow-sandbox-table-container">
<?php
echo wcf()->meta->get_description_field(
array(
'name' => 'wcf-analytics-note',
'content' => esc_html__( 'Analytics offers data that helps you understand how your flows are performing.', 'cartflows-pro' ),
)
);
?>
<div class="button-wrap">
<?php
$this->setup_analytics_button();
$this->reset_analytics_button();
?>
</div>
</div>
</div>
<?php
}
/**
* Render analytics display button beside title.
*/
public function setup_analytics_button() {
if ( ! Cartflows_Admin::is_flow_edit_admin() ) {
return;
}
$reports_btn_markup = '<style>.wrap{ position:relative;}</style>';
$reports_btn_markup .= "<div class='wcf-reports-button-wrap'>";
$reports_btn_markup .= "<button class='wcf-trigger-reports-popup button button-secondary'>";
$reports_btn_markup .= esc_html__( 'View Analytics', 'cartflows-pro' );
$reports_btn_markup .= '</button>';
$reports_btn_markup .= '</div>';
echo $reports_btn_markup;
}
/**
*
* Add Analytics reset button.
*
* @return void
*/
public function reset_analytics_button() {
if ( ! Cartflows_Admin::is_flow_edit_admin() ) {
return;
}
$reset_btn_markup = '<style>.wrap{ position:relative;}</style>';
$reset_btn_markup .= "<div class='wcf-reset-button-wrap'>";
$reset_btn_markup .= "<button class='wcf-reset-analytics button button-secondary' id='wcf-reset-analytics-button' data-process='" . esc_html__( 'Resetting', 'cartflows-pro' ) . "'>";
$reset_btn_markup .= esc_html__( 'Reset Analytics', 'cartflows-pro' );
$reset_btn_markup .= '</button>';
$reset_btn_markup .= '</div>';
echo $reset_btn_markup;
}
/**
* Set visits data for later use in analytics.
*/
public function set_visits_data() {
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : 0;
$earning = $this->get_earnings( $flow_id );
$visits = $this->fetch_visits( $flow_id );
$all_data = $this->visits_map( $flow_id, $visits, $earning );
$response = array(
'revenue' => $earning,
'all_steps' => $all_data,
);
wp_send_json_success( $response );
}
/**
* Display analytics stat table.
*/
public function render_analytics_stat() {
if ( ! Cartflows_Admin::is_flow_edit_admin() ) {
return;
}
$currency_symbol = '';
if ( wcf_pro()->is_woo_active ) {
if ( function_exists( 'get_woocommerce_currency_symbol' ) ) {
$currency_symbol = get_woocommerce_currency_symbol();
}
}
include CARTFLOWS_TRACKING_DIR . '/view/analytics-template.php';
include CARTFLOWS_TRACKING_DIR . '/view/analytics-popup.php';
}
/**
* Visits map.
*
* @param int $flow_id flow id.
* @param array $visits visits data.
* @param array $earning earning data.
* @return array
*/
public function visits_map( $flow_id, $visits, $earning ) {
$visits_map = array();
foreach ( $visits as $v_in => $v_data ) {
$step_id = $v_data->step_id;
$v_data_array = (array) $v_data;
$visits_map[ $step_id ] = $v_data_array;
$step_type = wcf()->utils->get_step_type( $step_id );
$visits_map[ $step_id ]['revenue'] = 0;
$visits_map[ $step_id ]['title'] = get_the_title( $step_id );
$visits_map[ $step_id ]['note'] = get_post_meta( $step_id, 'wcf-step-note', true );
$visits_map[ $step_id ]['conversion_rate'] = 0;
// Set conversion rate.
$conversions = intval( $v_data_array['conversions'] );
$total_visits = intval( $v_data_array['total_visits'] );
if ( $total_visits > 0 ) {
$conversion_rate = $conversions / intval( $v_data_array['total_visits'] ) * 100;
$visits_map[ $step_id ]['conversion_rate'] = number_format( (float) $conversion_rate, 2, '.', '' );
}
switch ( $step_type ) {
case 'checkout':
$visits_map[ $step_id ]['revenue'] = 0;
if ( isset( $earning['checkout'][ $step_id ] ) ) {
$visits_map[ $step_id ]['revenue'] = $earning['checkout'][ $step_id ];
Loading ...