Spyglasses provides AI traffic analytics for Drupal by detecting, blocking, and logging bot traffic from AI assistants and crawlers using Drupal's middleware API.
- AI Bot Detection: Detect traffic from ChatGPT, Claude, Perplexity, and other AI assistants
- AI Referrer Tracking: Track visitors coming from AI platforms
- Model Training Crawler Detection: Identify and optionally block AI training crawlers like GPTBot, CCBot, and ClaudeBot
- Middleware-Based: Uses Drupal's StackPHP middleware for high-performance request processing
- Configurable Blocking: Block or allow specific bots based on your preferences
- Real-time Analytics: Log all detected traffic to the Spyglasses platform for analysis
- Automatic Pattern Updates: Keep bot detection patterns up-to-date automatically
- Cache-Aware: Properly handles caching to avoid false positives
composer require spyglasses/drupal- Download the module from GitHub
- Extract to your
modules/contribdirectory - Enable the module via Drupal admin or drush:
drush en spyglassesSign up for a free account at spyglasses.io to get your API key.
- Navigate to Configuration > Web Services > Spyglasses (
/admin/config/services/spyglasses) - Enter your API key
- Configure your settings:
- Debug Mode: Enable for troubleshooting (logs detailed information)
- Auto-sync Patterns: Automatically update bot patterns daily
- Advanced Settings: Customize endpoints and cache settings if needed
Click "Sync Patterns Now" to fetch the latest bot detection patterns from the Spyglasses API.
Spyglasses uses Drupal's middleware API to intercept HTTP requests early in the request lifecycle. For each request:
- Pattern Matching: Checks the User-Agent against known AI bot patterns
- Referrer Detection: Analyzes the HTTP Referer for AI platform origins
- Blocking Decision: Applies your blocking rules (if configured)
- Logging: Sends detection data to Spyglasses for analytics
- Cache Headers: Sets appropriate cache headers to prevent false positives
- AI Assistants: ChatGPT-User, Claude-User, Perplexity-User, etc.
- AI Crawlers: GPTBot, ClaudeBot, CCBot, Applebot-Extended, etc.
- AI Referrers: Traffic from chat.openai.com, claude.ai, perplexity.ai, etc.
- Minimal Overhead: Middleware runs before full Drupal bootstrap
- Efficient Caching: Patterns cached for 24 hours by default
- Non-blocking Logging: API calls don't slow down your site
- Smart Exclusions: Skips static files and admin pages automatically
Configure blocking rules in your Spyglasses dashboard:
- Block AI Model Trainers: Block crawlers that train AI models
- Custom Block Rules: Block specific bots, categories, or patterns
- Custom Allow Rules: Always allow specific bots (overrides blocks)
Enable debug mode in the module settings to see detailed logs:
# View Drupal logs
drush watchdog:show --filter=spyglasses
# Or check the database
SELECT * FROM watchdog WHERE type = 'spyglasses' ORDER BY timestamp DESC;- Drupal: 8.8+ | 9.x | 10.x | 11.x
- PHP: 7.4+
- Extensions: cURL, JSON
- Permissions:
administer site configurationto configure
- Drupal Core Cache: ✅ Full support
- Varnish: ✅ Uses Vary headers
- Cloudflare: ✅ Compatible
- Redis/Memcache: ✅ Compatible
- BigPipe: ✅ Compatible
- Dynamic Page Cache: ✅ Compatible
- Internal Page Cache: ✅ Compatible
- Ban Module: ✅ Can be used together
// Get the Spyglasses client
$client = \Drupal::service('spyglasses.client');
// Detect bot traffic
$result = $client->detect($user_agent, $referrer);
// Sync patterns manually
$sync_service = \Drupal::service('spyglasses.pattern_sync');
$sync_service->syncIfNeeded();// Get configuration
$config = \Drupal::config('spyglasses.settings');
$api_key = $config->get('api_key');
$debug_mode = $config->get('debug_mode');No API Key Error
- Ensure your API key is set in the configuration
- Check that the key doesn't contain invalid characters
Patterns Not Syncing
- Verify your API key is valid
- Check network connectivity to spyglasses.io
- Enable debug mode to see detailed error messages
High Memory Usage
- Reduce cache TTL if you have many patterns
- Check for conflicting modules that might interfere
# Check module status
drush pm:list | grep spyglasses
# View recent logs
drush watchdog:show --filter=spyglasses --count=20
# Clear caches
drush cache:rebuild
# Test pattern sync
drush php:eval "\Drupal::service('spyglasses.pattern_sync')->syncIfNeeded();"# PHPUnit tests
vendor/bin/phpunit modules/contrib/spyglasses/tests/
# Code standards
vendor/bin/phpcs --standard=Drupal modules/contrib/spyglasses/- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This module processes HTTP requests and makes external API calls. Security considerations:
- API Key: Store securely, never commit to version control
- Input Validation: All user input is sanitized
- HTTPS: All API communication uses HTTPS
- Permissions: Requires admin permissions to configure
- Documentation: docs.spyglasses.io
- Issues: GitHub Issues
- Email: support@spyglasses.io
- Community: Drupal.org
This project is licensed under the GPL-2.0-or-later license. See the LICENSE file for details.
Made with ❤️ by Orchestra AI