Skip to content

Commit

Permalink
Ticket #112 - Enhanced bot detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahype committed Mar 22, 2019
1 parent 47a83fb commit 043f1f5
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion inc/class-statify-frontend.php
Expand Up @@ -165,7 +165,7 @@ private static function _skip_tracking() {
);
if ( is_null( $user_agent )
|| false === $user_agent
|| ! preg_match( '/(?:Windows|Macintosh|Linux|iPhone|iPad)/', $user_agent ) ) {
|| self::is_bot( $user_agent ) ) {
return true;
}

Expand All @@ -175,6 +175,37 @@ private static function _skip_tracking() {
);
}

/**
* Checks if user agent is a bot.
*
* @since 1.7.0
*
* @param string $user_agent Server user agent string.
*
* @return boolean $is_bot TRUE if user agent is a bot, FALSE if not.
*/
private static function is_bot( $user_agent ) {
$user_agent = strtolower( $user_agent );

$identifiers = array(
'bot',
'slurp',
'crawler',
'spider',
'curl',
'facebook',
'fetch',
);

foreach ( $identifiers as $identifier ) {
if ( strpos( $user_agent, $identifier ) !== true ) {
return true;
}
}

return false;
}

/**
* Rules to detect internal calls to skip tracking and not print code snippet.
*
Expand Down

0 comments on commit 043f1f5

Please sign in to comment.