Device detection using Matomo and AGENT

I have tried to use Matomo GitHub script to detect device properties.
When I publish it is an error message:
DeviceDetector\Parser\AbstractParser->setUserAgent(NULL)

Use of undefined constant ‘HTTP_USER_AGENT’ - assumed ‘‘HTTP_USER_AGENT’’ (this will throw an Error in a future version of PHP).

Need help what is wrong with the code as it will not return device properties. Code is the following:

require_once ROOT_DIR . "vendor/autoload.php";
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;

and script:

//$userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0";
$userAgent = $_SERVER[‘HTTP_USER_AGENT’];

$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;echo $_SERVER['HTTP_USER_AGENT'];
} else {
    var_dump($results);echo '0';
    
$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;
    
}

Hi,

If you look at how the syntax highlighting of the forum colors the above line, you can already see the issue you are having: You are using typographical quotes instead of ’ or " to quote the HTTP_USER_AGENT which makes it invalid PHP code.

I have checked again.

It will return only Client info: ArrayOS info: ArrayDevice info: desktopBrand info: Model:
Why is an empty result?


$userAgent = $_SERVER['HTTP_USER_AGENT'];

$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)) {
   echo $_SERVER['HTTP_USER_AGENT'];

$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;
    
   return;
} else {
    var_dump($results);
    echo '0';
/*
$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;
*/ 
}

Hi,

What is the user agent you are sending?
Also e.g. $clientinfo is an object, so I am not sure if you can print it with an echo like this.

I’d recommend to use var_dump($clientInfo); instead.

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0array(6) {
[“type”]=>
string(7) “browser”
[“name”]=>
string(7) “Firefox”
[“short_name”]=>
string(2) “FF”
[“version”]=>
string(4) “90.0”
[“engine”]=>
string(5) “Gecko”
[“engine_version”]=>
string(4) “90.0”
}
OS info: ArrayDevice info: desktopBrand info: Model:

It seems it retrieves values for a client site under your valued suggestion: var_dump($clientInfo);. But why is not returned
OS info: ArrayDevice info: desktopBrand info: Model:
Is this composer library problem?

There is any demo version and I’m new to this.

Because this data is stored in $osInfo, etc.
So if you want to see them, you also have to specifically output them (e.g. var_dump($osinfo)).

I have solved a problem and have the last question.

If bot is detected and we keep the line, it will return TRUE value for the variable named $result?

$result = $botParser->parse();

and it will stop any code execution using

   return;

I do not understand why is executed IF sentence as it is not a bot and usual user Maybe I’m wrong.

Also, will this line $result = $botParser->parse(); block any search engine and indexing?

If you think an user agent is detected as a bot, but isn’t please share the user agent with me.

This code will not block anything. DeviceDetector just gives the type of device to you. Everything else is up to you to program.