Device Detector Product


(Paul Hopkinson) #1

Hi, not really sure where to put this but I have a question on the Device Detector found here - GitHub - matomo-org/device-detector: The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model..

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 :slight_smile:
Thanks

Paul


(Lukas Winkler) #2

Hi,

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';


(Paul Hopkinson) #3

Hi Lukas,

Many thanks for responding.

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

Thanks

Paul


(Lukas Winkler) #4

Hi,

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);
}


(Paul Hopkinson) #5

thanks for coming back so quickly.

Running that gives me a 500 error so not displaying the actual error…

I noted an extra s in the results var so changed that but thats not fixed it.

Should there be a folder called vendor/ underneath where I’m running this script from?


(Paul Hopkinson) #6

ok solved the 500 error - missing a ; after I’m not a bot.

I’m getting
atal error: require_once(): Failed opening required ‘vendor/autoload.php’ (include_path=’.;.\includes;.\pear’)

so as per my question above - where does this folder live as I cant see it anywhere nor mentioned in any of the documentation that I can see


(Lukas Winkler) #7

Hi,

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)


(Paul Hopkinson) #8

ah ok got it - changed the folder and installed and it appears to process stuff now so thats really great :slight_smile:

Now I have :-
Fatal error: Class ‘DeviceDetector\DeviceDetector\Yaml\Symfony’ not found in xxx on line 24

Line 24 from the github download is

$dd->setYamlParser(new DeviceDetector\Yaml\Symfony());


(Fabian Dellwing) #9

You could try to run composer install and see if it installs anything.


(Paul Hopkinson) #10

no joy :frowning:
are you, or anyone who knows what they are doing, able to remote into my server and set it all up for me for a suitable fee?


(Paul Hopkinson) #11

any thoughts on this Lukas? Really want to try and get this product working but struggling at the moment.

Many thanks

Paul


(Lukas Winkler) #12

Hi,

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.


(Paul Hopkinson) #13

ah ok - that sorts that issue out.

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;
}

?>

(Paul Hopkinson) #14

Hi,

I’ve been away for a few days so not chased this up. Do you know why it would not be outputting the relevant data?

Thanks

Paul


(Lukas Winkler) #15

Sorry, forgot to answer:

You are never setting the $userAgent to the UserAgent you want to analyze.


(Paul Hopkinson) #16

oh - I was just copying the code from github…

What would I need to put to get the users device name and make/model?


(Paul Hopkinson) #17

ok i think I have cracked it!

$userAgent = $_SERVER[‘HTTP_USER_AGENT’];

That now outputs the relevant info !

Thanks so much Lukas for your help on this - hopefully will leave you in peace now

Best Regards

Paul