|
|
How do I find out what searches people did to end up on my Web site?I've been trying to figure out whether there's a way that I can automate digging through the "referrals" on my site so I can see what searches people did to end up on one of my Web site pages. I'm running a Linux server and have Apache installed, so I get a huge log file with tons of info. But what I'd love is a simple script that will let me get email once a week with a sorted list of what searches people did to get to me. Doable? There are lots of great applications that you can install on your server to get traffic statistics, programs that are going to do a far better job letting you visualize what's going on than anything you can cobble together in a shell script. Further, there are also great utilities like Google Analytics that are free and quite easy to hook in (see: adding Google Analytics to your Web site). You had a pretty specific request, however, so let's have a look at how we could dig through the Apache log file to identify which hits are directly from Google and then how to extract them so that you get a clean summary in your mailbox. First off, to have something run on a regular schedule, we'll use the cron facility in Linux. It's one of the very best features of a Linux system and if you have a Linux system, learning crontab is time very, very well spent. But let's start at the beginning. You'll need to find where Apache is storing your log files, then you can just start out by searching for "google.com" with "grep". The output lines are llooonnnggg: $ grep google.com /home/www/logs/askdavetaylor.com-access_log | head -1
98.203.51.79 - - [01/Jun/2009:18:01:00 -0600] "GET /how_does_ebay_actually_work.html HTTP/1.1" 200 34599 "http://www.google.com/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial& channel=s&hl=en&q=how+does+ebay+work&btnG=Google+Search" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10" (I've added line breaks so it's more readable, but the output is one long line in reality) As you can see, there are many fields in this output, separated by spaces. If you count, space by space, you'll see that the REFERRER field is #11, so we can isolate it by using the "cut" command: $ grep google.com /home/taylor/www/logs/askdavetaylor.com-access_log | cut -f11 -d\ | head -1
"http://www.google.com/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial &channel=s& hl=en&q=how+does+ebay+work&btnG=Google+Search" That's a bit more readable. Now let's go further and observe that Google queries are name=value pairs separated by an ampersand (as are, of course, all CGI query URLs). Let's break the URL down and see what we get: $ grep google.com /home/taylor/www/logs/askdavetaylor.com-access_log | cut -f11 -d\ | head -1 | tr '&' '\012'
"http://www.google.com/search?client=firefox-a rls=org.mozilla%3Aen-US%3Aofficial channel=s hl=en q=how+does+ebay+work btnG=Google+Search" One more step and I think, by George, we have something: $ grep google.com /home/taylor/www/logs/askdavetaylor.com-access_log |cut -f11 -d\ | head -1 | tr '&' '\012' | grep "q="
q=how+does+ebay+work One heck of a command for a small bit of output, but once we tweak the "head -1" which has let us just work with one match, we can now quickly see, say, the 20 most recent searches ("head -20"): q=how+does+ebay+work
q=converting+wma+files+to+mp3 q=i+made+up+a+yahoo+email+address+for+myspace+now+i+cant+delete+it q=can+i+use+two+wireless+routers aq=0 oq=can+i+use+two+wireless+" q=how+do+you+put+music+on+a+psp aq=0 oq=how+do+you+put+music aq=0 oq=ask+dave q=ask+dave+taylor" Uh oh, looks like that "grep" pattern isn't sufficiently isolating. Instead we'll try "^q=" and the results are more what we seek: q=how+does+ebay+work
q=converting+wma+files+to+mp3 q=i+made+up+a+yahoo+email+address+for+myspace+now+i+cant+delete+it q=can+i+use+two+wireless+routers q=how+do+you+put+music+on+a+psp q=ask+dave+taylor" q=build+web+page+to+embed+youtube q=parallels+cant+see+my+other+partition+ q=psp+won%27t+play+games+because+it+says+they+corrupted,+what+to+do%3F q=broken+psp+screen q=comcast+remote+codes" q=how+can+i+get+on+myspace+at+school q=iphone+photos+to+mac q=installing+windows+on+bootcamp q=how+to+get+spades+on+myspace q=sony+psp+warranty+claim q=how+to+download+music+to+psp Interesting, but what about getting a useful report from it? We need to clean things up a bit (remove the "q=" and replace '+' with ' ') and we need to sort and tally things so that we can see the most common searches rather than every single search. This is done with "sed" and the power combination of "sort | uniq -c | sort -rn": $ grep google.com /home/taylor/www/logs/askdavetaylor.com-access_log |cut -f11 -d\ | head -20 | Still a few things to tweak, but let's finally strip out that "head" and look at all the searches people have done to get to the site: 108 convert wma to mp3 43 myspace at school 34 windows security alert 34 how to convert wma to mp3 33 virtual memory too low 26 how do i delete my myspace 26 google address book 26 comcast remote codes 24 converting wma to mp3 23 how to install windows on mac Nice. That's great information and ready to use. At least, ready enough for this quick and dirty solution. My resultant script, when I take the command sequence and drop it into a Bourne shell script file, is: #!/bin/sh
# Referrrers - shell script generates an email of popular referrer searchs from Google: logfile="/home/taylor/www/logs/askdavetaylor.com-access_log" max=15 echo "Log file analysis for $(basename $logfile):" echo "" grep google.com $logfile | \ cut -f11 -d\ | tr '&' '\012' | \ grep "^q=" | sed 's/q=//;s/+/ /g' | \ sort | uniq -c | sort -rn | head -$max exit 0 Now, finally, use "crontab -e" to add a line to cron that invokes this new script on a weekly basis. It brings up your favorite $EDITOR with your cron file within - if you have one. Crontab entries are in the form: minute, hour, day-of-month, month, day-of-week, command, so lets pick midnight on Mondays as our desired date and time. In crontab, that looks like: 0 0 * * Monday command
There are two ways we can structure the command itself. We can just invoke the script, in which case the script itself will have to deal with turning the output into an email message, or we can do that within the crontab entry itself: sh $SCRIPTS/referrers.sh | mail -s "Referrer report" taylor
That's all there is to it. Make sure "SCRIPTS" is defined earlier in the crontab file, save and quit the edits, and you're done. Tuesday morning you'll have a report in your inbox.
More Useful HTML, JavaScript and Web Site Programming Articles:
✔ How to Create Predefined Google Image Search Links?
Thanks for the Amazon URL [see Creating Amazon Search Links]. That worked beautifully. In fact, I sent you $5.00 for coffee in thanks....
✔ Can I embed a Facebook search box on my blog site?I've seen your articles about how to add a Twitter or Google search box on a Web page, but I have a tougher...
✔ Can I use CSS for drop shadows on my blog?I want to give my site a bit of a facelift and add some neat graphical elements. One of which is drop shadows....
✔ How can I embed interactive photo panoramas on my site/blog?I read through your blog entry about how to take panoramic photos with iOS 6 and an iPhone 5 and got enthused. I've...
✔ How can I create a Twitter search URL shortcut?I'd like to add a few Twitter search links to my Web site. Is that possible, or does Twitter prohibit this sort of...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Blogs and Blogging
,
HTML, JavaScript and Web Site Programming
,
Shell Script Programming
(Article 8917,
Written by Dave Taylor)
Tagged: Previous: How do I stop jerks bugging me on AOL's AIM? Next: CrossOver: A Must-Try Windows Tool for Mac Users Reader Comments To Date: 7David said, on June 6, 2009 9:58 AM:
Hey Dave, Looking it my logs, I find other google search referrers coming from other google domains than google.com. I see searches from google domains such as: google.de, google.it, google.co.uk and google.fr. I changed the initial grep from google.com to google and counted a bunch more search terms. David said, on June 6, 2009 11:53 AM:
Dave, Further analysis of my logs showed that some google sites are using "?q=" instead of "&q=". I find that livesearch and bing use "?q=". Yahoo is using "?p=". Using a modified version of your command, I'm able to show the search references from all of these sites. Dave Taylor said, on June 6, 2009 9:48 PM:
David, if you don't have any files on your site with the word "google" in their name, you can indeed just search for google, rather than google.com. I don't have that luxury: I have quite a few files that contain the word "google". In addition, yeah, since there's no way to know exactly what order the variables are going to be fed from the search box to the actual search engine, then "?q=" is just as likely as "&q=": the first is the very first variable on the list, whereas the second is when it's not the first. Thanks for the updates! :-) Ken Wynn said, on June 6, 2009 11:19 PM:
Thanks Dave. Great info. I just caught an interview you did with Affilorama. It's great to see someone with your longevity who still is passionate about what you do. It's true that if you love what you do, you never work a day in your life. Thanks. Michelle Rodriquez said, on June 14, 2009 10:09 AM:
My head was spinning after reading Dave's answer. Yes I'm not into programming that's why I use Google analytics. :-) wileeeb said, on October 2, 2009 2:38 PM:
Do you know how to block? delete? uninstall? bing? I've removed it as a search provider, yet, when I open MSN my computer uses BING instead of IE. Not once, ever, has bing helped me. I've had to switch to GOOGLE each time to get the help I needed quickly - thus - I would like to block bing permanently (without having to pay a hefty fee to a computer repair person).
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
Dave you never miss the mark on your scripts. They are always clean and well explained, thank you!