'I want to execute a function if the userAgent is GoogleBot

I am trying to execute some code if it's a crawler. Specifically Googlebot. This is how I am doing it but I feel there is a cleaner way.

I saw some posts which contained regex solutions but the answers didn't come with explanations. I don't use code I don't understand.

let agent = window.navigator.userAgent.indexOf("Googlebot") != -1;
if(agent) {
//DO Code
}  

Thanks in advance friends.



Solution 1:[1]

I was just looking for the answer for the same question, that's how I get here.

Actually the following code is working fine for me:

const agent = window.navigator.userAgent;

const botUserAgentsArray = [
            'googlebot',
            'bingbot',
            'linkedinbot',
            'mediapartners-google',
            'lighthouse',
            'insights',
];

var isBotUserAgent = 0;
for (var j = 0; j < botUserAgentsArray.length; j++){
    if (agent.toLowerCase().indexOf(botUserAgentsArray[j].toLowerCase()) !== -1){
        console.log("botUserAgent found: " + botUserAgentsArray[j].toLowerCase());
        isBotUserAgent = 1;
        break;
    }
}

if (isBotUserAgent == 1){
    //Do what you have to do
}

The explanation: The window.navigator.userAgent gives back the user agent of the client. When the a search crawler (like the Google bot) visits your page, you can get it's user agent with that code. It will give you back something like this:

Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36

The list of the Google Crawlers (User Agents): https://developers.google.com/search/docs/advanced/crawling/overview-google-crawlers

The Google bots have the string googlebot in it. That's what the following line in the code is checking:

agent.toLowerCase().indexOf(botUserAgentsArray[j].toLowerCase()) !== -1

So it checks if the googlebot (or the other bot) string is part of the user agent string. -1 means, not part, !== -1 means, not -1, which means, it's part of the script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lpasztor