1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Goals\Reports;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Metrics\Formatter;
use Piwik\NumberFormatter;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution;
use Piwik\Plugins\CoreVisualizations\Visualizations\Sparklines;
use Piwik\Plugins\Goals\API;
use Piwik\Plugins\Goals\Goals;
use Piwik\Plugins\Goals\Pages;
use Piwik\Report\ReportWidgetFactory;
use Piwik\Site;
use Piwik\Tracker\GoalManager;
use Piwik\Widget\WidgetsList;
class Get extends Base
{
protected function init()
{
parent::init();
$this->name = Piwik::translate('Goals_Goals');
$this->processedMetrics = array('conversion_rate');
$this->documentation = ''; // TODO
$this->order = 1;
$this->orderGoal = 50;
$this->metrics = array('nb_conversions', 'nb_visits_converted', 'revenue');
$this->parameters = null;
}
public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $factory)
{
$idSite = $this->getIdSite();
$goals = API::getInstance()->getGoals($idSite);
$reports = Goals::getReportsWithGoalMetrics();
$page = new Pages($factory, $reports);
$widgetsList->addWidgetConfigs($page->createGoalsOverviewPage($goals));
if ($this->isEcommerceEnabled($idSite)) {
$widgetsList->addWidgetConfigs($page->createEcommerceOverviewPage());
$widgetsList->addWidgetConfigs($page->createEcommerceSalesPage());
}
foreach ($goals as $goal) {
$widgetsList->addWidgetConfigs($page->createGoalDetailPage($goal));
}
}
private function getIdSite()
{
return Common::getRequestVar('idSite', null, 'int');
}
private function isEcommerceEnabled($idSite)
{
if (!Plugin\Manager::getInstance()->isPluginActivated('Ecommerce')) {
return false;
}
$site = new Site($idSite);
return $site->isEcommerceEnabled();
}
public function configureView(ViewDataTable $view)
{
if ($view->isViewDataTableId(Sparklines::ID)) {
/** @var Sparklines $view */
$idSite = $this->getIdSite();
$isEcommerceEnabled = $this->isEcommerceEnabled($idSite);
$idGoal = Common::getRequestVar('idGoal', 0, 'int');
$numberFormatter = NumberFormatter::getInstance();
$view->config->filters[] = function (DataTable $table) use ($numberFormatter, $idSite) {
$firstRow = $table->getFirstRow();
if ($firstRow) {
$revenue = $firstRow->getColumn('revenue');
$currencySymbol = Site::getCurrencySymbolFor($idSite);
$revenue = $numberFormatter->formatCurrency($revenue, $currencySymbol, GoalManager::REVENUE_PRECISION);
$firstRow->setColumn('revenue', $revenue);
$conversionRate = $firstRow->getColumn('conversion_rate');
if (false !== $conversionRate) {
$firstRow->setColumn('conversion_rate', $numberFormatter->formatPercent($conversionRate, $precision = 1));
}
$conversions = $firstRow->getColumn('nb_conversions');
if (false !== $conversions) {
$firstRow->setColumn('nb_conversions', $numberFormatter->formatNumber($conversions));
}
$visitsConverted = $firstRow->getColumn('nb_visits_converted');
if (false !== $visitsConverted) {
$firstRow->setColumn('nb_visits_converted', $numberFormatter->formatNumber($visitsConverted));
}
}
};
$view->config->addTranslations(array(
'nb_visits' => Piwik::translate('VisitsSummary_NbVisitsDescription'),
'nb_conversions' => Piwik::translate('Goals_ConversionsDescription'),
'nb_visits_converted' => Piwik::translate('General_NVisits'),
'conversion_rate' => Piwik::translate('Goals_OverallConversionRate'),
'revenue' => Piwik::translate('Goals_OverallRevenue'),
));
$allowMultiple = Common::getRequestVar('allow_multiple', 0, 'int');
if ($allowMultiple) {
$view->config->addSparklineMetric(array('nb_conversions', 'nb_visits_converted'), $order = 10);
} else {
$view->config->addSparklineMetric(array('nb_conversions'), $order = 10);
}
$view->config->addSparklineMetric(array('conversion_rate'), $order = 20);
if (empty($idGoal)) {
// goals overview sparklines below evolution graph
if ($isEcommerceEnabled) {
// this would be ideally done in Ecommerce plugin but then it is hard to keep same order
$view->config->addSparklineMetric(array('revenue'), $order = 30);
}
} else {
$onlySummary = Common::getRequestVar('only_summary', 0, 'int');
if ($onlySummary) {
// in Goals Overview we list an overview for each goal....
$view->config->addTranslation('conversion_rate', Piwik::translate('Goals_ConversionRate'));
} elseif ($isEcommerceEnabled) {
// in Goals detail page...
$view->config->addSparklineMetric(array('revenue'), $order = 30);
}
}
} else if ($view->isViewDataTableId(Evolution::ID)) {
if (empty($view->config->columns_to_display)) {
$view->config->columns_to_display = array('nb_conversions');
}
}
}
public function configureReportMetadata(&$availableReports, $infos)
{
if (!$this->isEnabled()) {
return;
}
parent::configureReportMetadata($availableReports, $infos);
$this->addReportMetadataForEachGoal($availableReports, $infos, function ($goal) {
return Piwik::translate('Goals_GoalX', $goal['name']);
});
}
}
|