device-detector
device-detector copied to clipboard
Detect if is a mobile Bot
Hi, thanks for this awesome library, my issue is with google bot, it has a Desktop and a Mobile bot, and the library is not recognizing Googlebot-mobile version, my site makes a redirection when is mobile to another subdomain, like m.site.com, and we all want google to index it property. So when isBot() == true, the isMobile() is always false, and thats a problem.
Thanks in advance.
https://developers.google.com/webmasters/mobile-sites/references/googlebot
Do you also need the detection if it is a bot? Otherwise maybe have a look at https://github.com/piwik/device-detector/blob/master/DeviceDetector.php#L275
Cooooll, it worked!!!
But it will be nice if both methods could work together.
thank you!
I agree with @henriqueoliveira, in my case it's necessary to detect mobile bots. At the moment i've solved this by creating two instances of DeviceDetector
and using properly the skipBotDetection
method.
Any news? How to solve this problem ?
hi, we are not considering the possibility yet. the following code may help you:
<?php
declare(strict_types=1);
require_once('vendor/autoload.php');
use DeviceDetector\DeviceDetector;
use DeviceDetector\ClientHints;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$clientHints = ClientHints::factory($_SERVER);
$dd = new DeviceDetector($userAgent, $clientHints);
$dd->parse();
if ($dd->isBot()) {
$botInfo = $dd->getBot();
$isBotBrowser = '' !== $clientHints->getArchitecture();
$isBotMobile = $clientHints->isMobile();
if (!$isBotMobile && preg_match('~(?:Android|-Mobile/|Mobile Safari/)~i', $userAgent)) {
$isBotMobile = true;
}
} else {
$clientInfo = $dd->getClient();
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
$brand = $dd->getBrandName();
$model = $dd->getModel();
}