»

Simple Link Tracking with PHP – Part I

This little tutorial will teach you how to create a simple out-going link tracker in PHP.

One of our clients had a rotating-advertisement script we had set him up with, and they decided they wanted to count the number of times each ad was clicked. Easy enough to do, but why just count clicks? There’s so much more information that would be beneficial to capture. Where was the user when the clicked on the ad? When? What browser were they using? In response to their request, I whipped up this little script. This script can be inserted just about anywhere you need to track out-going links.

This tutorial assumes that you already have a database and the appropriate connection already established.

First we set up the database. Create a table as follows:

  1.  
  2. CREATE TABLE `link_tracker` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `href` text NOT NULL,
  5. `user_agent` text NOT NULL,
  6. `referer` text NOT NULL,
  7. `timestamp` datetime NOT NULL DEFAULT ’0000-00-00 00:00:00′,
  8. PRIMARY KEY (`id`)
  9. )
  10.  

Easy enough right? This will allow us to track which link is clicked, browser/OS, what page the link was clicked on, and when the link was clicked. Just the basics, but enough to suit the purposes of our client.

Next, we take care of the actual link tracking. Create a file called “jump.php” and fill it with the following:

  1.  
  2. <?php
  3.   $jump_to_link = $_GET[‘href’];
  4.   $user_agent = $_SERVER[‘HTTP_USER_AGENT’];
  5.   $referer = $_SERVER[‘HTTP_REFERER’];
  6.  
  7.   $str_sql = "INSERT into link_tracker (href, user_agent, ".
  8.              "referer, timestamp) " .
  9.              "VALUES ( ‘" . $jump_to_link . "’, ‘" .
  10.                             $user_agent . "’, ‘" .
  11.                             $referer . "’, NOW() )";
  12.  
  13.   mysql_query( $str_sql ) or die( mysql_error() );
  14.  
  15.   header(‘Location: ‘ . $jump_to_link);
  16. ?>
  17.  

Thats it. Short and sweet. We’re capturing all the information we set out to. Next, you have to set up the links you want to track. This is the easy part. Change your links as follows:

  1.  
  2. <a href="./jump.php?href=http://your.real.link.here/index.html">
  3.   Click here
  4. </a>
  5.  

Assuming everything went correctly, you should now be tracking links. The next part is pulling the information. Now, I’m not going to write out a complete application for you, I’ll just give you two simple little functions to get you started. You have to take care of styling! Thats right, no hand-holding here!

The first function does just as its name implies. It shows you each link that has been clicked, and display the number
of times. Nothing more, nothing less.

  1.  
  2. <?php
  3. function show_hitcounts()
  4. {
  5.   $str_sql = ‘SELECT COUNT(id) as clicks, href from ‘;
  6.   $str_sql .= ‘link_tracker GROUP BY href’;
  7.  
  8.   $result = mysql_query( $str_sql ) or die (mysql_error() );
  9.  
  10.   while( $f_result = mysql_fetch_object( $result ) )
  11.   {
  12.     echo $f_result->href;
  13.     echo ",";
  14.     echo $f_result->clicks;
  15.     echo "<br/>";
  16.   }
  17. }
  18. ?>

The next function also does as the function name implies.

  1.  
  2. <?php
  3. function show_browsercounts()
  4. {
  5.   $str_sql = "SELECT COUNT(id) as clicks, user_agent from ";
  6.   $str_sql .= "link_tracker GROUP BY user_agent";
  7.  
  8.   $result = mysql_query( $str_sql ) or die (mysql_error() );
  9.  
  10.   while( $f_result = mysql_fetch_object( $result ) )
  11.   {
  12.     echo $f_result->user_agent;
  13.     echo ",";
  14.     echo $f_result->clicks;
  15.     echo "<br/>";
  16.   }
  17. }
  18. ?>

And thats it. Easy right? Simply call either of those functions on whatever page you want, and you will have the beginnings of a simple link-tracking system. Hope this helped someone out! Comment or suggestions?

~ digifuzz

  • Share/Bookmark

Posted on Tuesday, July 10th, 2007 at 7:10 am and is filed under Php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Simple Link Tracking with PHP – Part I”

  1. 1

    Fatal error: Call to undefined function: () in /www/site.com/php/jump.php on line 23

    line is

    $header(‘Location: ‘ . $jump_to_link);

    any idea the issue?

    thanks

  2. 2

    Yes, sorry! It appears I made a typo. The line should read:

    header(“location: ” . $jump_to_link);

    (the $ in front of header should not have been there). Sorry about that, my finger must have slipped! ;)

Leave a Reply