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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/
namespace Piwik;
require_once PIWIK_INCLUDE_PATH . "/core/Piwik.php";
/**
* This class holds the various mappings we use to internally store and manipulate metrics.
*
* @api
*/
class Metrics
{
/**
* When saving DataTables in the DB, we replace all columns name with these IDs. This saves many bytes,
* eg. INDEX_NB_UNIQ_VISITORS is an integer: 4 bytes, but 'nb_uniq_visitors' is 16 bytes at least
*/
const INDEX_NB_UNIQ_VISITORS = 1;
const INDEX_NB_VISITS = 2;
const INDEX_NB_ACTIONS = 3;
const INDEX_MAX_ACTIONS = 4;
const INDEX_SUM_VISIT_LENGTH = 5;
const INDEX_BOUNCE_COUNT = 6;
const INDEX_NB_VISITS_CONVERTED = 7;
const INDEX_NB_CONVERSIONS = 8;
const INDEX_REVENUE = 9;
const INDEX_GOALS = 10;
const INDEX_SUM_DAILY_NB_UNIQ_VISITORS = 11;
// Specific to the Actions reports
const INDEX_PAGE_NB_HITS = 12;
const INDEX_PAGE_SUM_TIME_SPENT = 13;
const INDEX_PAGE_EXIT_NB_UNIQ_VISITORS = 14;
const INDEX_PAGE_EXIT_NB_VISITS = 15;
const INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS = 16;
const INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS = 17;
const INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS = 18;
const INDEX_PAGE_ENTRY_NB_VISITS = 19;
const INDEX_PAGE_ENTRY_NB_ACTIONS = 20;
const INDEX_PAGE_ENTRY_SUM_VISIT_LENGTH = 21;
const INDEX_PAGE_ENTRY_BOUNCE_COUNT = 22;
// Ecommerce Items reports
const INDEX_ECOMMERCE_ITEM_REVENUE = 23;
const INDEX_ECOMMERCE_ITEM_QUANTITY = 24;
const INDEX_ECOMMERCE_ITEM_PRICE = 25;
const INDEX_ECOMMERCE_ORDERS = 26;
const INDEX_ECOMMERCE_ITEM_PRICE_VIEWED = 27;
// Site Search
const INDEX_SITE_SEARCH_HAS_NO_RESULT = 28;
const INDEX_PAGE_IS_FOLLOWING_SITE_SEARCH_NB_HITS = 29;
// Performance Analytics
const INDEX_PAGE_SUM_TIME_GENERATION = 30;
const INDEX_PAGE_NB_HITS_WITH_TIME_GENERATION = 31;
const INDEX_PAGE_MIN_TIME_GENERATION = 32;
const INDEX_PAGE_MAX_TIME_GENERATION = 33;
// Goal reports
const INDEX_GOAL_NB_CONVERSIONS = 1;
const INDEX_GOAL_REVENUE = 2;
const INDEX_GOAL_NB_VISITS_CONVERTED = 3;
const INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL = 4;
const INDEX_GOAL_ECOMMERCE_REVENUE_TAX = 5;
const INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING = 6;
const INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT = 7;
const INDEX_GOAL_ECOMMERCE_ITEMS = 8;
static public $mappingFromIdToName = array(
Metrics::INDEX_NB_UNIQ_VISITORS => 'nb_uniq_visitors',
Metrics::INDEX_NB_VISITS => 'nb_visits',
Metrics::INDEX_NB_ACTIONS => 'nb_actions',
Metrics::INDEX_MAX_ACTIONS => 'max_actions',
Metrics::INDEX_SUM_VISIT_LENGTH => 'sum_visit_length',
Metrics::INDEX_BOUNCE_COUNT => 'bounce_count',
Metrics::INDEX_NB_VISITS_CONVERTED => 'nb_visits_converted',
Metrics::INDEX_NB_CONVERSIONS => 'nb_conversions',
Metrics::INDEX_REVENUE => 'revenue',
Metrics::INDEX_GOALS => 'goals',
Metrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_nb_uniq_visitors',
// Actions metrics
Metrics::INDEX_PAGE_NB_HITS => 'nb_hits',
Metrics::INDEX_PAGE_SUM_TIME_SPENT => 'sum_time_spent',
Metrics::INDEX_PAGE_SUM_TIME_GENERATION => 'sum_time_generation',
Metrics::INDEX_PAGE_NB_HITS_WITH_TIME_GENERATION => 'nb_hits_with_time_generation',
Metrics::INDEX_PAGE_MIN_TIME_GENERATION => 'min_time_generation',
Metrics::INDEX_PAGE_MAX_TIME_GENERATION => 'max_time_generation',
Metrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS => 'exit_nb_uniq_visitors',
Metrics::INDEX_PAGE_EXIT_NB_VISITS => 'exit_nb_visits',
Metrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_exit_nb_uniq_visitors',
Metrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS => 'entry_nb_uniq_visitors',
Metrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_entry_nb_uniq_visitors',
Metrics::INDEX_PAGE_ENTRY_NB_VISITS => 'entry_nb_visits',
Metrics::INDEX_PAGE_ENTRY_NB_ACTIONS => 'entry_nb_actions',
Metrics::INDEX_PAGE_ENTRY_SUM_VISIT_LENGTH => 'entry_sum_visit_length',
Metrics::INDEX_PAGE_ENTRY_BOUNCE_COUNT => 'entry_bounce_count',
Metrics::INDEX_PAGE_IS_FOLLOWING_SITE_SEARCH_NB_HITS => 'nb_hits_following_search',
// Items reports metrics
Metrics::INDEX_ECOMMERCE_ITEM_REVENUE => 'revenue',
Metrics::INDEX_ECOMMERCE_ITEM_QUANTITY => 'quantity',
Metrics::INDEX_ECOMMERCE_ITEM_PRICE => 'price',
Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED => 'price_viewed',
Metrics::INDEX_ECOMMERCE_ORDERS => 'orders',
);
static public $mappingFromIdToNameGoal = array(
Metrics::INDEX_GOAL_NB_CONVERSIONS => 'nb_conversions',
Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 'nb_visits_converted',
Metrics::INDEX_GOAL_REVENUE => 'revenue',
Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL => 'revenue_subtotal',
Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX => 'revenue_tax',
Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING => 'revenue_shipping',
Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT => 'revenue_discount',
Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 'items',
);
static public $mappingFromNameToId = array(
'nb_uniq_visitors' => Metrics::INDEX_NB_UNIQ_VISITORS,
'nb_visits' => Metrics::INDEX_NB_VISITS,
'nb_actions' => Metrics::INDEX_NB_ACTIONS,
'max_actions' => Metrics::INDEX_MAX_ACTIONS,
'sum_visit_length' => Metrics::INDEX_SUM_VISIT_LENGTH,
'bounce_count' => Metrics::INDEX_BOUNCE_COUNT,
'nb_visits_converted' => Metrics::INDEX_NB_VISITS_CONVERTED,
'nb_conversions' => Metrics::INDEX_NB_CONVERSIONS,
'revenue' => Metrics::INDEX_REVENUE,
'goals' => Metrics::INDEX_GOALS,
'sum_daily_nb_uniq_visitors' => Metrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS,
);
static protected $metricsAggregatedFromLogs = array(
Metrics::INDEX_NB_UNIQ_VISITORS,
Metrics::INDEX_NB_VISITS,
Metrics::INDEX_NB_ACTIONS,
Metrics::INDEX_MAX_ACTIONS,
Metrics::INDEX_SUM_VISIT_LENGTH,
Metrics::INDEX_BOUNCE_COUNT,
Metrics::INDEX_NB_VISITS_CONVERTED,
);
/* Used in DataTable Sort filter */
static public function getVisitsMetricNames()
{
$names = array();
foreach (self::$metricsAggregatedFromLogs as $metricId) {
$names[$metricId] = self::$mappingFromIdToName[$metricId];
}
return $names;
}
static public function getMappingFromIdToName()
{
$idToName = array_flip(self::$mappingFromIdToName);
return $idToName;
}
/**
* Is a lower value for a given column better?
* @param $column
* @return bool
*
* @ignore
*/
static public function isLowerValueBetter($column)
{
$lowerIsBetterPatterns = array(
'bounce', 'exit'
);
foreach ($lowerIsBetterPatterns as $pattern) {
if (strpos($column, $pattern) !== false) {
return true;
}
}
return false;
}
/**
* Derive the unit name from a column name
* @param $column
* @param $idSite
* @return string
* @ignore
*/
static public function getUnit($column, $idSite)
{
$nameToUnit = array(
'_rate' => '%',
'revenue' => MetricsFormatter::getCurrencySymbol($idSite),
'_time_' => 's'
);
foreach ($nameToUnit as $pattern => $type) {
if (strpos($column, $pattern) !== false) {
return $type;
}
}
return '';
}
static public function getDefaultMetricTranslations()
{
$trans = array(
'label' => 'General_ColumnLabel',
'date' => 'General_Date',
'avg_time_on_page' => 'General_ColumnAverageTimeOnPage',
'sum_time_spent' => 'General_ColumnSumVisitLength',
'sum_visit_length' => 'General_ColumnSumVisitLength',
'bounce_count' => 'General_ColumnBounces',
'bounce_count_returning' => 'VisitFrequency_ColumnBounceCountForReturningVisits',
'max_actions' => 'General_ColumnMaxActions',
'max_actions_returning' => 'VisitFrequency_ColumnMaxActionsInReturningVisit',
'nb_visits_converted_returning' => 'VisitFrequency_ColumnNbReturningVisitsConverted',
'sum_visit_length_returning' => 'VisitFrequency_ColumnSumVisitLengthReturning',
'nb_visits_converted' => 'General_ColumnVisitsWithConversions',
'nb_conversions' => 'Goals_ColumnConversions',
'revenue' => 'General_ColumnRevenue',
'nb_hits' => 'General_ColumnPageviews',
'entry_nb_visits' => 'General_ColumnEntrances',
'entry_nb_uniq_visitors' => 'General_ColumnUniqueEntrances',
'exit_nb_visits' => 'General_ColumnExits',
'exit_nb_uniq_visitors' => 'General_ColumnUniqueExits',
'entry_bounce_count' => 'General_ColumnBounces',
'exit_bounce_count' => 'General_ColumnBounces',
'exit_rate' => 'General_ColumnExitRate'
);
$trans = array_map(array('\\Piwik\\Piwik','translate'), $trans);
$dailySum = ' (' . Piwik::translate('General_DailySum') . ')';
$afterEntry = ' ' . Piwik::translate('General_AfterEntry');
$trans['sum_daily_nb_uniq_visitors'] = Piwik::translate('General_ColumnNbUniqVisitors') . $dailySum;
$trans['sum_daily_entry_nb_uniq_visitors'] = Piwik::translate('General_ColumnUniqueEntrances') . $dailySum;
$trans['sum_daily_exit_nb_uniq_visitors'] = Piwik::translate('General_ColumnUniqueExits') . $dailySum;
$trans['entry_nb_actions'] = Piwik::translate('General_ColumnNbActions') . $afterEntry;
$trans['entry_sum_visit_length'] = Piwik::translate('General_ColumnSumVisitLength') . $afterEntry;
$trans = array_merge(self::getDefaultMetrics(), self::getDefaultProcessedMetrics(), $trans);
return $trans;
}
static public function getDefaultMetrics()
{
$translations = array(
'nb_visits' => 'General_ColumnNbVisits',
'nb_uniq_visitors' => 'General_ColumnNbUniqVisitors',
'nb_actions' => 'General_ColumnNbActions',
);
$translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
return $translations;
}
static public function getDefaultProcessedMetrics()
{
$translations = array(
// Processed in AddColumnsProcessedMetrics
'nb_actions_per_visit' => 'General_ColumnActionsPerVisit',
'avg_time_on_site' => 'General_ColumnAvgTimeOnSite',
'bounce_rate' => 'General_ColumnBounceRate',
'conversion_rate' => 'General_ColumnConversionRate',
);
return array_map(array('\\Piwik\\Piwik','translate'), $translations);
}
static public function getDefaultMetricsDocumentation()
{
$documentation = array(
'nb_visits' => 'General_ColumnNbVisitsDocumentation',
'nb_uniq_visitors' => 'General_ColumnNbUniqVisitorsDocumentation',
'nb_actions' => 'General_ColumnNbActionsDocumentation',
'nb_actions_per_visit' => 'General_ColumnActionsPerVisitDocumentation',
'avg_time_on_site' => 'General_ColumnAvgTimeOnSiteDocumentation',
'bounce_rate' => 'General_ColumnBounceRateDocumentation',
'conversion_rate' => 'General_ColumnConversionRateDocumentation',
'avg_time_on_page' => 'General_ColumnAverageTimeOnPageDocumentation',
'nb_hits' => 'General_ColumnPageviewsDocumentation',
'exit_rate' => 'General_ColumnExitRateDocumentation'
);
return array_map(array('\\Piwik\\Piwik','translate'), $documentation);
}
public static function getPercentVisitColumn()
{
$percentVisitsLabel = str_replace(' ', ' ', Piwik::translate('General_ColumnPercentageVisits'));
return $percentVisitsLabel;
}
}
|