// Dashgoals :)
This commit is contained in:
@@ -29,6 +29,9 @@ if (!defined('_PS_VERSION_'))
|
||||
|
||||
class Dashgoals extends Module
|
||||
{
|
||||
protected static $month_labels = array();
|
||||
protected static $types = array('traffic', 'conversion', 'avg_cart_value');
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->name = 'dashgoals';
|
||||
@@ -38,6 +41,21 @@ class Dashgoals extends Module
|
||||
$this->author = 'PrestaShop';
|
||||
|
||||
parent::__construct();
|
||||
|
||||
Dashgoals::$month_labels = array(
|
||||
'01' => $this->l('January'),
|
||||
'02' => $this->l('February'),
|
||||
'03' => $this->l('March'),
|
||||
'04' => $this->l('April'),
|
||||
'05' => $this->l('May'),
|
||||
'06' => $this->l('June'),
|
||||
'07' => $this->l('July'),
|
||||
'08' => $this->l('August'),
|
||||
'09' => $this->l('September'),
|
||||
'10' => $this->l('October'),
|
||||
'11' => $this->l('November'),
|
||||
'12' => $this->l('December')
|
||||
);
|
||||
}
|
||||
|
||||
public function install()
|
||||
@@ -59,37 +77,63 @@ class Dashgoals extends Module
|
||||
public function hookDashboardZoneTwo($params)
|
||||
{
|
||||
$year = date('Y');
|
||||
$this->context->smarty->assign('goals_months', array(
|
||||
'01_'.$year => sprintf($this->l('January, %s'), $year),
|
||||
'02_'.$year => sprintf($this->l('February, %s'), $year),
|
||||
'03_'.$year => sprintf($this->l('March, %s'), $year),
|
||||
'04_'.$year => sprintf($this->l('April, %s'), $year),
|
||||
'05_'.$year => sprintf($this->l('May, %s'), $year),
|
||||
'06_'.$year => sprintf($this->l('June, %s'), $year),
|
||||
'07_'.$year => sprintf($this->l('July, %s'), $year),
|
||||
'08_'.$year => sprintf($this->l('August, %s'), $year),
|
||||
'09_'.$year => sprintf($this->l('September, %s'), $year),
|
||||
'10_'.$year => sprintf($this->l('October, %s'), $year),
|
||||
'11_'.$year => sprintf($this->l('November, %s'), $year),
|
||||
'12_'.$year => sprintf($this->l('December, %s'), $year),
|
||||
));
|
||||
$months = array();
|
||||
for ($i = '01'; $i <= 12; $i = sprintf('%02d', $i + 1))
|
||||
$months[$i.'_'.$year] = array('label' => Dashgoals::$month_labels[$i], 'values' => array());
|
||||
|
||||
foreach (Dashgoals::$types as $type)
|
||||
foreach ($months as $month => &$month_row)
|
||||
{
|
||||
$key = 'dashgoals_'.$type.'_'.$month;
|
||||
if (Tools::isSubmit('submitDashGoals'))
|
||||
ConfigurationKPI::updateValue(strtoupper($key), (float)Tools::getValue($key));
|
||||
$month_row['values'][$type] = ConfigurationKPI::get(strtoupper($key));
|
||||
}
|
||||
|
||||
$this->context->smarty->assign('goals_year', $year);
|
||||
$this->context->smarty->assign('goals_months', $months);
|
||||
return $this->display(__FILE__, 'dashboard_zone_two.tpl');
|
||||
}
|
||||
|
||||
public function hookDashboardData($params)
|
||||
{
|
||||
return array();
|
||||
// 'data_chart' => array('dash_goals_chart1' => $this->getChartData()),
|
||||
// );
|
||||
return array(
|
||||
'data_chart' => array('dash_goals_chart1' => $this->getChartData()),
|
||||
);
|
||||
}
|
||||
|
||||
public function getChartData()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
$year = date('Y');
|
||||
|
||||
public function renderConfigForm()
|
||||
{
|
||||
$visits = AdminStatsController::getVisits(false, date('Y-01-01'), date('Y-12-31'), 'month');
|
||||
$orders = AdminStatsController::getOrders(date('Y-01-01'), date('Y-12-31'), 'month');
|
||||
$sales = AdminStatsController::getTotalSales(date('Y-01-01'), date('Y-12-31'), 'month');
|
||||
|
||||
$stream1 = array('key' => $this->l('Traffic'), 'values' => array());
|
||||
$stream2 = array('key' => $this->l('Conversion Rate'), 'values' => array());
|
||||
$stream3 = array('key' => $this->l('Average Cart Value'), 'values' => array());
|
||||
|
||||
for ($i = '01'; $i <= 12; $i = sprintf('%02d', $i + 1))
|
||||
{
|
||||
$timestamp = strtotime($year.'-'.$i.'-01');
|
||||
|
||||
$goal = ConfigurationKPI::get(strtoupper('dashgoals_traffic_'.$i.'_'.$year));
|
||||
$stream1['values'][] = array('x' => Dashgoals::$month_labels[$i], 'y' => ($goal ? $visits[$timestamp] / $goal : 0));
|
||||
|
||||
$goal = ConfigurationKPI::get(strtoupper('dashgoals_conversion_'.$i.'_'.$year));
|
||||
$value = 0;
|
||||
if ($goal && isset($visits[$timestamp]) && $visits[$timestamp] && isset($orders[$timestamp]) && $orders[$timestamp])
|
||||
$value = (100 * $orders[$timestamp] / $visits[$timestamp]) / $goal;
|
||||
$stream2['values'][] = array('x' => Dashgoals::$month_labels[$i], 'y' => $value);
|
||||
|
||||
$goal = ConfigurationKPI::get(strtoupper('dashgoals_avg_cart_value_'.$i.'_'.$year));
|
||||
$value = 0;
|
||||
if ($goal && isset($orders[$timestamp]) && $orders[$timestamp] && isset($sales[$timestamp]) && $sales[$timestamp])
|
||||
$value = ($sales[$timestamp] / $orders[$timestamp]) / $goal;
|
||||
$stream3['values'][] = array('x' => Dashgoals::$month_labels[$i], 'y' => $value);
|
||||
}
|
||||
|
||||
return array('chart_type' => 'bar_chart_goals', 'data' => array($stream1, $stream2, $stream3));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,36 @@
|
||||
function line_chart_goals(widget_name, chart_details)
|
||||
function bar_chart_goals(widget_name, chart_details)
|
||||
{
|
||||
nv.addGraph(function() {
|
||||
var chart = nv.models.lineChart()
|
||||
.useInteractiveGuideline(true)
|
||||
.x(function(d) { return d[0] })
|
||||
.y(function(d) { return d[1]/100 })
|
||||
.color(d3.scale.category10().range());
|
||||
|
||||
chart.xAxis
|
||||
.tickFormat(function(d) {
|
||||
return d3.time.format('%m/%d/%y')(new Date(d))
|
||||
});
|
||||
|
||||
chart.yAxis
|
||||
.tickFormat(d3.format(',.1%'));
|
||||
nv.addGraph(function() {
|
||||
var chart = nv.models.multiBarChart();
|
||||
|
||||
chart.yAxis.tickFormat(d3.format('%'));
|
||||
|
||||
d3.select('#dash_goals_chart1 svg')
|
||||
.datum(chart_details.data)
|
||||
.transition()
|
||||
.call(chart);
|
||||
|
||||
nv.utils.windowResize(chart.update);
|
||||
|
||||
return chart;
|
||||
});
|
||||
}
|
||||
|
||||
function dashgoals_calc_sales()
|
||||
{
|
||||
$('.dashgoals_sales').each(function() {
|
||||
var key = $(this).attr('id').substr(16);
|
||||
var sales = parseFloat($('#dashgoals_traffic_' + key).val()) * parseFloat($('#dashgoals_avg_cart_value_' + key).val()) * parseFloat($('#dashgoals_conversion_' + key).val()) / 100;
|
||||
if (isNaN(sales))
|
||||
$(this).text('0');
|
||||
else
|
||||
$(this).text(parseInt(sales));
|
||||
});
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
$('.dashgoals_config_input').keyup(function() {
|
||||
$('.dashgoals_sales').each(function() {
|
||||
var key = $(this).attr('id').substr(16);
|
||||
var sales = parseFloat($('#dashgoals_traffic_' + key).val()) * parseFloat($('#dashgoals_average_cart_value_' + key).val()) * parseFloat($('#dashgoals_conversion_rate_' + key).val()) / 100;
|
||||
if (isNaN(sales))
|
||||
$(this).text('0');
|
||||
else
|
||||
$(this).text(parseInt(sales));
|
||||
});
|
||||
dashgoals_calc_sales();
|
||||
});
|
||||
});
|
||||
dashgoals_calc_sales();
|
||||
});
|
||||
|
||||
@@ -35,32 +35,35 @@
|
||||
</a>
|
||||
</span>
|
||||
</header>
|
||||
<section id="dashgoals_config" class="dash_config">
|
||||
<section id="dashgoals_config" class="dash_config hide">
|
||||
<header><i class="icon-wrench"></i> {l s='Configuration' mod='dashgoals'}</header>
|
||||
<table class="table table-condensed table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>-</th>
|
||||
<th>{l s='Traffic' mod='dashgoals'}</th>
|
||||
<th>{l s='Conversion Rate' mod='dashgoals'}</th>
|
||||
<th>{l s='Average Cart Value' mod='dashgoals'}</th>
|
||||
<th>{l s='Sales' mod='dashgoals'}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{foreach $goals_months as $month}
|
||||
<tr>
|
||||
<td>{$month}</td>
|
||||
<td><input id="dashgoals_traffic_{$month@key}" name="dashgoals_traffic_{$month@key}" class="dashgoals_config_input form-control" value="" /></td>
|
||||
<td><input id="dashgoals_conversion_rate_{$month@key}" name="dashgoals_conversion_rate_{$month@key}" class="dashgoals_config_input form-control" value="" /></td>
|
||||
<td><input id="dashgoals_average_cart_value_{$month@key}" name="dashgoals_average_cart_value_{$month@key}" class="dashgoals_config_input form-control" value="" /></td>
|
||||
<td id="dashgoals_sales_{$month@key}" class="dashgoals_sales">0</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
</tbody>
|
||||
</table>
|
||||
<form class="defaultForm form-horizontal" method="post" action="{$link->getAdminLink('AdminDashboard')}">
|
||||
<table class="table table-condensed table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{$goals_year}</th>
|
||||
<th>{l s='Traffic' mod='dashgoals'}</th>
|
||||
<th>{l s='Conversion Rate' mod='dashgoals'}</th>
|
||||
<th>{l s='Average Cart Value' mod='dashgoals'}</th>
|
||||
<th>{l s='Sales' mod='dashgoals'}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{foreach $goals_months as $month}
|
||||
<tr>
|
||||
<td>{$month.label}</td>
|
||||
<td><input id="dashgoals_traffic_{$month@key}" name="dashgoals_traffic_{$month@key}" class="dashgoals_config_input form-control" value="{$month.values.traffic|intval}" /></td>
|
||||
<td><input id="dashgoals_conversion_{$month@key}" name="dashgoals_conversion_{$month@key}" class="dashgoals_config_input form-control" value="{$month.values.conversion|floatval}" /></td>
|
||||
<td><input id="dashgoals_avg_cart_value_{$month@key}" name="dashgoals_avg_cart_value_{$month@key}" class="dashgoals_config_input form-control" value="{$month.values.avg_cart_value|intval}" /></td>
|
||||
<td id="dashgoals_sales_{$month@key}" class="dashgoals_sales">0</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
</tbody>
|
||||
</table>
|
||||
<input class="btn btn-default" name="submitDashGoals" type="submit" value="{l s='Save' mod='dashgoals'}" />
|
||||
</form>
|
||||
</section>
|
||||
<section class="loading hide">
|
||||
<section class="loading">
|
||||
<div id="dash_goals_chart1" class="chart with-transitions">
|
||||
<svg></svg>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user