Ejemplo n.º 1
0
/**
 * 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  string  $dec_point     Separator for the decimal point
 * @param  string  $thousands_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_db_get_datas($query, 'OBJECT');
    /* Set the count vars. */
    $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 conversion rate. */
    $rate = 0 === $conversions || 0 === $impressions ? 0 : $conversions * 100 / $impressions;
    return number_format($rate, $decimals, $dec_point, $thousands_sep);
}
Ejemplo n.º 2
0
?>
</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_db_get_datas(array('popup_id' => $data->popup_id, 'data_type' => 'impression', 'limit' => -1, 'period' => $timeframe), 'ARRAY_A');
        $conversions = wpbo_db_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', 'betteroptin') : __('Inactive', 'betteroptin');
        $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;
        ?>
Ejemplo n.º 3
0
/**
 * Retrieve data to feed the graph.
 *
 * @since  1.0.0
 * @return string Records encoded in JSON
 */
function wpbo_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_db_get_datas($query_i, 'OBJECT');
    $conversions = wpbo_db_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'));
            $max = strtotime(date('Y-m-d 23:59:59'));
            break;
        case 'this_week':
            $scale = 'Y-m-d 00:00:00';
            $timeformat = '%a';
            $minticksize = array(1, 'day');
            $min = strtotime('last monday');
            $max = strtotime('next sunday');
            break;
        case 'last_week':
            $scale = 'Y-m-d 00:00:00';
            $timeformat = '%a';
            $minticksize = array(1, 'day');
            $min = strtotime('last monday -7 days');
            $max = strtotime('next sunday -7 days');
            break;
        case 'this_month':
            $scale = 'Y-m-d 00:00:00';
            $timeformat = '%a';
            $minticksize = array(1, 'day');
            $min = strtotime('first day of this month');
            $max = strtotime('last day of this month');
            break;
        case 'last_month':
            $scale = 'Y-m-d 00:00:00';
            $timeformat = '%a';
            $minticksize = array(1, 'day');
            $min = strtotime('first day of last month');
            $max = strtotime('last day of last month');
            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);
            $max = strtotime('last day of this month', strtotime('+2 months', $current));
            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);
            $max = strtotime('last day of this month', strtotime('+2 months', $current));
            break;
        case 'this_year':
            $scale = 'Y-m-d 00:00:00';
            $timeformat = '%b';
            $minticksize = array(1, 'month');
            $min = strtotime('first day of January', time());
            $max = strtotime('last day of December', time());
            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());
            $max = strtotime('last day of December last year', time());
            break;
    }
    /* Propare global array */
    $datas = array('impressionsData' => array('label' => __('Impressions', 'betteroptin'), 'id' => 'impressions', 'data' => array()), 'conversionsData' => array('label' => __('Conversions', 'betteroptin'), 'id' => 'conversions', 'data' => array()), 'scale' => array('minTickSize' => $minticksize, 'timeformat' => $timeformat), 'min' => $min * 1000, 'max' => $max * 1000);
    /* Get the count on the scaled timestamp */
    $imp_array = wpbo_array_merge_combine($impressions, $scale);
    $con_array = wpbo_array_merge_combine($conversions, $scale);
    // Get a complete data array for the given timeframe (meaning there is a value for evey single time point)
    $increment = "{$minticksize['0']} {$minticksize['1']}";
    // Increment formatted to be used in strtotime()
    $imp_array = wpbo_fill_hits_period($imp_array, $min, $max, $increment, $scale);
    $con_array = wpbo_fill_hits_period($con_array, $min, $max, $increment, $scale);
    /* Add the hits to datas array */
    $datas['impressionsData']['data'] = $imp_array;
    $datas['conversionsData']['data'] = $con_array;
    /* Return results to script */
    echo json_encode($datas);
    die;
}