See Which Bots & AI Crawlers Visit Your React Site
In this article
This guide shows you how to identify which bots and AI crawlers are hitting your React site, so you can understand traffic patterns, block unwanted scrapers, or ensure your content is accessible to search engines. You'll set up a simple logging mechanism, then analyze the user-agent strings to classify each visitor.
What You'll Need
- A React site deployed to a server or platform where you can access server logs or add a serverless function (e.g., Vercel, Netlify, AWS).
- Basic familiarity with your hosting platform's logging or function setup.
- Optional: a database or external logging service (like a spreadsheet, Airtable, or a simple JSON file) to store the data.
Step-by-Step
- Decide where to capture requests. Client-side React code cannot reliably see bot traffic because many bots do not execute JavaScript. You need to log at the server level or in a serverless function that runs before your app loads.
- Create a serverless function (if using Vercel/Netlify). For Vercel, create a file like
api/log-bot.js. For Netlify, use a function innetlify/functions/log-bot.js. This function will receive the HTTP request, extract the User-Agent header, and log it. - Extract the User-Agent string. In your function, read the
user-agentheader from the request object. Example (Node.js):const ua = req.headers['user-agent'] || ''; - Determine if it's a bot or AI crawler. Check the User-Agent string against a list of known bot names. Common ones include:
Googlebot,Bingbot,GPTBot,ClaudeBot,PerplexityBot,Amazonbot,Applebot,facebookexternalhit, andTwitterbot. Use a regex or simple string includes to match. For example:const isBot = /bot|crawl|spider|GPTBot|ClaudeBot|PerplexityBot/i.test(ua); - Log the request details. If it's a bot, store the User-Agent, IP address (if available), timestamp, requested path, and any other relevant headers. You can write to a file, a database, or send to a logging service like Logflare or Axiom.
- Add the function to your site's routing. In your React app's
vercel.jsonornetlify.toml, configure a rewrite so that all requests first hit your logging function. For Vercel, add a rewrite like{"source": "/(.*)", "destination": "/api/log-bot"}but ensure it still serves your app after logging. Alternatively, you can use middleware in Next.js if your React app is built with Next.js. - Test the logging. Visit your site from a regular browser (you should see no bot log), then use a tool like
curl -A "GPTBot" https://yoursite.comto simulate a bot request. Check your logs to confirm it was captured. - Analyze the logs. After a few days, review the collected User-Agent strings. Group them by known bot names. You can create a simple script to count occurrences, or use a log analysis tool. Look for patterns: which bots visit most often, what paths they request, and how often they return.
- Take action based on findings. If you want to block certain bots, you can add rules in your server configuration or in your
robots.txtfile. If you want to allow them (for SEO), ensure your site is accessible and fast for those crawlers.
Common Mistakes
- Relying on client-side code: Bots often don't run JavaScript, so you'll miss them if you try to log from React components.
- Using a too-broad regex: Matching every User-Agent containing "bot" can catch legitimate browsers like Chrome's "HeadlessChrome" (which is actually a bot). Be specific with known names.
- Not logging enough information: Just logging the User-Agent isn't enough to understand behavior. Include the requested URL, status code, and timestamp.
- Blocking all bots: Some bots are essential for SEO (Google, Bing). Blocking them can harm your search rankings.
Tips
- Use a maintained list: Instead of hardcoding bot names, use a package like
isbot(npm) which has an up-to-date list of known bots. - Store logs in a structured format: Use JSON lines or a database table so you can query easily later.
- Add a robots.txt: Even if you log bots, you should have a
robots.txtfile that tells well-behaved bots which paths to crawl. This reduces unwanted traffic. - Monitor regularly: Bot traffic can change. Review logs weekly to spot new AI crawlers.
- Consider using a CDN or edge function: If you use Cloudflare or similar, you can log bot requests at the edge without affecting your serverless function limits.
Troubleshooting
- No logs appear at all: Check that your serverless function is actually being invoked. Add a simple console.log and check your platform's function logs. Also ensure the rewrite rule is correct and not conflicting with static assets.
- Only some bots are logged: Your regex might be missing some. Test with a variety of User-Agent strings. Use a tool like useragentstring.com to see real examples.
- Logs are too large: If you're logging every request, you'll generate a lot of data. Filter to only log when the User-Agent matches a bot pattern, and consider sampling (log every 10th request).
- Bots are still hitting your site even after you block them: Some bots ignore robots.txt. You may need to block at the server/firewall level using IP ranges or User-Agent filtering in your hosting provider's settings.
- Function timeout: If your logging function takes too long, it can slow down your site. Keep the logging logic minimal and asynchronous where possible.
Frequently Asked Questions
Can I see bots from client-side React code?
No, because most bots do not execute JavaScript. You must log at the server level or in a serverless function.
What is the difference between a bot and an AI crawler?
All AI crawlers are bots, but not all bots are AI crawlers. AI crawlers specifically collect data to train large language models (e.g., GPTBot, ClaudeBot). Other bots include search engine crawlers (Googlebot) and social media scrapers.
How do I know if a User-Agent is fake?
Some bots spoof legitimate User-Agents. To verify, you can do a reverse DNS lookup on the IP address. If it doesn't match the claimed bot, it's likely spoofed.
Should I block AI crawlers?
That's a business decision. If you don't want your content used for AI training, you can block them via robots.txt or server rules. However, some AI crawlers may also help with SEO (e.g., PerplexityBot drives traffic to your site).
How can I see which pages bots are crawling?
Log the request path in your function. Then analyze which paths have the most bot hits. This helps you see if bots are focusing on your most important content.
Do I need a database to store logs?
No, for small sites you can write to a file or use a free logging service. For larger sites, a database or a dedicated log management tool is better.