1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 */
8namespace Piwik\Plugins\Goals\Columns\Metrics;
9
10use Piwik\DataTable\Row;
11use Piwik\Metrics\Formatter;
12use Piwik\Piwik;
13use Piwik\Plugins\Goals\Goals;
14use Piwik\Tracker\GoalManager;
15
16/**
17 * The conversion rate for a specific goal. Calculated as:
18 *
19 *     goal's nb_conversions / nb_visits
20 *
21 * The goal's nb_conversions is calculated by the Goal archiver and nb_visits
22 * by the core archiving process.
23 */
24class GoalConversionRate extends GoalSpecificProcessedMetric
25{
26
27    public function getName()
28    {
29        return Goals::makeGoalColumn($this->idGoal, 'conversion_rate');
30    }
31
32    public function getTranslatedName()
33    {
34        return Piwik::translate('Goals_ConversionRate', $this->getGoalName());
35    }
36
37    public function getDocumentation()
38    {
39        return Piwik::translate('Goals_ColumnConversionRateDocumentation', $this->getGoalNameForDocs());
40    }
41
42    public function getDependentMetrics()
43    {
44        return array('nb_visits', Goals::makeGoalColumn($this->idGoal, 'nb_conversions'));
45    }
46
47    public function format($value, Formatter $formatter)
48    {
49        return $formatter->getPrettyPercentFromQuotient($value);
50    }
51
52    public function compute(Row $row)
53    {
54        $nbVisits = $this->getMetric($row, 'nb_visits');
55        $conversions = $this->getMetric($row, Goals::makeGoalColumn($this->idGoal, 'nb_visits_converted'));
56
57        return Piwik::getQuotientSafe($conversions, $nbVisits, GoalManager::REVENUE_PRECISION + 2);
58    }
59}