-
Notifications
You must be signed in to change notification settings - Fork 7
/
API.php
117 lines (99 loc) · 3.65 KB
/
API.php
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
<?php
/**
* Piwik - Open source web analytics
*
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
*/
namespace Piwik\Plugins\IPv6Usage;
/**
* The IPv6Usage API lets you access the IPv6 access statistics of your site.
*
*/
class API extends \Piwik\Plugin\API
{
/**
*/
public function getVisitEvolution($idSite, $period, $date)
{
\Piwik\Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getNumeric($idSite, $period, $date, 'IPv6Usage_IPv6');
return $dataTable;
}
public function getVisitsByProtocol($idSite, $period, $date)
{
\Piwik\Piwik::checkUserHasViewAccess($idSite);
$archive = \Piwik\Archive::build($idSite, $period, $date);
$ipv4 = $archive->getNumeric('IPv6Usage_IPv4');
$dataTable = new \Piwik\DataTable();
if ($ipv4) {
$newRow = new \Piwik\DataTable\Row();
$newRow->setColumns(array(
'label' => 'IPv4',
'nb_visits' => $ipv4
));
$dataTable->addRow($newRow);
}
$ipv6 = $archive->getNumeric('IPv6Usage_IPv6');
if ($ipv6) {
$newRow = new \Piwik\DataTable\Row();
$newRow->setColumns(array(
'label' => 'IPv6',
'nb_visits' => $ipv6
));
$dataTable->addRow($newRow);
}
$teredo = $archive->getNumeric('IPv6Usage_Teredo');
if ($teredo) {
$newRow = new \Piwik\DataTable\Row();
$newRow->setColumns(array(
'label' => 'Teredo',
'nb_visits' => $teredo
));
$dataTable->addRow($newRow);
}
$tun6to4 = $archive->getNumeric('IPv6Usage_Tun6to4');
if ($tun6to4) {
$newRow = new \Piwik\DataTable\Row();
$newRow->setColumns(array(
'label' => 'Tun6to4',
'nb_visits' => $tun6to4
));
$dataTable->addRow($newRow);
}
return $dataTable;
}
public function get($idSite, $period, $date, $segment = false, $columns = false)
{
\Piwik\Piwik::checkUserHasViewAccess($idSite);
$archive = \Piwik\Archive::build($idSite, $period, $date, $segment);
// array values are comma separated
$columns = \Piwik\Piwik::getArrayFromApiParameter($columns);
if (empty($columns)) {
$columns = array(
'IPv6Usage_IPv4',
'IPv6Usage_IPv6',
'IPv6Usage_Teredo',
'IPv6Usage_Tun6to4'
);
}
// We need to fetch uniq visits for processing
$columns[] = 'nb_visits';
$dataTable = $archive->getDataTableFromNumeric($columns);
$dataTable->filter('ColumnCallbackAddColumnPercentage', array('IPv6Usage_IPv4_rate', 'IPv6Usage_IPv4', 'nb_visits', 2));
$dataTable->filter('ColumnCallbackAddColumnPercentage', array('IPv6Usage_IPv6_rate', 'IPv6Usage_IPv6', 'nb_visits', 2));
$dataTable->filter('ColumnCallbackAddColumnPercentage', array('IPv6Usage_Teredo_rate', 'IPv6Usage_Teredo', 'nb_visits', 2));
$dataTable->filter('ColumnCallbackAddColumnPercentage', array('IPv6Usage_Tun6to4_rate', 'IPv6Usage_Tun6to4', 'nb_visits', 2));
// Delete column
$dataTable->deleteColumn('nb_visits');
return $dataTable;
}
protected function getNumeric($idSite, $period, $date, $toFetch)
{
\Piwik\Piwik::checkUserHasViewAccess($idSite);
$archive = \Piwik\Archive::build($idSite, $period, $date);
$dataTable = $archive->getNumeric($toFetch);
return $dataTable;
}
}