blob: fc4555df459ccea99b24cd26839876af0e6cfccd (
plain)
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
|
<?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\DataTable\Filter;
use Piwik\Plugins\Goals\Archiver as GoalsArchiver;
use Piwik\Archive;
use Piwik\DataTable\BaseFilter;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\Site;
class CalculateConversionPageRate extends BaseFilter
{
/**
* Constructor.
*
* @param DataTable $table The table to eventually filter.
*/
public function __construct($table)
{
parent::__construct($table);
}
/**
* @param DataTable $table
*/
public function filter($table)
{
// Find all goal ids used in the table and store in an array
$goals = [];
foreach ($table->getRowsWithoutSummaryRow() as $row) {
if (isset($row[Metrics::INDEX_GOALS])) {
foreach ($row[Metrics::INDEX_GOALS] as $goalIdString => $metrics) {
$goals[$goalIdString] = $goalIdString;
}
}
}
// Get the total top-level conversions for the goals in the table
$goalTotals = $this->getGoalTotalConversions($table, $goals);
if (count($goalTotals) === 0) {
return;
}
// Walk the rows and populate the nb_conversions_page_rate with nb_conversions_page_uniq / $goalTotals[goal id]
foreach ($table->getRowsWithoutSummaryRow() as &$row) {
if (isset($row[Metrics::INDEX_GOALS])) {
foreach ($row[Metrics::INDEX_GOALS] as $goalIdString => $metrics) {
if (isset($row[Metrics::INDEX_GOALS][$goalIdString][Metrics::INDEX_GOAL_NB_CONVERSIONS_PAGE_UNIQ])) {
$rate = Piwik::getQuotientSafe(
$row[Metrics::INDEX_GOALS][$goalIdString][Metrics::INDEX_GOAL_NB_CONVERSIONS_PAGE_UNIQ],
$goalTotals[$goalIdString],
3
);
// Prevent page rates over 100% which can happen when there are subpages
if ($rate > 1) {
$rate = 1;
}
$row[Metrics::INDEX_GOALS][$goalIdString][Metrics::INDEX_GOAL_NB_CONVERSIONS_PAGE_RATE] = $rate;
}
}
}
}
}
/**
* Get the conversions total for each goal in the top level datatable
*
* @param DataTable $table
* @param array $goalIds
* @return array
*/
private function getGoalTotalConversions(DataTable $table, array $goalIds): array
{
$goalTotals = [];
/** @var Site $site */
$site = $table->getMetadata('site');
if (empty($site)) {
return $goalTotals;
}
$idSite = $site->getId();
$period = $table->getMetadata('period');
$periodName = $period->getLabel();
$date = $period->getDateStart()->toString();
$date = ($periodName === 'range' ? $date . ',' . $period->getDateEnd()->toString() : $date);
$segment = $table->getMetadata('segment');
$archive = Archive::build($idSite, $periodName, $date, $segment);
foreach ($goalIds as $idGoal => $g) {
$total = $archive->getNumeric(GoalsArchiver::getRecordName('nb_conversions', $idGoal));
if (count($total)) {
$goalTotals[$idGoal] = reset($total);
}
}
return $goalTotals;
}
}
|