Dan Knight
- 2002.06.28
Last week we created our database for external links and figured
out how to display these links even when fields were empty, as
covered in Part 9 and Part 10 of this series. Now we take things a step
further.
I've been including external links on the Low End Mac home page
since April 1997, the month I began the
site. In all these years, I've had no way of knowing how often
visitors to the site follow those links or which links have been
the most popular.
Working with PHP and MySQL to manage external links, I now have
the opportunity to track how many times a link is clicked. I'll be
able to use that information to better understand what visitors to
the site find interesting.
Counting Clicks
The first step in counting clicks was creating a PHP script that
would use the unique ID of each record in our database, increment
the count for that record, and then open the desired page. Here's
the core of that code:
$array = mysql_fetch_array(mysql_query("SELECT URL,clicks FROM xlinks WHERE ID='$id'"));
$clicks = $array[clicks] + 1;
mysql_query("UPDATE xlinks SET clicks='$clicks' WHERE ID='$id'");
header("Location: $array[URL]");
This finds record X in the xlinks (external links) database,
reads the URL and number of clicks, adds one to the click count,
and then opens the desired page with the last line of code.
Of course, this means we have to go back and modify our display
code so that it calls up this PHP script (portal.php) instead of
leading directly to the external page. Where the earlier script
contained this line:
<a href=\"$array[URL]\">$array[linktext]</a>, ";
the revised script reads thus:
<a href=\"/scripts/portal.php?id=$array[ID]\">$array[linktext]</a>, ";
When I click on a link, it quickly calls up the portal script,
increments the count, and launches the desired page. It happens so
fast that you'll probably never notice the difference, but now I'll
be able to track how often these external links are followed.
Learned From Tracking Clicks
Since implementing this system, we've posted 60 external links.
The most clicked outside article was
Crouching Apple, hidden "Jaguar" on Extreme Tech,
which received 438 visits due to our link. Right behind it was
A battle PC giants should lose on osOpinion at 347.
The least clicked external link was
Apache anti-hack patch posted, which was only relevant to
those running their own servers on OS X or some other flavor of
Unix, with 27 clicks.
Overall, we're typically seeing 60-200 clicks to the articles we
link to. After all these year, it's nice to know how often these
links are being used.