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.

So, bot should be detected and blocked inside the first IF sentence. Our goal is bot detection and block any contact form submissions.

An example:
Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)
(United States,US)
Is this the correct?

if (!is_null($result)) {
    // do not do anything if a bot is detected
    return;
   
}
else {
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDevice();
  $brand = $dd->getBrandName();
  $model = $dd->getModel();

  echo $model;
  echo $device;
}

No, if you want to know if DeviceDetector considers a request a bot, you have to check for $dd->isBot() or in your case $result->isBot().

Also keep in mind that DeviceDetector can only detect bots that explicitly mention that they are a bot and the ones spamming your contact form most likely won’t do that.

Thank you for the message. I’m checking link:

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

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

// handle non-bot requests
if ($dd->isBot()) {
  // handle bots,spiders,crawlers,...
  $botInfo = $dd->getBot();
} else {
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDeviceName();
  $brand = $dd->getBrandName();
  $model = $dd->getModel();
}

Can we make it above code if DeviceDetector considers a request a bot? If I understand

$botParser->discardDetails();

parse() will then return true instead of information and we will lose variable values for a bot:
$botInfo

Hi Lukas,
Is it possible to give us feedback if above code is the correct follow up.