Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Glossary.php « API « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68a6ecf3faad2f0f9bceee8bb746964ec49f1d15 (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
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
 * Piwik - 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\API;

use Piwik\Metrics;

class Glossary
{
    protected $metadata = array();

    public function __construct(API $api)
    {
        $this->api = $api;
    }

    public function reportsGlossary($idSite)
    {
        $metadata = $this->api->getReportMetadata($idSite);

        $reports = array();
        foreach ($metadata as $report) {
            if (isset($report['documentation'])) {
                $reports[] = array(
                    'name' => sprintf("%s (%s)", $report['name'], $report['category']),
                    'documentation' => $report['documentation']
                );
            }
        }

        usort($reports, function ($a, $b) {
            return strcmp($a['name'], $b['name']);
        });

        return $reports;
    }

    public function metricsGlossary($idSite)
    {
        $metadata = $this->api->getReportMetadata($idSite);

        $metrics = array();
        foreach ($metadata as $report) {
            if (!isset($report['metricsDocumentation'])) {
                continue;
            }

            foreach ($report['metricsDocumentation'] as $metricId => $metricDocumentation) {

                $metricKey = $metricId;

                if(empty($report['metrics'][$metricId])
                    && empty($report['processedMetrics'][$metricId])) {
                    continue;
                }

                $metricName = isset($report['metrics'][$metricId]) ? $report['metrics'][$metricId] : $report['processedMetrics'][$metricId];


                // Already one metric with same name, but different documentation...
                if (isset($metrics[$metricKey])
                    && $metrics[$metricKey]['documentation'] !== $metricDocumentation) {

                    // Don't show nb_hits in glossary since it duplicates others, eg. nb_downloads,
                    if($metricKey == 'nb_hits') {
                        continue;
                    }

                    $metricName = sprintf("%s (%s)", $metricName, $report['category']);
                    $metricKey = $metricName;

                    if (isset($metrics[$metricKey]) && $metrics[$metricKey]['documentation'] !== $metricDocumentation) {
                        throw new \Exception(sprintf("Metric %s has two different documentations: \n(1) %s \n(2) %s",
                                $metricKey,
                                $metrics[$metricKey]['documentation'],
                                $metricDocumentation)
                        );
                    }
                } else {

                    if (!isset($report['metrics'][$metricId])
                        && !isset($report['processedMetrics'][$metricId])
                    ) {
                        // $metricId metric name not found in  $report['dimension'] report
                        // it will be set in another one
                        continue;
                    }

                }

                $metrics[$metricKey] = array(
                    'name' => $metricName,
                    'id' => $metricId,
                    'documentation' => $metricDocumentation
                );
            }
        }


        $metricsTranslations = Metrics::getDefaultMetricTranslations();
        foreach (Metrics::getDefaultMetricsDocumentation() as $metric => $translation) {
            if (!isset($metrics[$metric]) && isset($metricsTranslations[$metric])) {
                $metrics[$metric] = array(
                    'name' => $metricsTranslations[$metric],
                    'id' => $metric,
                    'documentation' => $translation
                );
            }
        }
        
        usort($metrics, function ($a, $b) {
            return strcmp($a['name'], $b['name']);
        });
        return $metrics;
    }
}