-
Notifications
You must be signed in to change notification settings - Fork 70
Add AI bot classification for event enrichment #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaredmixpanel
wants to merge
5
commits into
master
Choose a base branch
from
feature/ai-bot-classification
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2438795
test: add AI bot classification tests
jaredmixpanel 72ab59a
feat: implement AI bot classifier
jaredmixpanel 2f93087
feat: add bot classification consumer wrapper
jaredmixpanel 645891b
chore: upgrade PHPUnit to v9 for PHP 8.4 compatibility
jaredmixpanel e9fbba6
fix: address PR review comments
jaredmixpanel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
| require_once(dirname(__FILE__) . "/AiBotDatabase.php"); | ||
|
|
||
| /** | ||
| * Classifies user-agent strings against a database of known AI bot patterns. | ||
| */ | ||
| class BotClassifier_AiBotClassifier { | ||
| /** @var array The bot patterns to check against (built-in + any custom) */ | ||
| private $_patterns; | ||
|
|
||
| /** | ||
| * @param array $additional_bots Optional additional bot patterns to prepend (checked first) | ||
| */ | ||
| public function __construct($additional_bots = array()) { | ||
jaredmixpanel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->_patterns = array_merge($additional_bots, BotClassifier_AiBotDatabase::getDatabase()); | ||
| } | ||
|
|
||
| /** | ||
| * Classify a user-agent string against the AI bot database. | ||
| * @param string|null $user_agent | ||
| * @return array Classification result with '$is_ai_bot' (always present) and optional | ||
| * '$ai_bot_name', '$ai_bot_provider', '$ai_bot_category' | ||
| */ | ||
| public function classify($user_agent) { | ||
| if ($user_agent === null || $user_agent === "" || !is_string($user_agent)) { | ||
| return array('$is_ai_bot' => false); | ||
| } | ||
| foreach ($this->_patterns as $bot) { | ||
| $match = @preg_match($bot["pattern"], $user_agent); | ||
| if ($match === false) { | ||
| continue; // Invalid regex, skip this pattern | ||
| } | ||
| if ($match) { | ||
| return array( | ||
| '$is_ai_bot' => true, | ||
| '$ai_bot_name' => $bot["name"], | ||
| '$ai_bot_provider' => $bot["provider"], | ||
| '$ai_bot_category' => $bot["category"] | ||
| ); | ||
| } | ||
jaredmixpanel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return array('$is_ai_bot' => false); | ||
| } | ||
|
|
||
| /** | ||
| * Create a classifier with optional additional bot patterns (checked before built-in). | ||
| * @param array $options Options array with optional 'additional_bots' key | ||
| * @return BotClassifier_AiBotClassifier | ||
| */ | ||
| public static function createClassifier($options = array()) { | ||
| $additional = isset($options["additional_bots"]) ? $options["additional_bots"] : array(); | ||
| return new BotClassifier_AiBotClassifier($additional); | ||
| } | ||
|
|
||
| /** | ||
| * Return bot database for inspection (no regex patterns exposed). | ||
| * @return array | ||
| */ | ||
| public function getBotDatabase() { | ||
| return BotClassifier_AiBotDatabase::getDatabaseForInspection(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
| /** | ||
| * Provides the AI bot user-agent pattern database. | ||
| * Each entry contains a regex pattern (for preg_match()), bot name, provider, category, and description. | ||
| */ | ||
| class BotClassifier_AiBotDatabase { | ||
| /** @var array The built-in AI bot pattern database */ | ||
| private static $_database = array( | ||
| // === OpenAI === | ||
| array("pattern" => "/GPTBot\//i", "name" => "GPTBot", | ||
| "provider" => "OpenAI", "category" => "indexing", | ||
| "description" => "OpenAI web crawler for model training data"), | ||
| array("pattern" => "/ChatGPT-User\//i", "name" => "ChatGPT-User", | ||
| "provider" => "OpenAI", "category" => "retrieval", | ||
| "description" => "ChatGPT real-time retrieval for user queries (RAG)"), | ||
| array("pattern" => "/OAI-SearchBot\//i", "name" => "OAI-SearchBot", | ||
| "provider" => "OpenAI", "category" => "indexing", | ||
| "description" => "OpenAI search indexing crawler"), | ||
| // === Anthropic === | ||
| array("pattern" => "/ClaudeBot\//i", "name" => "ClaudeBot", | ||
| "provider" => "Anthropic", "category" => "indexing", | ||
| "description" => "Anthropic web crawler for model training"), | ||
| array("pattern" => "/Claude-User\//i", "name" => "Claude-User", | ||
| "provider" => "Anthropic", "category" => "retrieval", | ||
| "description" => "Claude real-time retrieval for user queries"), | ||
| // === Google === | ||
| array("pattern" => "/Google-Extended\//i", "name" => "Google-Extended", | ||
| "provider" => "Google", "category" => "indexing", | ||
| "description" => "Google AI training data crawler (separate from Googlebot)"), | ||
| // === Perplexity === | ||
| array("pattern" => "/PerplexityBot\//i", "name" => "PerplexityBot", | ||
| "provider" => "Perplexity", "category" => "retrieval", | ||
| "description" => "Perplexity AI search crawler"), | ||
| // === ByteDance === | ||
| array("pattern" => "/Bytespider\//i", "name" => "Bytespider", | ||
| "provider" => "ByteDance", "category" => "indexing", | ||
| "description" => "ByteDance/TikTok AI crawler"), | ||
| // === Common Crawl === | ||
| array("pattern" => "/CCBot\//i", "name" => "CCBot", | ||
| "provider" => "Common Crawl", "category" => "indexing", | ||
| "description" => "Common Crawl bot (data used by many AI models)"), | ||
| // === Apple === | ||
| array("pattern" => "/Applebot-Extended\//i", "name" => "Applebot-Extended", | ||
| "provider" => "Apple", "category" => "indexing", | ||
| "description" => "Apple AI/Siri training data crawler"), | ||
| // === Meta === | ||
| array("pattern" => "/Meta-ExternalAgent\//i", "name" => "Meta-ExternalAgent", | ||
| "provider" => "Meta", "category" => "indexing", | ||
| "description" => "Meta/Facebook AI training data crawler"), | ||
| // === Cohere === | ||
| array("pattern" => "/cohere-ai\//i", "name" => "cohere-ai", | ||
| "provider" => "Cohere", "category" => "indexing", | ||
| "description" => "Cohere AI training data crawler"), | ||
| ); | ||
|
|
||
| /** @return array */ | ||
| public static function getDatabase() { | ||
| return self::$_database; | ||
| } | ||
|
|
||
| /** | ||
| * Returns database entries without regex patterns (safe for inspection). | ||
| * @return array | ||
| */ | ||
| public static function getDatabaseForInspection() { | ||
| $result = array(); | ||
| foreach (self::$_database as $entry) { | ||
| $result[] = array( | ||
| "name" => $entry["name"], | ||
| "provider" => $entry["provider"], | ||
| "category" => $entry["category"], | ||
| "description" => $entry["description"] | ||
| ); | ||
| } | ||
| return $result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
| require_once(dirname(__FILE__) . "/AbstractConsumer.php"); | ||
| require_once(dirname(__FILE__) . "/../BotClassifier/AiBotClassifier.php"); | ||
|
|
||
| /** | ||
| * Consumer wrapper that classifies AI bots in tracked event batches. | ||
| * Wraps any other consumer and enriches event data with bot classification | ||
| * properties when a user-agent string is present. | ||
| */ | ||
| class ConsumerStrategies_BotClassifyingConsumer extends ConsumerStrategies_AbstractConsumer { | ||
| /** @var ConsumerStrategies_AbstractConsumer */ | ||
| private $_innerConsumer; | ||
| /** @var BotClassifier_AiBotClassifier */ | ||
| private $_classifier; | ||
| /** @var string */ | ||
| private $_userAgentProperty = '$user_agent'; | ||
| /** @var array */ | ||
| private $_consumers = array( | ||
| "file" => "ConsumerStrategies_FileConsumer", | ||
| "curl" => "ConsumerStrategies_CurlConsumer", | ||
| "socket" => "ConsumerStrategies_SocketConsumer" | ||
| ); | ||
|
|
||
| function __construct($options = array()) { | ||
| parent::__construct($options); | ||
| $inner_key = isset($options["bot_classifying_inner_consumer"]) | ||
| ? $options["bot_classifying_inner_consumer"] : "curl"; | ||
| // NOTE: Do NOT merge $options["consumers"] into $_consumers here. | ||
| // The "consumers" key in $options is also consumed by Producers_MixpanelBaseProducer, | ||
| // and merging it would include "bot_classifying" => self, risking self-instantiation. | ||
| // Only the three hardcoded consumer types (file, curl, socket) are valid inner consumers. | ||
| $InnerClass = $this->_consumers[$inner_key]; | ||
| $this->_innerConsumer = new $InnerClass($options); | ||
| $additional_bots = isset($options["bot_additional_patterns"]) | ||
| ? $options["bot_additional_patterns"] : array(); | ||
| $this->_classifier = new BotClassifier_AiBotClassifier($additional_bots); | ||
jaredmixpanel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (isset($options["bot_user_agent_property"])) { | ||
| $this->_userAgentProperty = $options["bot_user_agent_property"]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Classify bot user-agents in each message and forward to inner consumer. | ||
| * @param array $batch | ||
| * @return boolean | ||
| */ | ||
| public function persist($batch) { | ||
| foreach ($batch as &$message) { | ||
| if (isset($message["properties"]) && isset($message["properties"][$this->_userAgentProperty])) { | ||
| $classification = $this->_classifier->classify($message["properties"][$this->_userAgentProperty]); | ||
| $message["properties"] = array_merge($message["properties"], $classification); | ||
| } | ||
| } | ||
| unset($message); | ||
| return $this->_innerConsumer->persist($batch); | ||
| } | ||
|
|
||
| /** @return int */ | ||
| public function getNumThreads() { | ||
| return $this->_innerConsumer->getNumThreads(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <phpunit backupGlobals="false" | ||
| backupStaticAttributes="false" | ||
| bootstrap="vendor/autoload.php" | ||
| cacheTokens="true" | ||
| colors="false" | ||
| convertErrorsToExceptions="true" | ||
| convertNoticesToExceptions="true" | ||
| convertWarningsToExceptions="true" | ||
| processIsolation="false" | ||
| stopOnFailure="false" | ||
| syntaxCheck="false" | ||
| verbose="false"> | ||
| verbose="false" | ||
| bootstrap="vendor/autoload.php"> | ||
|
|
||
| <testsuites> | ||
| <testsuite name="Mixpanel Test"> | ||
| <directory>./test/</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <filter> | ||
| <blacklist> | ||
| <directory>examples</directory> | ||
| <directory>vendor</directory> | ||
| <directory>test</directory> | ||
| </blacklist> | ||
| </filter> | ||
|
|
||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.