PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Plugin / ComputedMetric.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 2 years ago Dimension 2 years ago API.php 2 years ago AggregatedMetric.php 2 years ago ArchivedMetric.php 2 years ago Archiver.php 2 years ago Categories.php 2 years ago ComponentFactory.php 2 years ago ComputedMetric.php 2 years ago ConsoleCommand.php 2 years ago Controller.php 2 years ago ControllerAdmin.php 2 years ago Dependency.php 2 years ago LogTablesProvider.php 2 years ago Manager.php 2 years ago Menu.php 2 years ago MetadataLoader.php 2 years ago Metric.php 2 years ago PluginException.php 2 years ago ProcessedMetric.php 2 years ago ReleaseChannels.php 2 years ago Report.php 2 years ago ReportsProvider.php 2 years ago RequestProcessors.php 2 years ago Segment.php 2 years ago SettingsProvider.php 2 years ago Tasks.php 2 years ago ThemeStyles.php 2 years ago ViewDataTable.php 2 years ago Visualization.php 2 years ago WidgetsProvider.php 2 years ago
ComputedMetric.php
235 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Plugin;
10
11 use Piwik\Archive\DataTableFactory;
12 use Piwik\Columns\Dimension;
13 use Piwik\Columns\MetricsList;
14 use Piwik\Common;
15 use Piwik\DataTable;
16 use Piwik\DataTable\Row;
17 use Piwik\Metrics\Formatter;
18 use Piwik\Piwik;
19 class ComputedMetric extends \Piwik\Plugin\ProcessedMetric
20 {
21 const AGGREGATION_AVG = 'avg';
22 const AGGREGATION_RATE = 'rate';
23 /**
24 * @var string
25 */
26 private $aggregation;
27 /**
28 * @var int
29 */
30 protected $idSite;
31 private $name = '';
32 private $type = '';
33 private $translatedName = '';
34 private $documentation = '';
35 private $metric1 = '';
36 private $metric2 = '';
37 private $category = '';
38 private $metricsList;
39 public function __construct($metric1, $metric2, $aggregation = 'avg')
40 {
41 $nameShort1 = str_replace(array('nb_'), '', $metric1);
42 $nameShort2 = str_replace(array('nb_'), '', $metric2);
43 if ($aggregation === \Piwik\Plugin\ComputedMetric::AGGREGATION_AVG) {
44 $this->name = 'avg_' . $nameShort1 . '_per_' . $nameShort2;
45 } elseif ($aggregation === \Piwik\Plugin\ComputedMetric::AGGREGATION_RATE) {
46 $this->name = $nameShort1 . '_' . $nameShort2 . '_rate';
47 } else {
48 throw new \Exception('Not supported aggregation type');
49 }
50 $this->setMetric1($metric1);
51 $this->setMetric2($metric2);
52 $this->aggregation = $aggregation;
53 }
54 public function getDependentMetrics()
55 {
56 return array($this->metric1, $this->metric2);
57 }
58 public function setCategory($category)
59 {
60 $this->category = $category;
61 return $this;
62 }
63 public function getCategoryId()
64 {
65 return $this->category;
66 }
67 public function setMetric1($metric1)
68 {
69 $this->metric1 = $metric1;
70 return $this;
71 }
72 public function setMetric2($metric2)
73 {
74 $this->metric2 = $metric2;
75 return $this;
76 }
77 public function setDocumentation($documentation)
78 {
79 $this->documentation = $documentation;
80 return $this;
81 }
82 public function setTranslatedName($name)
83 {
84 $this->translatedName = $name;
85 return $this;
86 }
87 public function setType($type)
88 {
89 $this->type = $type;
90 return $this;
91 }
92 public function setName($name)
93 {
94 $this->name = $name;
95 return $this;
96 }
97 public function getName()
98 {
99 return $this->name;
100 }
101 public function compute(Row $row)
102 {
103 $metric1 = $this->getMetric($row, $this->metric1);
104 $metric2 = $this->getMetric($row, $this->metric2);
105 $precision = 2;
106 if ($this->aggregation === self::AGGREGATION_RATE) {
107 $precision = 3;
108 }
109 return Piwik::getQuotientSafe($metric1, $metric2, $precision);
110 }
111 private function getDetectedType()
112 {
113 if (!$this->type) {
114 if ($this->aggregation === self::AGGREGATION_RATE) {
115 $this->type = Dimension::TYPE_PERCENT;
116 } else {
117 $this->type = Dimension::TYPE_NUMBER;
118 // default to number
119 $metric1 = $this->getMetricsList()->getMetric($this->metric1);
120 if ($metric1) {
121 $dimension = $metric1->getDimension();
122 if ($dimension && $dimension->getType() != Dimension::TYPE_BOOL) {
123 $this->type = $dimension->getType();
124 }
125 }
126 }
127 }
128 return $this->type;
129 }
130 public function format($value, Formatter $formatter)
131 {
132 if ($this->aggregation === self::AGGREGATION_RATE) {
133 return $formatter->getPrettyPercentFromQuotient($value);
134 }
135 $type = $this->getDetectedType();
136 switch ($type) {
137 case Dimension::TYPE_MONEY:
138 return $formatter->getPrettyMoney($value, $this->idSite);
139 case Dimension::TYPE_FLOAT:
140 return $formatter->getPrettyNumber($value, $precision = 2);
141 case Dimension::TYPE_NUMBER:
142 return $formatter->getPrettyNumber($value, 1);
143 // we still need to round to have somewhat more accurate result
144 case Dimension::TYPE_DURATION_S:
145 return $formatter->getPrettyTimeFromSeconds(round($value), $displayAsSentence = true);
146 case Dimension::TYPE_DURATION_MS:
147 $val = round($value / 1000, $value / 1000 > 60 ? 0 : 2);
148 return $formatter->getPrettyTimeFromSeconds($val, $displayAsSentence = true);
149 case Dimension::TYPE_PERCENT:
150 return $formatter->getPrettyPercentFromQuotient($value);
151 case Dimension::TYPE_BYTE:
152 return $formatter->getPrettySizeFromBytes($value);
153 }
154 return $value;
155 }
156 public function getTranslatedName()
157 {
158 if (!$this->translatedName) {
159 $metric = $this->getMetricsList();
160 $metric1 = $metric->getMetric($this->metric1);
161 $metric2 = $metric->getMetric($this->metric2);
162 if ($this->aggregation === self::AGGREGATION_AVG) {
163 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric && $metric2 && $metric2 instanceof \Piwik\Plugin\ArchivedMetric) {
164 $metric1Name = $metric1->getDimension()->getName();
165 $metric2Name = $metric2->getDimension()->getName();
166 return Piwik::translate('General_ComputedMetricAverage', array($metric1Name, $metric2Name));
167 }
168 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
169 $metric1Name = $metric1->getDimension()->getName();
170 return Piwik::translate('General_AverageX', array($metric1Name));
171 }
172 if ($metric1 && $metric2) {
173 return $metric1->getTranslatedName() . ' per ' . $metric2->getTranslatedName();
174 }
175 return $this->metric1 . ' per ' . $this->metric2;
176 } else {
177 if ($this->aggregation === self::AGGREGATION_RATE) {
178 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
179 return Piwik::translate('General_ComputedMetricRate', array($metric1->getTranslatedName()));
180 } else {
181 return Piwik::translate('General_ComputedMetricRate', array($this->metric1));
182 }
183 }
184 }
185 }
186 return $this->translatedName;
187 }
188 public function getDocumentation()
189 {
190 if (!$this->documentation) {
191 $metric = $this->getMetricsList();
192 $metric1 = $metric->getMetric($this->metric1);
193 $metric2 = $metric->getMetric($this->metric2);
194 if ($this->aggregation === self::AGGREGATION_AVG) {
195 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric && $metric2 && $metric2 instanceof \Piwik\Plugin\ArchivedMetric) {
196 return Piwik::translate('General_ComputedMetricAverageDocumentation', array($metric1->getDimension()->getName(), $metric2->getTranslatedName()));
197 }
198 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
199 return Piwik::translate('General_ComputedMetricAverageShortDocumentation', array($metric1->getDimension()->getName()));
200 }
201 return Piwik::translate('General_ComputedMetricAverageDocumentation', array($this->metric1, $this->metric2));
202 } else {
203 if ($this->aggregation === self::AGGREGATION_RATE) {
204 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
205 return Piwik::translate('General_ComputedMetricRateDocumentation', array($metric1->getDimension()->getNamePlural(), $metric2->getDimension()->getNamePlural()));
206 } else {
207 return Piwik::translate('General_ComputedMetricRateShortDocumentation', array($this->metric1));
208 }
209 }
210 }
211 }
212 return $this->documentation;
213 }
214 private function getMetricsList()
215 {
216 if (!$this->metricsList) {
217 $this->metricsList = MetricsList::get();
218 }
219 return $this->metricsList;
220 }
221 public function beforeFormat($report, DataTable $table)
222 {
223 $this->idSite = DataTableFactory::getSiteIdFromMetadata($table);
224 if (empty($this->idSite)) {
225 $this->idSite = Common::getRequestVar('idSite', 0, 'int');
226 }
227 return !empty($this->idSite);
228 // skip formatting if there is no site to get currency info from
229 }
230 public function getSemanticType() : ?string
231 {
232 return $this->getDetectedType();
233 }
234 }
235