I’m a little confused as I’ve never come across Matomo before so not sure if the Device Detector is a plugin specifically for that the Matomo product or it can be used separately on other web apps.
If the later can anyone point me in the direction of how to get it working? I’ve downloaded the code from github and extracted it and put it in a directory under the webroot but how do I call it from a file at the root. There doesn’t appear to be any instructions for a numpty like me
Thanks
While device-detector powers a part of Matomo it can be used completly seperatly in your web app.
To set it up, I’d really recommend you to look into composer.
Then you’ll only need to run composer require piwik/device-detector and get the latest version and can load it (and every other library you install with composer) in your php script with require_once 'vendor/autoload.php';
I’ve installed composer and run the commands as described
C:\Users\flintadmin>composer require piwik/device-detector
Using version ^3.10 for piwik/device-detector
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing mustangostang/spyc (0.6.2): Downloading (100%)
- Installing piwik/device-detector (3.10.1): Downloading (100%)
piwik/device-detector suggests installing doctrine/cache (Can directly be used for caching purpose)
Writing lock file
Generating autoload files
I have a php file like this
<?php
require_once 'vendor/autoload.php';
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
?>
is that meant to display something?
FYI I’m really not a php developer but more server side admin and Lucee (http://lucee.org/) so any help is really appreciated
The code you posted is missing to important parts:
You are never setting $userAgent to the useragent you would like to detect.
You never output the result
So to fix this you can try something like this:
<?php
require_once 'vendor/autoload.php';
use DeviceDetector\Parser\Bot AS BotParser;
$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)) {
// do not do anything if a bot is detected
echo "I am not a bot";
} else {
var_dump($results);
}
The vender folder has been created by the composer command you ran above. Maybe you’ll need to change the require to point to the exact path. (Or run the composer command in your project directory)
After starring at the error message for a bit to long, I found the error. Do you by chance have also copied the following line:
// OPTIONAL: Set custom yaml parser
// By default Spyc will be used for parsing yaml files. You can also use another yaml parser.
// You may need to implement the Yaml Parser facade if you want to use another parser than Spyc or [Symfony](https://github.com/symfony/yaml)
$dd->setYamlParser(new DeviceDetector\Yaml\Symfony());
By default Device-Detecter uses Spyc (which gets installed with composer) to parse the Yaml files.
This optional line shows how you would instruct DeviceDetector to use another Yaml-Parser. (In this case the Symfony Yaml-Parser which you don’t have installed)
As the default parser should work in your use case, there is on need to set another parser.
the script now looks like this - having stripped out all the commented sections but it produces a blank screen
<?php
require_once 'vendor/autoload.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
// OPTIONAL: Set version truncation to none, so full versions will be returned
// By default only minor versions will be returned (e.g. X.Y)
// for other options see VERSION_TRUNCATION_* constants in DeviceParserAbstract class
DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);
$dd = new DeviceDetector($userAgent);
// OPTIONAL: If called, getBot() will only return true if a bot was detected (speeds up detection a bit)
$dd->discardBotInformation();
// OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
$dd->skipBotDetection();
$dd->parse();
if ($dd->isBot()) {
// handle bots,spiders,crawlers,...
$botInfo = $dd->getBot();
echo $botInfo;
} 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;
}
?>