| Category: | CGI |
| Author/Contact Info | /msg sulfericacid |
| Description: | I finished a banner rotating script a few days ago with VERY little help from PM (which is a good thing.. it means I'm either learning to figure things out on my own or learning not to bug anyone :) ). That script gave me the idea to track link counts as well. I think this is the most useful script I've written so far, all you do is redirect the links on your site to something that looks like <a href="linktracker.pl?url=http://www.yoursite.com/yourfile.html"> and it'll count how many times it was clicked then redirect to the file. It's kind of nice knowing which sections of your pages are getting the most clicks. Things I've learned: Other: |
--------------------------------------------
linktracker.pl (file you redirect to)
#!/usr/bin/perl
#
# link tracker
#
use warnings;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
use POSIX;
use DB_File;
my %db;
my $db = "linktracker.db";
tie %db, "DB_File", "$db", O_CREAT | O_RDWR, 0644, $DB_BTREE
or die "Cannot open file 'db': $!\n";
my $url = url_param('url');
if (exists $db{$url}) {
my $value = $db{$url};
$value++;
$db{$url} = $value;
print "Location: $url\n\n";
}
else {
$db{$url} = 1;
print "Location: $url\n\n";
}
-------------------------------------------
linkcount.pl (stats page) #!/usr/bin/perl # # link counter # use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use POSIX; use DB_File; my %db; my $db = "linktracker.db"; tie %db, "DB_File", "$db", O_CREAT | O_RDWR, 0644, $DB_BTREE or die "Cannot open file 'db': $!\n"; print header, start_html; print "<center>"; print "<table><tr>"; print qq(<td bgcolor="000080"><b><font color=white>URL</font></b></td> +); print qq(<td bgcolor="000080"><b><font color=white>Click Count</font>< +/b></td></tr>); foreach (keys %db) { print "<td>$_ </td> <td><center>$db{$_}</center></td></tr>\n"; } print "</table>"; print "</center>"; |
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Link Tracker
by Anonymous Monk on Sep 13, 2003 at 17:00 UTC | |
|
Re: Link Tracker
by jeffa (Bishop) on Sep 13, 2003 at 18:03 UTC | |
by sulfericacid (Deacon) on Sep 13, 2003 at 21:25 UTC | |
by graff (Chancellor) on Sep 13, 2003 at 22:59 UTC | |
|
Re: Link Tracker
by Limbic~Region (Chancellor) on Sep 13, 2003 at 15:54 UTC |