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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
<?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;
use Piwik\DataAccess\LogAggregator;
use Piwik\DataArray;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Tracker\GoalManager;
use Piwik\Plugins\VisitFrequency\API as VisitFrequencyAPI;
class Archiver extends \Piwik\Plugin\Archiver
{
const VISITS_UNTIL_RECORD_NAME = 'visits_until_conv';
const DAYS_UNTIL_CONV_RECORD_NAME = 'days_until_conv';
const ITEMS_SKU_RECORD_NAME = 'Goals_ItemsSku';
const ITEMS_NAME_RECORD_NAME = 'Goals_ItemsName';
const ITEMS_CATEGORY_RECORD_NAME = 'Goals_ItemsCategory';
const SKU_FIELD = 'idaction_sku';
const NAME_FIELD = 'idaction_name';
const CATEGORY_FIELD = 'idaction_category';
const CATEGORY2_FIELD = 'idaction_category2';
const CATEGORY3_FIELD = 'idaction_category3';
const CATEGORY4_FIELD = 'idaction_category4';
const CATEGORY5_FIELD = 'idaction_category5';
const NO_LABEL = ':';
const LOG_CONVERSION_TABLE = 'log_conversion';
const VISITS_COUNT_FIELD = 'visitor_count_visits';
const SECONDS_SINCE_FIRST_VISIT_FIELD = 'visitor_seconds_since_first';
/**
* This array stores the ranges to use when displaying the 'visits to conversion' report
*/
public static $visitCountRanges = array(
array(1, 1),
array(2, 2),
array(3, 3),
array(4, 4),
array(5, 5),
array(6, 6),
array(7, 7),
array(8, 8),
array(9, 14),
array(15, 25),
array(26, 50),
array(51, 100),
array(100)
);
/**
* This array stores the ranges to use when displaying the 'days to conversion' report
*/
public static $daysToConvRanges = array(
array(0, 0),
array(1, 1),
array(2, 2),
array(3, 3),
array(4, 4),
array(5, 5),
array(6, 6),
array(7, 7),
array(8, 14),
array(15, 30),
array(31, 60),
array(61, 120),
array(121, 364),
array(364)
);
protected $dimensionRecord = [
self::SKU_FIELD => self::ITEMS_SKU_RECORD_NAME,
self::NAME_FIELD => self::ITEMS_NAME_RECORD_NAME,
self::CATEGORY_FIELD => self::ITEMS_CATEGORY_RECORD_NAME
];
protected $actionMapping = [
self::SKU_FIELD => 'idaction_product_sku',
self::NAME_FIELD => 'idaction_product_name',
self::CATEGORY_FIELD => 'idaction_product_cat',
self::CATEGORY2_FIELD => 'idaction_product_cat2',
self::CATEGORY3_FIELD => 'idaction_product_cat3',
self::CATEGORY4_FIELD => 'idaction_product_cat4',
self::CATEGORY5_FIELD => 'idaction_product_cat5',
];
/**
* Array containing one DataArray for each Ecommerce items dimension (name/sku/category abandoned carts and orders)
* @var DataArray[][]
*/
protected $itemReports = [];
public function aggregateDayReport()
{
$this->aggregateGeneralGoalMetrics();
$this->aggregateEcommerceItems();
$this->getProcessor()->processDependentArchive('Goals', VisitFrequencyAPI::NEW_VISITOR_SEGMENT);
$this->getProcessor()->processDependentArchive('Goals', VisitFrequencyAPI::RETURNING_VISITOR_SEGMENT);
}
protected function aggregateGeneralGoalMetrics()
{
$prefixes = array(
self::VISITS_UNTIL_RECORD_NAME => 'vcv',
self::DAYS_UNTIL_CONV_RECORD_NAME => 'vdsf',
);
$selects = array();
$selects = array_merge($selects, LogAggregator::getSelectsFromRangedColumn(
self::VISITS_COUNT_FIELD, self::$visitCountRanges, self::LOG_CONVERSION_TABLE, $prefixes[self::VISITS_UNTIL_RECORD_NAME]
));
$selects = array_merge($selects, LogAggregator::getSelectsFromRangedColumn(
'FLOOR(log_conversion.' . self::SECONDS_SINCE_FIRST_VISIT_FIELD . ' / 86400)', self::$daysToConvRanges, self::LOG_CONVERSION_TABLE, $prefixes[self::DAYS_UNTIL_CONV_RECORD_NAME]
));
$query = $this->getLogAggregator()->queryConversionsByDimension(array(), false, $selects);
if ($query === false) {
return;
}
$totalConversions = $totalRevenue = 0;
$goals = new DataArray();
$visitsToConversions = $daysToConversions = array();
$conversionMetrics = $this->getLogAggregator()->getConversionsMetricFields();
while ($row = $query->fetch()) {
$idGoal = $row['idgoal'];
unset($row['idgoal']);
unset($row['label']);
$values = array();
foreach ($conversionMetrics as $field => $statement) {
$values[$field] = $row[$field];
}
$goals->sumMetrics($idGoal, $values);
if (empty($visitsToConversions[$idGoal])) {
$visitsToConversions[$idGoal] = new DataTable();
}
$array = LogAggregator::makeArrayOneColumn($row, Metrics::INDEX_NB_CONVERSIONS, $prefixes[self::VISITS_UNTIL_RECORD_NAME]);
$visitsToConversions[$idGoal]->addDataTable(DataTable::makeFromIndexedArray($array));
if (empty($daysToConversions[$idGoal])) {
$daysToConversions[$idGoal] = new DataTable();
}
$array = LogAggregator::makeArrayOneColumn($row, Metrics::INDEX_NB_CONVERSIONS, $prefixes[self::DAYS_UNTIL_CONV_RECORD_NAME]);
$daysToConversions[$idGoal]->addDataTable(DataTable::makeFromIndexedArray($array));
// We don't want to sum Abandoned cart metrics in the overall revenue/conversions/converted visits
// since it is a "negative conversion"
if ($idGoal != GoalManager::IDGOAL_CART) {
$totalConversions += $row[Metrics::INDEX_GOAL_NB_CONVERSIONS];
$totalRevenue += $row[Metrics::INDEX_GOAL_REVENUE];
}
}
// Stats by goal, for all visitors
$numericRecords = $this->getConversionsNumericMetrics($goals);
$this->getProcessor()->insertNumericRecords($numericRecords);
$this->insertReports(self::VISITS_UNTIL_RECORD_NAME, $visitsToConversions);
$this->insertReports(self::DAYS_UNTIL_CONV_RECORD_NAME, $daysToConversions);
// Stats for all goals
$nbConvertedVisits = $this->getProcessor()->getNumberOfVisitsConverted();
$metrics = array(
self::getRecordName('nb_conversions') => $totalConversions,
self::getRecordName('nb_visits_converted') => $nbConvertedVisits,
self::getRecordName('revenue') => $totalRevenue,
);
$this->getProcessor()->insertNumericRecords($metrics);
}
protected function getConversionsNumericMetrics(DataArray $goals)
{
$numericRecords = array();
$goals = $goals->getDataArray();
foreach ($goals as $idGoal => $array) {
foreach ($array as $metricId => $value) {
$metricName = Metrics::$mappingFromIdToNameGoal[$metricId];
$recordName = self::getRecordName($metricName, $idGoal);
$numericRecords[$recordName] = $value;
}
}
return $numericRecords;
}
/**
* @param string $recordName 'nb_conversions'
* @param int|bool $idGoal idGoal to return the metrics for, or false to return overall
* @return string Archive record name
*/
public static function getRecordName($recordName, $idGoal = false)
{
$idGoalStr = '';
if ($idGoal !== false) {
$idGoalStr = $idGoal . "_";
}
return 'Goal_' . $idGoalStr . $recordName;
}
protected function insertReports($recordName, $visitsToConversions)
{
foreach ($visitsToConversions as $idGoal => $table) {
$record = self::getRecordName($recordName, $idGoal);
$this->getProcessor()->insertBlobRecord($record, $table->getSerialized());
}
$overviewTable = $this->getOverviewFromGoalTables($visitsToConversions);
$this->getProcessor()->insertBlobRecord(self::getRecordName($recordName), $overviewTable->getSerialized());
}
protected function getOverviewFromGoalTables($tableByGoal)
{
$overview = new DataTable();
foreach ($tableByGoal as $idGoal => $table) {
if ($this->isStandardGoal($idGoal)) {
$overview->addDataTable($table);
}
}
return $overview;
}
protected function isStandardGoal($idGoal)
{
return !in_array($idGoal, $this->getEcommerceIdGoals());
}
protected function aggregateEcommerceItems()
{
$this->initItemReports();
foreach ($this->getItemsDimensions() as $dimension) {
$query = $this->getLogAggregator()->queryEcommerceItems($dimension);
if ($query !== false) {
$this->aggregateFromEcommerceItems($query, $dimension);
}
$query = $this->queryItemViewsForDimension($dimension);
if ($query !== false) {
$this->aggregateFromEcommerceViews($query, $dimension);
}
}
$this->insertItemReports();
return true;
}
protected function queryItemViewsForDimension($dimension)
{
$column = $this->actionMapping[$dimension];
$where = "log_link_visit_action.$column is not null";
return $this->getLogAggregator()->queryActionsByDimension(
['label' => 'log_action1.name'],
$where,
['AVG(log_link_visit_action.product_price) AS `avg_price_viewed`'],
false,
null,
[$column]
);
}
protected function initItemReports()
{
foreach ($this->getEcommerceIdGoals() as $ecommerceType) {
foreach ($this->dimensionRecord as $dimension => $record) {
$this->itemReports[$dimension][$ecommerceType] = new DataArray();
}
}
}
protected function insertItemReports()
{
foreach ($this->itemReports as $dimension => $itemAggregatesByType) {
foreach ($itemAggregatesByType as $ecommerceType => $itemAggregate) {
$recordName = $this->dimensionRecord[$dimension];
if ($ecommerceType == GoalManager::IDGOAL_CART) {
$recordName = self::getItemRecordNameAbandonedCart($recordName);
}
$table = $itemAggregate->asDataTable();
$this->getProcessor()->insertBlobRecord($recordName, $table->getSerialized());
}
}
}
protected function getItemsDimensions()
{
$dimensions = array_keys($this->dimensionRecord);
foreach ($this->getItemExtraCategories() as $category) {
$dimensions[] = $category;
}
return $dimensions;
}
protected function getItemExtraCategories()
{
return array(self::CATEGORY2_FIELD, self::CATEGORY3_FIELD, self::CATEGORY4_FIELD, self::CATEGORY5_FIELD);
}
protected function isItemExtraCategory($field)
{
return in_array($field, $this->getItemExtraCategories());
}
protected function aggregateFromEcommerceItems($query, $dimension)
{
while ($row = $query->fetch()) {
$ecommerceType = $row['ecommerceType'];
$label = $this->cleanupRowGetLabel($row, $dimension);
if ($label === false) {
continue;
}
// Aggregate extra categories in the Item categories array
if ($this->isItemExtraCategory($dimension)) {
$array = $this->itemReports[self::CATEGORY_FIELD][$ecommerceType];
} else {
$array = $this->itemReports[$dimension][$ecommerceType];
}
$this->roundColumnValues($row);
$array->sumMetrics($label, $row);
}
}
protected function aggregateFromEcommerceViews($query, $dimension)
{
while ($row = $query->fetch()) {
$label = $this->getRowLabel($row, $dimension);
if ($label === false) {
continue; // ignore empty additional categories
}
// Aggregate extra categories in the Item categories array
if ($this->isItemExtraCategory($dimension)) {
$array = $this->itemReports[self::CATEGORY_FIELD];
} else {
$array = $this->itemReports[$dimension];
}
unset($row['label']);
$row['avg_price_viewed'] = round($row['avg_price_viewed'], GoalManager::REVENUE_PRECISION);
// add views to all types
foreach ($array as $ecommerceType => $dataArray) {
$dataArray->sumMetrics($label, $row);
}
}
}
protected function cleanupRowGetLabel(&$row, $currentField)
{
$label = $this->getRowLabel($row, $currentField);
if (isset($row['ecommerceType']) && $row['ecommerceType'] == GoalManager::IDGOAL_CART) {
// abandoned carts are the number of visits with an abandoned cart
$row[Metrics::INDEX_ECOMMERCE_ORDERS] = $row[Metrics::INDEX_NB_VISITS];
}
unset($row[Metrics::INDEX_NB_VISITS]);
unset($row['label']);
unset($row['labelIdAction']);
unset($row['ecommerceType']);
return $label;
}
protected function getRowLabel(&$row, $currentField)
{
$label = $row['label'];
if (empty($label)) {
// An empty additional category -> skip this iteration
if ($this->isItemExtraCategory($currentField)) {
return false;
}
$label = "Value not defined";
}
return $label;
}
protected function roundColumnValues(&$row)
{
$columnsToRound = array(
Metrics::INDEX_ECOMMERCE_ITEM_REVENUE,
Metrics::INDEX_ECOMMERCE_ITEM_QUANTITY,
Metrics::INDEX_ECOMMERCE_ITEM_PRICE,
Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED,
);
foreach ($columnsToRound as $column) {
if (isset($row[$column])
&& $row[$column] == round($row[$column])
) {
$row[$column] = round($row[$column]);
}
}
}
protected function getEcommerceIdGoals()
{
return array(GoalManager::IDGOAL_CART, GoalManager::IDGOAL_ORDER);
}
public static function getItemRecordNameAbandonedCart($recordName)
{
return $recordName . '_Cart';
}
/**
* @internal param $this->getProcessor()
*/
public function aggregateMultipleReports()
{
/*
* Archive Ecommerce Items
*/
$dataTableToSum = $this->dimensionRecord;
foreach ($this->dimensionRecord as $recordName) {
$dataTableToSum[] = self::getItemRecordNameAbandonedCart($recordName);
}
$columnsAggregationOperation = null;
$this->getProcessor()->aggregateDataTableRecords($dataTableToSum,
$maximumRowsInDataTableLevelZero = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array());
/*
* Archive General Goal metrics
*/
$goalIdsToSum = GoalManager::getGoalIds($this->getProcessor()->getParams()->getSite()->getId());
//Ecommerce
$goalIdsToSum[] = GoalManager::IDGOAL_ORDER;
$goalIdsToSum[] = GoalManager::IDGOAL_CART; //bug here if idgoal=1
// Overall goal metrics
$goalIdsToSum[] = false;
$fieldsToSum = array();
foreach ($goalIdsToSum as $goalId) {
$metricsToSum = Goals::getGoalColumns($goalId);
foreach ($metricsToSum as $metricName) {
$fieldsToSum[] = self::getRecordName($metricName, $goalId);
}
}
$this->getProcessor()->aggregateNumericMetrics($fieldsToSum);
$columnsAggregationOperation = null;
foreach ($goalIdsToSum as $goalId) {
// sum up the visits to conversion data table & the days to conversion data table
$this->getProcessor()->aggregateDataTableRecords(
array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME, $goalId),
self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME, $goalId)),
$maximumRowsInDataTableLevelZero = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array());
}
$columnsAggregationOperation = null;
// sum up goal overview reports
$this->getProcessor()->aggregateDataTableRecords(
array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME),
self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME)),
$maximumRowsInDataTableLevelZero = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array());
$this->getProcessor()->processDependentArchive('Goals', VisitFrequencyAPI::NEW_VISITOR_SEGMENT);
$this->getProcessor()->processDependentArchive('Goals', VisitFrequencyAPI::RETURNING_VISITOR_SEGMENT);
}
}
|