-
Notifications
You must be signed in to change notification settings - Fork 19
/
API.php
295 lines (246 loc) · 12.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CustomDimensions;
use Piwik\Common;
use Piwik\DataTable\Row;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\Plugins\CustomDimensions\Dao\Configuration;
use Piwik\Plugins\CustomDimensions\Dao\LogTable;
use Piwik\Plugins\CustomDimensions\Dimension\Active;
use Piwik\Plugins\CustomDimensions\Dimension\CaseSensitive;
use Piwik\Plugins\CustomDimensions\Dimension\Dimension;
use Piwik\Plugins\CustomDimensions\Dimension\Extraction;
use Piwik\Plugins\CustomDimensions\Dimension\Extractions;
use Piwik\Plugins\CustomDimensions\Dimension\Index;
use Piwik\Plugins\CustomDimensions\Dimension\Name;
use Piwik\Plugins\CustomDimensions\Dimension\Scope;
use Piwik\Tracker\Cache;
/**
* The Custom Dimensions API lets you manage and access reports for your configured Custom Dimensions.
*
* @method static API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Fetch a report for the given idDimension. Only reports for active dimensions can be fetched. Requires at least
* view access.
*
* @param int $idDimension
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|false $segment
* @param bool|false $expanded
* @param bool|false $flat
* @param int|null $idSubtable
* @return DataTable|DataTable\Map
* @throws \Exception
*/
public function getCustomDimension($idDimension, $idSite, $period, $date, $segment = false, $expanded = false, $flat = false, $idSubtable = null)
{
Piwik::checkUserHasViewAccess($idSite);
$dimension = new Dimension($idDimension, $idSite);
$dimension->checkActive();
$record = Archiver::buildRecordNameForCustomDimensionId($idDimension);
$dataTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment, $expanded, $flat, $idSubtable);
if (isset($idSubtable) && $dataTable->getRowsCount()) {
$parentTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment);
foreach ($parentTable->getRows() as $row) {
if ($row->getIdSubDataTable() == $idSubtable) {
$parentValue = $row->getColumn('label');
$dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\AddSubtableSegmentMetadata', array($idDimension, $parentValue));
break;
}
}
} else {
$dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\AddSegmentMetadata', array($idDimension));
}
$dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\RemoveUserIfNeeded', array($idSite, $period, $date));
return $dataTable;
}
/**
* Configures a new Custom Dimension. Note that Custom Dimensions cannot be deleted, be careful when creating one
* as you might run quickly out of available Custom Dimension slots. Requires at least Admin access for the
* specified website. A current list of available `$scopes` can be fetched via the API method
* `CustomDimensions.getAvailableScopes()`. This method will also contain information whether actually Custom
* Dimension slots are available or whether they are all already in use.
*
* @param int $idSite The idSite the dimension shall belong to
* @param string $name The name of the dimension
* @param string $scope Either 'visit' or 'action'. To get an up to date list of availabe scopes fetch the
* API method `CustomDimensions.getAvailableScopes`
* @param int $active '0' if dimension should be inactive, '1' if dimension should be active
* @param array $extractions Either an empty array or if extractions shall be used one or multiple extractions
* the format array(array('dimension' => 'url', 'pattern' => 'index_(.+).html'), array('dimension' => 'urlparam', 'pattern' => '...'))
* Supported dimensions are eg 'url', 'urlparam' and 'action_name'. To get an up to date list of
* supported dimensions request the API method `CustomDimensions.getAvailableExtractionDimensions`.
* Note: Extractions can be only set for dimensions in scope 'action'.
* @param int|bool $caseSensitive '0' if extractions should be applied case insensitive, '1' if extractions should be applied case sensitive
* @return int Returns the ID of the configured dimension. Note that the same idDimension will be used for different websites.
* @throws \Exception
*/
public function configureNewCustomDimension($idSite, $name, $scope, $active, $extractions = array(), $caseSensitive = true)
{
Piwik::checkUserHasWriteAccess($idSite);
$this->checkCustomDimensionConfig($name, $active, $extractions, $caseSensitive);
$scopeCheck = new Scope($scope);
$scopeCheck->check();
$extractions = $this->unsanitizeExtractions($extractions);
$this->checkExtractionsAreSupportedForScope($scope, $extractions);
$index = new Index();
$index = $index->getNextIndex($idSite, $scope);
$configuration = $this->getConfiguration();
$idDimension = $configuration->configureNewDimension($idSite, $name, $scope, $index, $active, $extractions, $caseSensitive);
Cache::deleteCacheWebsiteAttributes($idSite);
Cache::clearCacheGeneral();
return $idDimension;
}
private function unsanitizeExtractions($extractions)
{
if (!empty($extractions) && is_array($extractions)) {
foreach ($extractions as $index => $extraction) {
if (!empty($extraction['pattern']) && is_string($extraction['pattern'])) {
$extractions[$index]['pattern'] = Common::unsanitizeInputValue($extraction['pattern']);
}
}
}
return $extractions;
}
/**
* Updates an existing Custom Dimension. This method updates all values, you need to pass existing values of the
* dimension if you do not want to reset any value. Requires at least Admin access for the specified website.
*
* @param int $idDimension The id of a Custom Dimension.
* @param int $idSite The idSite the dimension belongs to
* @param string $name The name of the dimension
* @param int $active '0' if dimension should be inactive, '1' if dimension should be active
* @param array $extractions Either an empty array or if extractions shall be used one or multiple extractions
* the format array(array('dimension' => 'url', 'pattern' => 'index_(.+).html'), array('dimension' => 'urlparam', 'pattern' => '...'))
* Supported dimensions are eg 'url', 'urlparam' and 'action_name'. To get an up to date list of
* supported dimensions request the API method `CustomDimensions.getAvailableExtractionDimensions`.
* Note: Extractions can be only set for dimensions in scope 'action'.
* @param int|bool|null $caseSensitive '0' if extractions should be applied case insensitive, '1' if extractions should be applied case sensitive, null to keep case sensitive unchanged
* @return int Returns the ID of the configured dimension. Note that the same idDimension will be used for different websites.
* @throws \Exception
*/
public function configureExistingCustomDimension($idDimension, $idSite, $name, $active, $extractions = array(), $caseSensitive = null)
{
Piwik::checkUserHasWriteAccess($idSite);
$dimension = new Dimension($idDimension, $idSite);
$dimension->checkExists();
if (!isset($caseSensitive)) {
$caseSensitive = $dimension->getCaseSensitive();
}
$extractions = $this->unsanitizeExtractions($extractions);
$this->checkCustomDimensionConfig($name, $active, $extractions, $caseSensitive);
$this->checkExtractionsAreSupportedForScope($dimension->getScope(), $extractions);
$this->getConfiguration()->configureExistingDimension($idDimension, $idSite, $name, $active, $extractions, $caseSensitive);
Cache::deleteCacheWebsiteAttributes($idSite);
Cache::clearCacheGeneral();
}
private function checkExtractionsAreSupportedForScope($scope, $extractions)
{
if (!CustomDimensions::doesScopeSupportExtractions($scope) && !empty($extractions)) {
throw new \Exception("Extractions can be used only in scope 'action'");
}
}
/**
* Get a list of all configured CustomDimensions for a given website. Requires at least Admin access for the
* specified website.
*
* @param int $idSite
* @return array
*/
public function getConfiguredCustomDimensions($idSite)
{
Piwik::checkUserHasViewAccess($idSite);
$configs = $this->getConfiguration()->getCustomDimensionsForSite($idSite);
return $configs;
}
/**
* For convenience. Hidden to reduce API surface area.
* @hide
*/
public function getConfiguredCustomDimensionsHavingScope($idSite, $scope)
{
$result = $this->getConfiguredCustomDimensions($idSite);
$result = array_filter($result, function ($row) use ($scope) { return $row['scope'] == $scope; });
$result = array_values($result);
return $result;
}
private function checkCustomDimensionConfig($name, $active, $extractions, $caseSensitive)
{
// ideally we would work with these objects a bit more instead of arrays but we'd have a lot of
// serialize/unserialize to do as we need to cache all configured custom dimensions for tracker cache and
// we do not want to serialize all php instances there. Also we need to return an array for each
// configured dimension in API methods anyway
$name = new Name($name);
$name->check();
$active = new Active($active);
$active->check();
$extractions = new Extractions($extractions);
$extractions->check();
$caseSensitive = new CaseSensitive($caseSensitive);
$caseSensitive->check();
}
/**
* Get a list of all supported scopes that can be used in the API method
* `CustomDimensions.configureNewCustomDimension`. The response also contains information whether more Custom
* Dimensions can be created or not. Requires at least Admin access for the specified website.
*
* @param int $idSite
* @return array
*/
public function getAvailableScopes($idSite)
{
Piwik::checkUserHasViewAccess($idSite);
$scopes = array();
foreach (CustomDimensions::getPublicScopes() as $scope) {
$configs = $this->getConfiguredCustomDimensionsHavingScope($idSite, $scope);
$indexes = $this->getTracking($scope)->getInstalledIndexes();
$scopes[] = array(
'value' => $scope,
'name' => Piwik::translate('General_TrackingScope' . ucfirst($scope)),
'numSlotsAvailable' => count($indexes),
'numSlotsUsed' => count($configs),
'numSlotsLeft' => count($indexes) - count($configs),
'supportsExtractions' => CustomDimensions::doesScopeSupportExtractions($scope)
);
}
return $scopes;
}
/**
* Get a list of all available dimensions that can be used in an extraction. Requires at least Admin access
* to one website.
*
* @return array
*/
public function getAvailableExtractionDimensions()
{
Piwik::checkUserHasSomeWriteAccess();
$supported = Extraction::getSupportedDimensions();
$dimensions = array();
foreach ($supported as $value => $dimension) {
$dimensions[] = array('value' => $value, 'name' => $dimension);
}
return $dimensions;
}
private function getTracking($scope)
{
return new LogTable($scope);
}
private function getConfiguration()
{
return new Configuration();
}
}