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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Goals\Visualizations;
use Piwik\API\DataTablePostProcessor;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Filter\AddColumnsProcessedMetricsGoal;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
use Piwik\Plugins\Goals\API as APIGoals;
use Piwik\Site;
use Piwik\View;
require_once PIWIK_INCLUDE_PATH . '/core/Twig.php';
/**
* DataTable Visualization that derives from HtmlTable and sets show_goals_columns to true.
*/
class Goals extends HtmlTable
{
const ID = 'tableGoals';
const FOOTER_ICON = 'icon-goal';
const FOOTER_ICON_TITLE = 'General_DisplayTableWithGoalMetrics';
public function beforeLoadDataTable()
{
parent::beforeLoadDataTable();
$this->config->show_totals_row = false;
if ($this->config->disable_subtable_when_show_goals) {
$this->config->subtable_controller_action = null;
}
$this->setShowGoalsColumnsProperties();
}
public function beforeRender()
{
$this->config->show_totals_row = false;
$this->config->show_goals = true;
$this->config->show_goals_columns = true;
$this->config->datatable_css_class = 'dataTableVizGoals';
$this->config->show_exclude_low_population = true;
$this->config->metrics_documentation['nb_visits'] = Piwik::translate('Goals_ColumnVisits');
if (1 == Common::getRequestVar('documentationForGoalsPage', 0, 'int')) {
// TODO: should not use query parameter
$this->config->documentation = Piwik::translate('Goals_ConversionByTypeReportDocumentation',
array('<br />', '<br />', '<a href="https://matomo.org/docs/tracking-goals-web-analytics/" rel="noreferrer noopener" target="_blank">', '</a>'));
}
parent::beforeRender();
}
private function setShowGoalsColumnsProperties()
{
// set view properties based on goal requested
$idSite = Common::getRequestVar('idSite', null, 'int');
$idGoal = Common::getRequestVar('idGoal', AddColumnsProcessedMetricsGoal::GOALS_OVERVIEW, 'string');
$goalsToProcess = null;
if (Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER == $idGoal) {
$this->setPropertiesForEcommerceView();
$goalsToProcess = array($idGoal);
} else if (AddColumnsProcessedMetricsGoal::GOALS_FULL_TABLE == $idGoal) {
$this->setPropertiesForGoals($idSite, 'all');
$goalsToProcess = $this->getAllGoalIds($idSite);
} else if (AddColumnsProcessedMetricsGoal::GOALS_OVERVIEW == $idGoal) {
$this->setPropertiesForGoalsOverview($idSite);
$goalsToProcess = $this->getAllGoalIds($idSite);
} else {
$this->setPropertiesForGoals($idSite, array($idGoal));
$goalsToProcess = array($idGoal);
}
// add goals columns
$this->requestConfig->request_parameters_to_modify['filter_update_columns_when_show_all_goals'] = $idGoal;
$this->requestConfig->request_parameters_to_modify['filter_show_goal_columns_process_goals'] = implode(',', $goalsToProcess);
}
private function setPropertiesForEcommerceView()
{
$this->requestConfig->filter_sort_column = 'goal_ecommerceOrder_revenue';
$this->requestConfig->filter_sort_order = 'desc';
$this->config->columns_to_display = array(
'label', 'nb_visits', 'goal_ecommerceOrder_nb_conversions', 'goal_ecommerceOrder_revenue',
'goal_ecommerceOrder_conversion_rate', 'goal_ecommerceOrder_avg_order_revenue', 'goal_ecommerceOrder_items',
'goal_ecommerceOrder_revenue_per_visit'
);
$this->config->translations = array_merge($this->config->translations, array(
'goal_ecommerceOrder_nb_conversions' => Piwik::translate('General_EcommerceOrders'),
'goal_ecommerceOrder_revenue' => Piwik::translate('General_TotalRevenue'),
'goal_ecommerceOrder_revenue_per_visit' => Piwik::translate('General_ColumnValuePerVisit')
));
$goalName = Piwik::translate('General_EcommerceOrders');
$this->config->metrics_documentation['revenue_per_visit'] =
Piwik::translate('Goals_ColumnRevenuePerVisitDocumentation', $goalName);
}
protected function setPropertiesForGoalsOverview($idSite)
{
$allGoals = $this->getGoals($idSite);
// set view properties
$this->config->columns_to_display = array('label', 'nb_visits');
foreach ($allGoals as $goal) {
$column = "goal_{$goal['idgoal']}_conversion_rate";
$this->config->columns_to_display[] = $column;
}
$this->config->columns_to_display[] = 'revenue_per_visit';
}
protected function setPropertiesForGoals($idSite, $idGoals)
{
$allGoals = $this->getGoals($idSite);
if ('all' == $idGoals) {
$idGoals = array_keys($allGoals);
} else {
// only sort by a goal's conversions if not showing all goals (for FULL_REPORT)
$this->requestConfig->filter_sort_column = 'goal_' . reset($idGoals) . '_nb_conversions';
$this->requestConfig->filter_sort_order = 'desc';
}
$this->config->columns_to_display = array('label', 'nb_visits');
$goalColumnTemplates = array(
'goal_%s_nb_conversions',
'goal_%s_conversion_rate',
'goal_%s_revenue',
'goal_%s_revenue_per_visit',
);
// set columns to display (columns of same type but different goals will be next to each other,
// ie, goal_0_nb_conversions, goal_1_nb_conversions, etc.)
foreach ($goalColumnTemplates as $columnTemplate) {
foreach ($idGoals as $idGoal) {
$this->config->columns_to_display[] = sprintf($columnTemplate, $idGoal);
}
}
$this->config->columns_to_display[] = 'revenue_per_visit';
}
protected $goalsForCurrentSite = null;
protected function getGoals($idSite)
{
if ($this->goalsForCurrentSite === null) {
// get all goals to display info for
$allGoals = array();
// add the ecommerce goal if ecommerce is enabled for the site
if (Site::isEcommerceEnabledFor($idSite)) {
$ecommerceGoal = array(
'idgoal' => Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER,
'name' => Piwik::translate('Goals_EcommerceOrder'),
'quoted_name' => false
);
$allGoals[$ecommerceGoal['idgoal']] = $ecommerceGoal;
}
// add the site's goals (and escape all goal names)
$siteGoals = Request::processRequest('Goals.getGoals', ['idSite' => $idSite, 'filter_limit' => '-1'], $default = []);
foreach ($siteGoals as &$goal) {
$goal['name'] = Common::sanitizeInputValue($goal['name']);
$goal['quoted_name'] = '"' . $goal['name'] . '"';
$allGoals[$goal['idgoal']] = $goal;
}
$this->goalsForCurrentSite = $allGoals;
}
return $this->goalsForCurrentSite;
}
protected function getAllGoalIds($idSite)
{
$allGoals = $this->getGoals($idSite);
return array_map(function ($data) {
return $data['idgoal'];
}, $allGoals);
}
/**
* Hide any goal columns marked as excluded by datatable filters
*/
protected function removeExcludedColumns()
{
$excludedGoalColumns = $this->dataTable->getMetadata('excluded_goal_columns');
if ($excludedGoalColumns) {
$excludedGoalColumns = json_decode($excludedGoalColumns);
foreach ($this->config->columns_to_display as $k => $v) {
foreach ($excludedGoalColumns as $excludeColName) {
if ($v === $excludeColName) {
unset($this->config->columns_to_display[$k]);
break;
}
}
}
$this->dataTable->deleteMetadata('excluded_goal_columns');
}
}
}
|