How To Track GoogleBot Visits
Nov 4th, 2007 by yigber
SEO is a key element to marketing success, right ?
When you start doing SEO work on your pages, you wanna know as soon as possible when the Googlebot came to visit. It’s even more relevant when working on pages that have not been modified for a long time, and by now all the bots know they should revisit as late as possible.
Here’s some code that will generate an alert by email, whenever the Googlebot comes along. Actually this will track and alert a visit from any bot as long as it first accesses the robots.txt file.
It patch here works by triggering a php script called botalert.php whenever anyone tries to access robots.txt. All the php script does is send an email to a destination you specify. That’s it.
The Foolowing must be true:
- Apache
- Rewrite Engine on
- PHP
- ‘exec’ enabled on server
- read/write permission to .htaccess in documentroot
To make these alerts happen you have to perform these 2 steps:
1. Add a RewriteRule to the .htaccess in the server’s root.
2. Upload the botalert.php code to the server’s root.
Step 1
Edit the .htaccess file in the server’s root directory. Put in this directive:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule robots.txt botalert.php [L] </IfModule>
Step 2
Put this code in a file named botalert.php and place that script in the server’s root directory.
<?php
$pats = array (
"#(Googlebot)#i" => "Google"
,"#(Slurp)#i" => "Yahoo"
,"#(Moz)#i" => "unknown bot"
// put more patterns as you like...
);
$domain_name = $_SERVER['HTTP_HOST'];
$alert_email = "MY-EMAIL-ADDRESS";
foreach ($pats as $pat=>$botname) {
if (preg_match($pat,$_SERVER['HTTP_USER_AGENT'])) {
exec('echo "'.$botname.' came to visit" |mail -s "bot alert on '.strftime("%d/%m/%Y %H:%M:%S").' from '.$domain_name.'" '.$alert_email);
break;
}
}
header("HTTP/1.0 404 Not Found"); // unless you do have robots.txt, in which case do: passthru('robots.txt')
?>