/** * Today's Conversion Rate. * * Get today's conversion rate using the stats class. * * @since 1.2.2 * @param integer $decimals Number of decimal to return for the conversion rate * @param integer $dec_point Separator for the decimal point * @param integer $thousnads_sep Separator for the thousands * @return integer Conversion rate for the day */ function wpbo_today_conversion($decimals = 2, $dec_point = '.', $thousands_sep = ',') { /* Prepare the query. */ $query = array('data_type' => 'any', 'limit' => -1, 'period' => strtotime('today')); /* Get the datas. */ $datas = wpbo_get_datas($query, 'OBJECT'); /* Set the count vars. */ $total = count($datas); $impressions = 0; $conversions = 0; /* Check the number of conversions. */ foreach ($datas as $data) { /* Increment conversions */ if ('conversion' == $data->data_type) { ++$conversions; } /* Increment impressions */ if ('impression' == $data->data_type) { ++$impressions; } } /* Get the converison rate. */ $rate = 0 === $conversions || 0 === $impressions ? 0 : $conversions * 100 / $impressions; return number_format($rate, $decimals, $dec_point, $thousands_sep); }
/** * Retrieve data to feed the graph. * * @since 1.0.0 * @return string Records encoded in JSON */ public function get_graph_data() { $query = array('data_type' => 'any', 'limit' => -1); $timeframe = unserialize(stripslashes($_POST['wpbo_analytics_time'])); $popup = isset($_POST['wpbo_analytics_popup']) ? $_POST['wpbo_analytics_popup'] : 'all'; $period = isset($_POST['wpbo_analytics_period']) ? $_POST['wpbo_analytics_period'] : 'today'; /* Set the period */ $query['period'] = $timeframe; /* Select the popup */ if ('all' != $popup) { $query['popup_id'] = intval($popup); } /* Separate impressions and conversions */ $query_i = $query; $query_i['data_type'] = 'impression'; $query_c = $query; $query_c['data_type'] = 'conversion'; /* Get the datas */ $impressions = wpbo_get_datas($query_i, 'OBJECT'); $conversions = wpbo_get_datas($query_c, 'OBJECT'); /* Set the scale */ $scale = date('Y-m-d'); switch ($period) { case 'today': $scale = 'Y-m-d H:00:00'; $timeformat = '%d/%b'; $minticksize = array(1, 'hour'); $min = strtotime(date('Y-m-d 00:00:00')) * 1000; $max = strtotime(date('Y-m-d 23:59:59')) * 1000; break; case 'this_week': $scale = 'Y-m-d 00:00:00'; $timeformat = '%a'; $minticksize = array(1, 'day'); $min = strtotime('last monday') * 1000; $max = strtotime('next sunday') * 1000; break; case 'last_week': $scale = 'Y-m-d 00:00:00'; $timeformat = '%a'; $minticksize = array(1, 'day'); $min = strtotime('last monday -7 days') * 1000; $max = strtotime('next sunday -7 days') * 1000; break; case 'this_month': $scale = 'Y-m-d 00:00:00'; $timeformat = '%a'; $minticksize = array(1, 'day'); $min = strtotime('first day of this month') * 1000; $max = strtotime('last day of this month') * 1000; break; case 'last_month': $scale = 'Y-m-d 00:00:00'; $timeformat = '%a'; $minticksize = array(1, 'day'); $min = strtotime('first day of last month') * 1000; $max = strtotime('last day of last month') * 1000; break; case 'this_quarter': $scale = 'Y-m-d 00:00:00'; $timeformat = '%b'; $minticksize = array(1, 'month'); $quarters = array(1, 4, 7, 10); $month = intval(date('m')); if (in_array($month, $quarters)) { $current = date('Y-m-d', time()); } else { /* Get first month of this quarter */ while (!in_array($month, $quarters)) { $month = $month - 1; } $current = date('Y') . '-' . $month . '-' . '01'; } $current = strtotime($current); $min = strtotime('first day of this month', $current) * 1000; $max = strtotime('last day of this month', strtotime('+2 months', $current)) * 1000; break; case 'last_quarter': $scale = 'Y-m-d 00:00:00'; $timeformat = '%b'; $minticksize = array(1, 'month'); $quarters = array(1, 4, 7, 10); $month = intval(date('m')) - 3; $rewind = false; if (in_array($month, $quarters)) { $current = date('Y-m-d', time()); } else { /* Get first month of this quarter */ while (!in_array($month, $quarters)) { $month = $month - 1; /* Rewind to last year after we passed January */ if (0 === $month) { $month = 12; } } $current = date('Y') . '-' . $month . '-' . '01'; } /* Set the theorical current date */ $current = false === $rewind ? strtotime($current) : strtotime('-1 year', $current); $min = strtotime('first day of this month', $current) * 1000; $max = strtotime('last day of this month', strtotime('+2 months', $current)) * 1000; break; case 'this_year': $scale = 'Y-m-d 00:00:00'; $timeformat = '%b'; $minticksize = array(1, 'month'); $min = strtotime('first day of January', time()) * 1000; $max = strtotime('last day of December', time()) * 1000; break; case 'last_year': $scale = 'Y-m-d 00:00:00'; $timeformat = '%b'; $minticksize = array(1, 'month'); $min = strtotime('first day of January last year', time()) * 1000; $max = strtotime('last day of December last year', time()) * 1000; break; } /* Propare global array */ $datas = array('impressionsData' => array('label' => __('Impressions', 'wpbo'), 'id' => 'impressions', 'data' => array()), 'conversionsData' => array('label' => __('Conversions', 'wpbo'), 'id' => 'conversions', 'data' => array()), 'scale' => array('minTickSize' => $minticksize, 'timeformat' => $timeformat), 'min' => $min, 'max' => $max); /* Get the count on the scaled timestamp */ $imp_array = $this->array_merge_combine($impressions, $scale); $con_array = $this->array_merge_combine($conversions, $scale); /** * Fill the blanks! * * Both impressions and conversions array need to have the same number of entries * (same number of timestamps) for the graph to work properly. * * We alternatively merge the impressions and conversions array. The only added keys * must have a value of 0. */ $tmp_arr_imp = array_flip(array_keys($imp_array)); $tmp_arr_con = array_flip(array_keys($con_array)); /* Set all counts to 0 */ $tmp_arr_imp = array_map(array('Better_Optin_Admin', 'return_zero'), $tmp_arr_imp); $tmp_arr_con = array_map(array('Better_Optin_Admin', 'return_zero'), $tmp_arr_con); /* Add missing values in both impressions and conversions arrays */ $imp_array = $imp_array + $tmp_arr_con; $con_array = $con_array + $tmp_arr_imp; /* Convert the arrays to a format that Float can read. */ $imp_array = $this->float_format($imp_array); $con_array = $this->float_format($con_array); /* Add the hits to datas array */ $datas['impressionsData']['data'] = $imp_array; $datas['conversionsData']['data'] = $con_array; /* Return results to script */ print_r(json_encode($datas)); die; }
?> </th> </tr> </thead> <tbody> <?php if (is_array($datas)) { foreach ($datas as $key => $data) { if (in_array($data->popup_id, $seen)) { continue; } /* Mark this popup as parsed */ array_push($seen, $data->popup_id); $popup = get_post($data->popup_id); $impressions = wpbo_get_datas(array('popup_id' => $data->popup_id, 'data_type' => 'impression', 'limit' => -1, 'period' => $timeframe), 'ARRAY_A'); $conversions = wpbo_get_datas(array('popup_id' => $data->popup_id, 'data_type' => 'conversion', 'limit' => -1, 'period' => $timeframe), 'ARRAY_A'); $rate = 0 === count($conversions) || 0 === count($impressions) ? 0 : 100 * count($conversions) / count($impressions); $status = 'publish' == $popup->post_status ? __('Active', 'wpbo') : __('Inactive', 'wpbo'); $status_class = 'publish' == $popup->post_status ? 'wpbo-stats-active' : 'wpbo-stats-inactive'; /* Increment the global vars */ $total_impressions = $total_impressions + count($impressions); $total_conversions = $total_conversions + count($conversions); ?> <tr valign="top"> <td scope="row"><a href="<?php echo add_query_arg(array('post' => $data->popup_id, 'action' => 'edit'), admin_url('post.php')); ?> "><?php echo $popup->post_title; ?>