Device detection using Matomo

I try to set device detection as universal detection script. How to manage PHP script as it will be an error: Uncaught Error: Class ‘BotParser’
init script

//use DeviceDetector\DeviceDetector;
//use DeviceDetector\Parser\Device\AbstractDeviceParser;

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
$userAgent = $_SERVER['HTTP_USER_AGENT']; 

$botParser = new BotParser();
$botParser->setUserAgent($userAgent);

$botParser->discardDetails();

$result = $botParser->parse();

if (!is_null($result)) {
    return;
}
$dd = new DeviceDetector($userAgent);

$dd->parse();
$clientInfo = $dd->getClient();
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
$brand = $dd->getBrandName();
$model = $dd->getModel();

echo 'Clint info: '.$clientInfo;
echo 'OS info: '.$osInfo;
echo 'Device info: '.$device;
echo 'Brand info: '.$brand;
echo 'Model: '.$model;

Source file: https://packagist.org/packages/matomo/device-detector

You need to use the class. As it states in their README:

use DeviceDetector\Parser\Bot AS BotParser;

I have checked again.
An error:
PHP Fatal error: Uncaught TypeError: Argument 1 passed to DeviceDetector\Parser\AbstractParser::setUserAgent() must be of the type string, null given, called in matomo/device-detector/Parser/AbstractParser.php:145


//use DeviceDetector\DeviceDetector;
//use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;

How are you testing this script?

Smarty design framework. Maybe it is Matomo issue and missing files.

Well no, the error is telling you that the method DeviceDetector\Parser\AbstractParser::setUserAgent() expects a string, but is given null instead.

So what that means is that somehow $_SERVER['HTTP_USER_AGENT'] is null. Which led me to believe you might be testing this on the CLI instead of in a browser. Hence the question of how you are testing this :slight_smile:

I have tested again and a little bit new code:

$userAgent="Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0";

$botParser = new BotParser();
$botParser->setUserAgent($userAgent);

// OPTIONAL: discard bot information. parse() will then return true instead of information
$botParser->discardDetails();

$result = $botParser->parse();

if (!is_null($result)) {
    return;
} else {
    var_dump($results);
}

Wh is it returned value NULL? It is the correct variable value:

$userAgent="Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0";

Because Firefox 59 is a real browser, not a bot. And the BotParser only knows bots.

What are you trying to detect?

If I understand we need to push also other library and this was mistake due to avoided

//use DeviceDetector\DeviceDetector;
//use DeviceDetector\Parser\Device\AbstractDeviceParser;


require_once 'vendor/autoload.php';
$userAgent = $_SERVER[‘HTTP_USER_AGENT’];

use DeviceDetector\Parser\Bot AS BotParser;

$botParser = new BotParser();
$botParser->setUserAgent($userAgent);

// OPTIONAL: discard bot information. parse() will then return true instead of information
$botParser->discardDetails();

$result = $botParser->parse();

if (!is_null($result)) {
    // do not do anything if a bot is detected
    return;
}

// handle non-bot requests

How to handle non-bot requests?
Is it correct the bottom code?

// handle non-bot requests
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDeviceName();
  $brand = $dd->getBrandName();
  $model = $dd->getModel();

$dd->isSmartphone();
$dd->isFeaturePhone();
$dd->isTablet();
$dd->isPhablet();
$dd->isConsole();
$dd->isPortableMediaPlayer();
$dd->isCarBrowser();
$dd->isTV();
$dd->isSmartDisplay();
$dd->isSmartSpeaker();
$dd->isCamera();
$dd->isWearable();
$dd->isPeripheral();

$dd->isBrowser();
$dd->isFeedReader();
$dd->isMobileApp();
$dd->isPIM();
$dd->isLibrary();
$dd->isMediaPlayer();