Newer
Older
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
mattab
a validé
namespace Piwik\Plugins\DevicesDetection;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
use Piwik\DataTable;
mattab
a validé
use Piwik\Metrics;
/**
* The DevicesDetection API lets you access reports on your visitors devices, brands, models, Operating system, Browsers.
* @method static \Piwik\Plugins\DevicesDetection\API getInstance()
class API extends \Piwik\Plugin\API
{
/**
* @param string $name
* @param int $idSite
* @param string $period
* @param string $date
* @param string $segment
* @return DataTable
*/
protected function getDataTable($name, $idSite, $period, $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable($name);
mattab
a validé
$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS));
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
return $dataTable;
}
/**
* Gets datatable displaying number of visits by device type (eg. desktop, smartphone, tablet)
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getType($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_types', $idSite, $period, $date, $segment);
// ensure all device types are in the list
$this->ensureDefaultRowsInTable($dataTable);
mattab
a validé
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getDeviceTypeLogo'));
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getDeviceTypeLabel'));
return $dataTable;
}
protected function ensureDefaultRowsInTable($dataTable)
{
$requiredRows = array_fill(0, count(DeviceParserAbstract::getAvailableDeviceTypes()), Metrics::INDEX_NB_VISITS);
$dataTables = array($dataTable);
if (!($dataTable instanceof DataTable\Map)) {
foreach ($dataTables as $table) {
if ($table->getRowsCount() == 0) {
continue;
}
foreach ($requiredRows as $requiredRow => $key) {
$row = $table->getRowFromLabel($requiredRow);
if (empty($row)) {
$table->addRowsFromSimpleArray(array(
array('label' => $requiredRow, $key => 0)
));
}
}
}
}
}
/**
* Gets datatable displaying number of visits by device manufacturer name
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getBrand($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_brands', $idSite, $period, $date, $segment);
mattab
a validé
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getDeviceBrandLabel'));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getBrandLogo'));
return $dataTable;
}
/**
* Gets datatable displaying number of visits by device model
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getModel($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_models', $idSite, $period, $date, $segment);
mattab
a validé
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getModelName'));
return $dataTable;
}
/**
* Gets datatable displaying number of visits by OS family (eg. Windows, Android, Linux)
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getOsFamilies($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_os', $idSite, $period, $date, $segment);
// handle legacy archives
$this->checkForFallbackToOsVersions($dataTable, $idSite, $period, $date, $segment);
$dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\getOSFamilyFullName'));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getOsFamilyLogo'));
return $dataTable;
}
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
/**
* That methods handles teh fallback to os 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 ment 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.
*
* @param DataTable\DataTableInterface $dataTable
* @param $idSite
* @param $period
* @param $date
* @param $segment
*/
protected function checkForFallbackToOsVersions(DataTable\DataTableInterface &$dataTable, $idSite, $period, $date, $segment)
{
if ($dataTable instanceof DataTable\Map) {
$dataTables = $dataTable->getDataTables();
foreach ($dataTables as $date => &$table) {
if (!$table->getRowsCount()) {
$subTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$subTable->filter('GroupBy', array('label', function ($osWithVersion) {
if (strpos($osWithVersion, ';')) {
return substr($osWithVersion, 0, 3);
}
return $osWithVersion;
}));
$dataTable->addTable($subTable, $date);
}
}
} else if (!$dataTable->getRowsCount()) {
$dataTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$dataTable->filter('GroupBy', array('label', function ($osWithVersion) {
if (strpos($osWithVersion, ';')) {
return substr($osWithVersion, 0, 3);
}
return $osWithVersion;
}));
}
}
/**
* Gets datatable displaying number of visits by OS version (eg. Android 4.0, Windows 7)
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getOsVersions($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getOsLogo'));
sgiehl
a validé
// use GroupBy filter to avoid duplicate rows if old (UserSettings) and new (DevicesDetection) reports were combined
$dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\getOsFullName'));
return $dataTable;
}
/**
* Gets datatable displaying number of visits by Browser family (eg. Firefox, InternetExplorer)
mattab
a validé
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*
* @deprecated since 2.9.0 Use {@link getBrowsers} instead.
*/
public function getBrowserFamilies($idSite, $period, $date, $segment = false)
{
}
/**
* Gets datatable displaying number of visits by Browser (Without version)
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable
*/
public function getBrowsers($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browsers', $idSite, $period, $date, $segment);
// handle legacy archives
$this->checkForFallbackToBrowserVersions($dataTable, $idSite, $period, $date, $segment);
sgiehl
a validé
$dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\getBrowserName'));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getBrowserFamilyLogo'));
return $dataTable;
}
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
/**
* That methods handles teh fallback to browser 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 ment 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.
*
* @param DataTable\DataTableInterface $dataTable
* @param $idSite
* @param $period
* @param $date
* @param $segment
*/
protected function checkForFallbackToBrowserVersions(DataTable\DataTableInterface &$dataTable, $idSite, $period, $date, $segment)
{
if ($dataTable instanceof DataTable\Map) {
$dataTables = $dataTable->getDataTables();
foreach ($dataTables as $date => &$table) {
if (!$table->getRowsCount()) {
$subTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$subTable->filter('GroupBy', array('label', function ($browserWithVersion) {
if (preg_match("/(.+) [0-9]+(?:\.[0-9]+)?$/", $browserWithVersion, $matches) === 0) {
return $browserWithVersion;
}
return $matches[1];
}));
$dataTable->addTable($subTable, $date);
}
}
} else if (!$dataTable->getRowsCount()) {
$dataTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$dataTable->filter('GroupBy', array('label', function ($browserWithVersion) {
if (preg_match("/(.+) [0-9]+(?:\.[0-9]+)?$/", $browserWithVersion, $matches) === 0) {
return $browserWithVersion;
}
return $matches[1];
}));
}
}
/**
* Gets datatable displaying number of visits by Browser version (eg. Firefox 20, Safari 6.0)
* @param int $idSite
* @param string $period
* @param string $date
* @return DataTable
*/
public function getBrowserVersions($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getBrowserLogo'));
sgiehl
a validé
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getBrowserNameWithVersion'));
return $dataTable;
}
/**
* Gets datatable displaying number of visits by Browser engine (eg. Trident, Gecko, Blink,...)
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable
*/
public function getBrowserEngines($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable('DevicesDetection_browserEngines', $idSite, $period, $date, $segment);
// use GroupBy filter to avoid duplicate rows if old (UserSettings) and new (DevicesDetection) reports were combined
$dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\getBrowserEngineName'));
return $dataTable;
}