Skip to content

Commit

Permalink
Fix detecting device type by form factor of client hints (matomo-org#…
Browse files Browse the repository at this point in the history
…7843)

* Fix detecting device type by form factor of client hints
  • Loading branch information
Nommyde authored Sep 23, 2024
1 parent 2d18ce9 commit 4df1139
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Parser/Device/AbstractDeviceParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2047,11 +2047,11 @@ abstract class AbstractDeviceParser extends AbstractParser
protected static $clientHintFormFactorsMapping = [
'automotive' => self::DEVICE_TYPE_CAR_BROWSER,
'xr' => self::DEVICE_TYPE_WEARABLE,
'eink' => self::DEVICE_TYPE_TABLET,
'watch' => self::DEVICE_TYPE_WEARABLE,
'mobile' => self::DEVICE_TYPE_SMARTPHONE,
'tablet' => self::DEVICE_TYPE_TABLET,
'desktop' => self::DEVICE_TYPE_DESKTOP,
'eink' => self::DEVICE_TYPE_TABLET,
];

/**
Expand Down Expand Up @@ -2212,6 +2212,8 @@ public function parse(): ?array
}

if (empty($matches)) {
$this->deviceType = $resultClientHint['deviceType'] ?? null;

return $resultClientHint;
}

Expand Down Expand Up @@ -2294,21 +2296,19 @@ protected function buildModel(string $model, array $matches): string
protected function parseClientHints(): ?array
{
if ($this->clientHints && $this->clientHints->getModel()) {
$deviceType = null;
$formFactors = $this->clientHints->getFormFactors();
$detectedDeviceType = null;
$formFactors = $this->clientHints->getFormFactors();

if (\count($formFactors) > 0) {
foreach (self::$clientHintFormFactorsMapping as $formFactor => $deviceTypeId) {
if (\array_key_exists($formFactor, $formFactors)) {
$deviceType = self::getDeviceName($deviceTypeId);
foreach (self::$clientHintFormFactorsMapping as $formFactor => $deviceType) {
if (\in_array($formFactor, $formFactors)) {
$detectedDeviceType = $deviceType;

break;
}
break;
}
}

return [
'deviceType' => $deviceType,
'deviceType' => $detectedDeviceType,
'model' => $this->clientHints->getModel(),
'brand' => '',
];
Expand Down
13 changes: 13 additions & 0 deletions Tests/fixtures/unknown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3215,6 +3215,19 @@
browser_family: Chrome
headers:
http-x-requested-with: every.browser.inc
-
user_agent: Some Unknown UA
device:
type: "wearable"
brand: ""
model: "Some Unknown Model"
os: [ ]
client: null
os_family: Unknown
browser_family: Unknown
headers:
sec-ch-ua-form-factors: '"EInk", "Watch"'
sec-ch-ua-model: '"Some Unknown Model"'
-
user_agent: Microsoft Office Word/16.78.1008 (Desktop; Desktop app; Unknown/Unknown)
os: [ ]
Expand Down

0 comments on commit 4df1139

Please sign in to comment.