File: GoalSpecificProcessedMetric.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (108 lines) | stat: -rw-r--r-- 3,331 bytes parent folder | download | duplicates (2)
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Goals\Columns\Metrics;

use Piwik\API\Request;
use Piwik\DataTable\Row;
use Piwik\Piwik;
use Piwik\Plugin\ProcessedMetric;
use Piwik\Tracker\GoalManager;

/**
 * Base class for processed metrics that are calculated using metrics that are
 * specific to certain goals.
 */
abstract class GoalSpecificProcessedMetric extends ProcessedMetric
{
    /**
     * The ID of the goal to calculate metrics for.
     *
     * @var int
     */
    protected $idGoal;

    /**
     * The ID of the site the goal belongs to.
     *
     * @var int
     */
    protected $idSite;

    /**
     * Constructor.
     *
     * @param int|null $idSite The ID of the site the goal belongs to. If supplied, affects the formatting
     *                         and translated name of the metric.
     * @param int $idGoal The ID of the goal to calculate metrics for.
     */
    public function __construct($idSite, $idGoal)
    {
        $this->idSite = $idSite;
        $this->idGoal = $idGoal;
    }

    protected function getGoalMetrics(Row $row)
    {
        $allGoalMetrics = $this->getMetric($row, 'goals');
        if (isset($allGoalMetrics[$this->idGoal])) {
            return $allGoalMetrics[$this->idGoal];
        } else {
            $alternateKey = 'idgoal=' . $this->idGoal;
            if (isset($allGoalMetrics[$alternateKey])) {
                return $allGoalMetrics[$alternateKey];
            } elseif ($this->idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) {
                $alternateKey = GoalManager::IDGOAL_ORDER;
                if (isset($allGoalMetrics[$alternateKey])) {
                    return $allGoalMetrics[$alternateKey];
                }
            } elseif ($this->idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
                $alternateKey = GoalManager::IDGOAL_CART;
                if (isset($allGoalMetrics[$alternateKey])) {
                    return $allGoalMetrics[$alternateKey];
                }
            } else {
                return array();
            }
        }
    }

    protected static $goalsCache = [];

    protected function getGoalName(): string
    {
        if ($this->idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) {
            return Piwik::translate('Goals_EcommerceOrder');
        } elseif ($this->idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
            return Piwik::translate('Goals_AbandonedCart');
        }

        if (isset($this->idSite)) {
            if (!isset(self::$goalsCache[$this->idSite])) {
                self::$goalsCache[$this->idSite] = Request::processRequest(
                    'Goals.getGoals',
                    ['idSite' => $this->idSite, 'filter_limit' => '-1'],
                    $default = []
                );
            }
            return self::$goalsCache[$this->idSite][$this->idGoal]['name'] ?? '';
        } else {
            return '';
        }
    }

    protected function getGoalNameForDocs(): string
    {
        $goalName = $this->getGoalName();
        if ($goalName == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) {
            $goalName = '"' . $goalName . '"';
        }
        return $goalName;
    }
}