Chapter 6

searchPhraseReportGoogle.pl

#!/usr/bin/perl
#----------------------------------#
#  PROGRAM:  Search Phrase Report  #
#----------------------------------#

$numArgs = $#ARGV + 1;

%googleDirCnt = ();

foreach $argnum (0 .. $#ARGV) {
   print "Processing $ARGV[$argnum] file\n\n";
   $LOGFILE = "$ARGV[$argnum]";
   open(LOGFILE) or die("Could not open log file: $ARGV[$argnum].");
   foreach $line (<LOGFILE>) {
     #do Google analysis
     if(($line =~ /q=/) && ($line =~ /google/)) { @tmp1 = split ('GET ',$line); @tmp2 = split (' ', $tmp1[1]); @tmp3 = split ('q=', $tmp1[1]); @tmp4 = split ('\&', $tmp3[1]); #do some cleanup $tmp4[0] =~ s/\+/ /; $tmp4[0] =~ s/\%20/ /g; $tmp4[0] =~ s/\%3C/\</gi; $tmp4[0] =~ s/\%3E/\>/gi; $tmp4[0] =~ s/\%23/\#/g; $tmp4[0] =~ s/\%22/\"/g; $tmp4[0] =~ s/\%25/\%/g; $tmp4[0] =~ s/\%3A/\:/gi; $tmp4[0] =~ s/\%2F/\//gi; $tmp4[0] =~ s/\%2B/\+/gi; @tmp5 = split ('\"', $tmp4[0]); $tmpKey = "<tr><td>".$tmp2[0]." </td><td>".$tmp5[0]."</td>"; $googleDirCnt{$tmpKey} = $googleDirCnt{$tmpKey} +1; } } close(LOGFILE); } open (FP, '>keywordsummary.html'); print FP "<html><head><title>Keyword Summary</title><head>"; print FP "<body><strong>Google Summary</strong>"; print FP "<table width=400><tr><td><b>Resource/URL</b></td><td><b>Keyword</b></td>"; print FP "<td><b>Count</b></td><tr>"; foreach $key (sort hashValueDescendingNum (keys(%googleDirCnt))) { print FP $key."<td>".$googleDirCnt{$key}."</td></tr>"; } print FP "</table></body></html>"; close (FP); sub hashValueDescendingNum { $googleDirCnt{$b} ...

Get SEO Warrior now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.