File size: 20,935 Bytes
096b791 | 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | <?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\DevicesDetection;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use Exception;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
use DeviceDetector\Parser\Client\Browser as BrowserParser;
use Piwik\Site;
/**
* The DevicesDetection API lets you access reports about your visitors' device types, brands, models,
* operating systems, and browsers.
*
* @method static \Piwik\Plugins\DevicesDetection\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* @param int|string|int[] $idSite
* @param string|null|false $segment
* @return DataTable|DataTable\Map
*/
protected function getDataTable(string $name, $idSite, string $period, string $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable($name);
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
return $dataTable;
}
/**
* Returns visit information grouped by device type.
*
* This report always includes every device type Matomo can detect, even if a type has zero visits.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The device type report with segment metadata for each device type.
*/
public function getType($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_types', $idSite, $period, $date, $segment);
// ensure all device types are in the list
$this->ensureDefaultRowsInTable($dataTable);
$mapping = AbstractDeviceParser::getAvailableDeviceTypeNames();
$dataTable->filter('AddSegmentByLabelMapping', ['deviceType', $mapping]);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getDeviceTypeLogo']);
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getDeviceTypeLabel']);
return $dataTable;
}
/**
* @param DataTable|DataTable\Map $dataTable
*/
protected function ensureDefaultRowsInTable($dataTable): void
{
$requiredRows = array_fill(0, count(AbstractDeviceParser::getAvailableDeviceTypes()), Metrics::INDEX_NB_VISITS);
$dataTable->filter(function (DataTable $table) use ($requiredRows) {
if ($table->getRowsCount() == 0) {
return;
}
foreach ($requiredRows as $requiredRow => $key) {
$row = $table->getRowFromLabel((string)$requiredRow);
if (empty($row)) {
$table->addRowsFromSimpleArray([
['label' => $requiredRow, $key => 0],
]);
}
}
});
}
/**
* Returns visit information grouped by device brand.
*
* Most device brand information is only available for non-desktop devices.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The device brand report with segment metadata for each detected brand.
*/
public function getBrand($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_brands', $idSite, $period, $date, $segment);
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getDeviceBrandLabel']);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getBrandLogo']);
$dataTable->filter('AddSegmentByLabel', ['deviceBrand']);
return $dataTable;
}
/**
* Returns visit information grouped by device model.
*
* This report is unavailable when device model detection is disabled by compliance policy.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The device model report with segment metadata for each brand/model pair.
*/
public function getModel($idSite, string $period, string $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$idSites = Site::getIdSitesFromIdSitesString($idSite);
// filter sites where model report disabled by compliance
$idSitesFiltered = array_filter($idSites, function ($idSite) {
return !DevicesDetection::isDeviceModelDetectionDisabledByCompliancePolicy((int)$idSite);
});
// show an error only if none of the requested sites is left
if (count($idSitesFiltered) === 0 && count($idSites) !== count($idSitesFiltered)) {
throw new Exception(Piwik::translate('DevicesDetection_DeviceModelReportDisabledByCompliancePolicy'));
}
$dataTable = $this->getDataTable('DevicesDetection_models', $idSitesFiltered, $period, $date, $segment);
$dataTable->filter(function (DataTable $table) {
foreach ($table->getRowsWithoutSummaryRow() as $row) {
/** @var string $label */
$label = $row->getColumn('label');
if (strpos($label, ';') !== false) {
[$brand, $model] = explode(';', $label, 2);
$brand = getDeviceBrandLabel($brand);
} else {
$brand = '';
$model = $label;
}
$segment = sprintf('deviceBrand==%s;deviceModel==%s', urlencode($brand), urlencode($model));
$row->setMetadata('segment', $segment);
}
});
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getModelName']);
return $dataTable;
}
/**
* Returns visit information grouped by operating system family.
*
* For legacy archives, this report falls back to operating system version data when needed.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The operating system family report with grouped family labels and logos.
*/
public function getOsFamilies($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_os', $idSite, $period, $date, $segment);
// handle legacy archives
if ($dataTable instanceof DataTable\Map || !$dataTable->getRowsCount()) {
$versionDataTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$dataTable = $this->mergeDataTables($dataTable, $versionDataTable);
}
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getOSFamilyFullName']);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getOsFamilyLogo']);
return $dataTable;
}
/**
* That methods handles the fallback to version datatables to calculate those without versions.
*
* Unlike DevicesDetection plugin now, the UserSettings plugin did not store archives holding the os and browser data without
* their version number. The "version-less" reports were always generated out of the "version-containing" archives .
* For big archives (month/year) that meant that some of the data was truncated, due to the datatable entry limit.
* To avoid that data loss / inaccuracy in the future, DevicesDetection plugin will also store archives without the version.
* For data archived before DevicesDetection plugin was enabled, those archives do not exist, so we try to calculate
* them here from the "version-containing" reports if possible.
*
* @template T of DataTable|DataTable\Map
*
* @param T $dataTable
* @param T $dataTable2
* @return T
*/
protected function mergeDataTables(DataTable\DataTableInterface $dataTable, DataTable\DataTableInterface $dataTable2)
{
if ($dataTable instanceof DataTable\Map && $dataTable2 instanceof DataTable\Map) {
$dataTables = $dataTable->getDataTables();
foreach ($dataTables as $label => $table) {
$versionDataTables = $dataTable2->getDataTables();
if (!array_key_exists($label, $versionDataTables)) {
continue;
}
$newDataTable = $this->mergeDataTables($table, $versionDataTables[$label]);
$dataTable->addTable($newDataTable, $label);
}
} elseif (!$dataTable->getRowsCount() && $dataTable2->getRowsCount()) {
$dataTable2->filter('GroupBy', ['label', function ($label) {
if (preg_match("/(.+) [0-9]+(?:\.[0-9]+)?$/", $label, $matches)) {
return $matches[1]; // should match for browsers
}
if (strpos($label, ';')) {
return substr($label, 0, 3); // should match for os
}
return $label;
}]);
return $dataTable2;
}
return $dataTable;
}
/**
* Returns visit information grouped by operating system version.
*
* Each row includes segment metadata for the operating system code and version.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The operating system version report with segment metadata and logos.
*/
public function getOsVersions($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$segments = ['operatingSystemCode', 'operatingSystemVersion'];
$dataTable->filter('AddSegmentByLabel', [$segments, Archiver::BROWSER_SEPARATOR]);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getOsLogo']);
// use GroupBy filter to avoid duplicate rows if old (UserSettings) and new (DevicesDetection) reports were combined
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getOsFullName']);
return $dataTable;
}
/**
* Returns visit information grouped by browser family without version numbers.
*
* For legacy archives, this report falls back to browser version data when needed.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The browser family report with grouped browser names and logos.
*/
public function getBrowsers($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browsers', $idSite, $period, $date, $segment);
$availableBrowsers = BrowserParser::getAvailableBrowsers();
$dataTable->filter('AddSegmentValue', [function ($label) use ($availableBrowsers) {
if (!array_key_exists($label, $availableBrowsers) && $label !== 'UNK') {
return false;
}
return $label;
}]);
// handle legacy archives
if ($dataTable instanceof DataTable\Map || !$dataTable->getRowsCount()) {
$versionDataTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$dataTable = $this->mergeDataTables($dataTable, $versionDataTable);
}
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getBrowserName']);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getBrowserFamilyLogo']);
return $dataTable;
}
/**
* Returns visit information grouped by browser version.
*
* Each row includes segment metadata for the browser code and detected version.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The browser version report with rewritten labels and logos.
*/
public function getBrowserVersions($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$segments = ['browserCode', 'browserVersion'];
$dataTable->filter('AddSegmentByLabel', [$segments, Archiver::BROWSER_SEPARATOR]);
$dataTable->filter('ColumnCallbackAddMetadata', ['label', 'logo', __NAMESPACE__ . '\getBrowserLogo']);
$dataTable->filter('ColumnCallbackReplace', ['label', __NAMESPACE__ . '\getBrowserNameWithVersion']);
return $dataTable;
}
/**
* Returns visit information grouped by browser engine.
*
* Row labels are normalized to the detected browser engine names.
*
* @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 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
* containing the specified date.
* @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|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map The browser engine report with grouped engine names.
*/
public function getBrowserEngines($idSite, string $period, string $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browserEngines', $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
// use GroupBy filter to avoid duplicate rows if old (UserSettings) and new (DevicesDetection) reports were combined
$dataTable->filter('GroupBy', ['label', __NAMESPACE__ . '\getBrowserEngineName']);
return $dataTable;
}
}
|