-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAPI.php
More file actions
70 lines (61 loc) · 2.99 KB
/
API.php
File metadata and controls
70 lines (61 loc) · 2.99 KB
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
<?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\Bandwidth;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Piwik;
/**
* Bandwidth API lets you request bandwidth metrics for page views and downloads.
*
* @method static \Piwik\Plugins\Bandwidth\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Returns bandwidth metrics for the requested site, period, and segment.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param string $period The period to process, processes data for the period containing the specified date.
* Allowed values: "day", "week", "month", "year", "range".
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth, lastYear),
* or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX, previousX).
* @param string|false $segment (Optional) Custom segment to filter the report.
* Example: "referrerName==twitter.com"
* Supports AND (;) and OR (,) operators.
* [See documentation:](https://developer.matomo.org/api-reference/reporting-api-segmentation)
* @param string|array|false $columns Optional metric columns to return. Accepts a comma-separated list,
* an array of metric names, or false to return all available metrics.
* @return DataTable|DataTable\Map Bandwidth metrics with integer values.
*/
public function get($idSite, $period, $date, $segment = false, $columns = false)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$columnNames = Metrics::getNumericRecordNameToColumnsMapping();
$archiveRecordNames = array_keys($columnNames);
$metricColumnNames = array_values($columnNames);
$dataTable = $archive->getDataTableFromNumeric($archiveRecordNames);
$dataTable->filter('ReplaceColumnNames', [$columnNames]);
$dataTable->filter(function (DataTable $dataTable) use ($metricColumnNames) {
foreach ($dataTable->getRows() as $row) {
foreach ($metricColumnNames as $metric) {
$row->setColumn($metric, (int)$row->getColumn($metric));
}
}
});
$requestedColumns = Piwik::getArrayFromApiParameter($columns);
$columnsToShow = $requestedColumns ?: $metricColumnNames;
$dataTable->queueFilter('ColumnDelete', [$columnsToRemove = [], $columnsToShow]);
return $dataTable;
}
}