// Always more KPIs

This commit is contained in:
Damien Metzger
2013-09-17 12:47:13 +02:00
parent a0be556795
commit f7855f2a3a
6 changed files with 148 additions and 77 deletions
@@ -1,57 +0,0 @@
{*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
{extends file="helpers/list/list_header.tpl"}
{block name='leadin'}
<div class="panel">
<div class="row">
<div id="box-clients" class="col-lg-3 box-stats color1" >
<div class="boxchart-overlay">
<div class="boxchart"></div>
</div>
<span class="title">Conversion Rate <br/><small>30 days</small></span>
<span class="value">1.89%</span>
</div>
</div>
</div>
<script>
var data = [4, 8, 15, 16, 23, 42, 8, 15, 16, 23, 42, 16, 23, 42, 8];
var chart = d3.select("#box-clients .boxchart").append("svg")
.attr("class", "data_chart")
.attr("width", data.length * 6)
.attr("height", 45);
var y = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, d3.max(data)]);
chart.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("y", function(d) { return 45 - d; })
.attr("x", function(d, i) { return i * 6; })
.attr("width", 4)
.attr("height", y);
</script>
{/block}
@@ -23,22 +23,70 @@
* International Registered Trademark & Property of PrestaShop SA
*}
<div id="{$id|escape}" class="col-lg-3 box-stats {$color|escape}" >
<i class="{$icon|escape}"></i>
{if $icon}<i class="{$icon|escape}"></i>{/if}
{if $chart}
<div class="boxchart-overlay">
<div class="boxchart">
</div>
</div>
{/if}
<span class="title">{$title|escape}<br /><small>{$subtitle|escape}</small></span>
<span class="value">{$value|escape}</span>
</div>
{if $source != ''}
<script>
$.ajax({
url: '{$source|addslashes}' + '&rand=' + new Date().getTime(),
dataType: 'json',
type: 'GET',
cache: false,
headers: { 'cache-control': 'no-cache' },
success: function(jsonData){
if (!jsonData.has_errors)
$('#{$id|addslashes} .value').html(jsonData.value);
$.ajax({
url: '{$source|addslashes}' + '&rand=' + new Date().getTime(),
dataType: 'json',
type: 'GET',
cache: false,
headers: { 'cache-control': 'no-cache' },
success: function(jsonData){
if (!jsonData.has_errors)
{
if (jsonData.value)
$('#{$id|addslashes} .value').html(jsonData.value);
if (jsonData.data)
{
$("#{$id|addslashes} .boxchart svg").remove();
set_d3_{$id|str_replace:'-':'_'|addslashes}(jsonData.data);
}
}
}
});
</script>
{/if}
{if $chart}
<script>
function set_d3_{$id|str_replace:'-':'_'|addslashes}(jsonObject)
{
var data = new Array;
$.each(jsonObject, function (index, value) {
data.push(value);
});
var chart = d3.select("#{$id|addslashes} .boxchart").append("svg")
.attr("class", "data_chart")
.attr("width", data.length * 6)
.attr("height", 45);
var y = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, d3.max(data) * 45]);
chart.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("y", function(d) { return 45 - d * 45 / d3.max(data); })
.attr("x", function(d, i) { return i * 6; })
.attr("width", 4)
.attr("height", y);
}
});
{if $data}
set_d3_{$id|str_replace:'-':'_'|addslashes}($.parseJSON("{$data|addslashes}"));
{/if}
</script>
{/if}
+4
View File
@@ -31,10 +31,12 @@ class HelperKpiCore extends Helper
public $id;
public $icon;
public $chart;
public $color;
public $title;
public $subtitle;
public $value;
public $data;
public $source;
public function generate()
@@ -44,10 +46,12 @@ class HelperKpiCore extends Helper
$this->tpl->assign(array(
'id' => $this->id,
'icon' => $this->icon,
'chart' => (bool)$this->chart,
'color' => $this->color,
'title' => $this->title,
'subtitle' => $this->subtitle,
'value' => $this->value,
'data' => $this->data,
'source' => $this->source
));
return $this->tpl->fetch();
+18 -4
View File
@@ -1245,7 +1245,21 @@ class AdminOrdersControllerCore extends AdminController
$kpis = array();
/* The data generation is located in AdminStatsControllerCore */
$helper = new HelperKpi();
$helper->id = 'box-conversion-rate';
$helper->chart = true;
$helper->color = 'color1';
$helper->title = $this->l('Conversion Rate');
$helper->subtitle = $this->l('30 days');
if (Configuration::get('PS_KPI_CONVERSION_RATE') !== false)
$helper->value = Configuration::get('PS_KPI_CONVERSION_RATE');
if (Configuration::get('PS_KPI_CONVERSION_RATE_CHART') !== false)
$helper->data = Configuration::get('PS_KPI_CONVERSION_RATE_CHART');
if (Configuration::get('PS_KPI_CONVERSION_RATE_EXPIRE') < $time)
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=conversion_rate';
$kpis[] = $helper->generate();
$helper = new HelperKpi();
$helper->id = 'box-carts';
$helper->icon = 'icon-shopping-cart';
@@ -1257,7 +1271,7 @@ class AdminOrdersControllerCore extends AdminController
if (Configuration::get('PS_KPI_ABANDONED_CARTS_EXPIRE') < $time)
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=abandoned_cart';
$kpis[] = $helper->generate();
$helper = new HelperKpi();
$helper->id = 'box-average-order';
$helper->icon = 'icon-money';
@@ -1269,12 +1283,12 @@ class AdminOrdersControllerCore extends AdminController
if (Configuration::get('PS_KPI_AVG_ORDER_VALUE_EXPIRE') < $time)
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=average_order_value';
$kpis[] = $helper->generate();
$helper = new HelperKpi();
$helper->id = 'box-net-profit-visitor';
$helper->icon = 'icon-user';
$helper->color = 'color4';
$helper->title = $this->l('Net Profit per Visitor ');
$helper->title = $this->l('Net Profit per Visitor');
$helper->subtitle = $this->l('30 days');
if (Configuration::get('PS_KPI_NETPROFIT_VISITOR') !== false)
$helper->value = Configuration::get('PS_KPI_NETPROFIT_VISITOR');
+65 -3
View File
@@ -31,6 +31,64 @@ class AdminStatsControllerCore extends AdminStatsTabController
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
switch (Tools::getValue('kpi'))
{
case 'conversion_rate':
$visits = array();
$gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false;
if (Validate::isLoadedObject($gapi) && $gapi->isConfigured())
{
if ($result = $gapi->requestReportData('ga:date', 'ga:visits', date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), null, null, 1, 30))
foreach ($result as $row)
$visits[strtotime(preg_replace('/^([0-9]{4})([0-9]{2})([0-9]{2})$/', '$1-$2-$3', $row['dimensions']['date']))] = $row['metrics']['visits'];
}
else
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT
LEFT(`date_add`, 10) as date,
COUNT(*) as visits
FROM `'._DB_PREFIX_.'connections`
WHERE `date_add` BETWEEN "'.pSQL(date('Y-m-d', strtotime('-31 day'))).' 00:00:00" AND "'.pSQL(date('Y-m-d', strtotime('-1 day'))).' 23:59:59"
'.Shop::addSqlRestriction(false).'
GROUP BY LEFT(`date_add`, 10)');
foreach ($result as $row)
$visits[strtotime($row['date'])] = $row['visits'];
}
$orders = array();
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT
LEFT(`invoice_date`, 10) as date,
COUNT(*) as orders
FROM `'._DB_PREFIX_.'orders`
WHERE `invoice_date` BETWEEN "'.pSQL(date('Y-m-d', strtotime('-31 day'))).' 00:00:00" AND "'.pSQL(date('Y-m-d', strtotime('-1 day'))).' 23:59:59"
'.Shop::addSqlRestriction(Shop::SHARE_ORDER).'
GROUP BY LEFT(`invoice_date`, 10)');
foreach ($result as $row)
$orders[strtotime($row['date'])] = $row['orders'];
$data = array();
$from = strtotime(date('Y-m-d 00:00:00', strtotime('-31 day')));
$to = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day')));
for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date))
{
$data[$date] = 0;
if (isset($visits[$date]) && $visits[$date])
$data[$date] = round(100 * ((isset($orders[$date]) && $orders[$date]) ? $orders[$date] : 0) / $visits[$date], 2);
}
$visits_sum = array_sum($visits);
$orders_sum = array_sum($orders);
if ($visits_sum)
$value = round(100 * $orders_sum / $visits_sum, 2);
elseif ($orders_sum)
$value = '&infin;';
else
$value = 0;
$value .= '%';
Configuration::updateValue('PS_KPI_CONVERSION_RATE_CHART', Tools::jsonEncode($data));
Configuration::updateValue('PS_KPI_CONVERSION_RATE', $value);
Configuration::updateValue('PS_KPI_CONVERSION_RATE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day'))));
break;
case 'abandoned_cart':
$value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
@@ -54,7 +112,6 @@ class AdminStatsControllerCore extends AdminStatsTabController
Configuration::updateValue('PS_KPI_AVG_ORDER_VALUE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day'))));
break;
case 'netprofit_visitor':
$gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false;
if (Validate::isLoadedObject($gapi) && $gapi->isConfigured())
{
@@ -68,7 +125,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
SELECT COUNT(DISTINCT id_guest)
FROM `'._DB_PREFIX_.'connections`
WHERE `date_add` BETWEEN "'.pSQL(date('Y-m-d', strtotime('-31 day'))).' 00:00:00" AND "'.pSQL(date('Y-m-d', strtotime('-1 day'))).' 23:59:59"
'.Shop::addSqlRestriction(false).'');
'.Shop::addSqlRestriction(false));
}
$row_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
@@ -114,7 +171,12 @@ class AdminStatsControllerCore extends AdminStatsTabController
$value = false;
}
if ($value !== false)
die(Tools::jsonEncode(array('value' => $value)));
{
$array = array('value' => $value);
if (isset($data))
$array['data'] = $data;
die(Tools::jsonEncode($array));
}
die(Tools::jsonEncode(array('has_errors' => true)));
}
}
+2 -2
View File
@@ -102,7 +102,7 @@ class Dashtrends extends Module
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT
LEFT(`date_add`, 10) as date,
COUNT(`id_connections`) as visits_score
COUNT(*) as visits_score
FROM `'._DB_PREFIX_.'connections`
WHERE `date_add` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'"
'.Shop::addSqlRestriction(false).'
@@ -115,7 +115,7 @@ class Dashtrends extends Module
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT
LEFT(`invoice_date`, 10) as date,
COUNT(`id_order`) as orders_score,
COUNT(*) as orders_score,
SUM(`total_paid_tax_excl` / `conversion_rate`) as total_paid_tax_excl,
SUM(`total_discounts_tax_excl` / `conversion_rate`) as total_discounts_tax_excl
FROM `'._DB_PREFIX_.'orders`