device-detector icon indicating copy to clipboard operation
device-detector copied to clipboard

Detect if is a mobile Bot

Open henriqueoliveira opened this issue 9 years ago • 6 comments

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.

henriqueoliveira avatar Aug 10 '15 18:08 henriqueoliveira

https://developers.google.com/webmasters/mobile-sites/references/googlebot

henriqueoliveira avatar Aug 10 '15 18:08 henriqueoliveira

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

sgiehl avatar Aug 10 '15 19:08 sgiehl

Cooooll, it worked!!!

But it will be nice if both methods could work together.

thank you!

henriqueoliveira avatar Aug 10 '15 20:08 henriqueoliveira

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.

marcosag avatar Feb 03 '16 15:02 marcosag

Any news? How to solve this problem ?

bozorkom avatar Nov 11 '22 16:11 bozorkom

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

sanchezzzhak avatar Nov 11 '22 22:11 sanchezzzhak