Skip to content

How to Track AI Visit

Not every request to your site comes from a person using a browser. Some web requests are generated by automated clients, including AI-related traffic. They may fetch pages, read content, collect data, train on published material from your site, or summarize it for their users somewhere else.

These type of clients usually identify themselves through the user-agent string, such as:

  • Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.0; +https://openai.com/gptbot)
  • Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
  • Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot)
  • ...

UAParser.js helps you parse these user-agent strings into structured data, while also provides functions to distinguish AI-related traffic from regular browsers and other types of bots.

Code Examples

js
import { UAParser } from 'ua-parser-js';
import { Bots } from 'ua-parser-js/extensions';
import { isAIAssistant, isAICrawler, isBot } from 'ua-parser-js/bot-detection';

const result = UAParser(Bots, req.headers);

if (isBot(result)) {
    if (isAICrawler(result)) {
        console.log('You are an AI Crawler Bot!');
    } else if (isAIAssistant(result)) {
        console.log('You are an AI Assistant Bot!');
    } else {
        console.log('You are a Bot!');
    }
} else {
    console.log('You might be a human or an unknown Bot!');
}
js
import { isAIAssistant, isAICrawler, isBot } from 'ua-parser-js/bot-detection';

const ua_firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0';
const ua_ahrefsBot = 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)';
const ua_searchGPT = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OAI-SearchBot/1.0; +https://openai.com/searchbot';
const ua_userGPT = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; ChatGPT-User/1.0; +https://openai.com/bot';

console.log(isBot(ua_firefox)); // false

console.log(isBot(ua_ahrefsBot)); // true
console.log(isAICrawler(ua_ahrefsBot)); // false

console.log(isBot(ua_searchGPT)); // true
console.log(isAICrawler(ua_searchGPT)); // true
console.log(isAIAssistant(ua_searchGPT)); // false

console.log(isBot(ua_userGPT)); // true
console.log(isAICrawler(ua_userGPT)); // false
console.log(isAIAssistant(ua_userGPT)); // true
js
import { UAParser } from 'ua-parser-js';
import { Crawlers } from 'ua-parser-js/extensions';

const ua = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.0; +https://openai.com/gptbot)";
const crawlerParser = new UAParser(Crawlers);
const browser = crawlerParser.setUA(ua).getBrowser();

console.log(browser);  
// { name: 'GPTBot', type: 'crawler', version: '1.0', major: '1' }

References:

UAParser.js v2 is licensed under AGPLv3 or PRO licenses.