How To Count Clicks In Your Website
Posted on 03 Feb 2000
by peter van dijck (pedrito)
Rated 3.91 (Ratings: 1)
- More articles in Code
Hi. What if you could count the amount of times visitors clicked on certain links on your site? You could track how often they click on that link to greenpeace, or how often they click on the links at the top of the page compared with those on the bottom of the page. You could even count how often they click on that animated gif you had set up as a link, or how many people you're sending to other sites.
Well, I'm here to tell you: it's easy! This is an Absolute Beginners™ tutorial.
What you'll need for this tutorial:
- a good kick in the butt (we all need that now and then)
What we'll make:
We'll make a click.php3 page, through which all clicks will go. It will log where the click came from and put that in a database. So your links will look like
where id will tell the click.php3 page where the link came from. This way, you can have a bunch of links all have the same id, and we keep it simple. Simple, as you know, makes us Happy.
Tricky bit: no more relative links like this: ../../page.html (We're keeping it simple)
Ok, let's get started. There's a nifty function in PHP (look it up) that's called header. It sends a header. It can be used to send the browser to another page, like this:
<?header("Location: http://evolt.org"); exit;?>
Try it out, upload it and go to the page. You'll be send straight to evolt.
Tricky bit: be careful not to put any spaces before the header function. This will force output and it won't work.
Cool hey? Now try this:
<?header("Location: http://$url"); exit;?>
Can you guess what this does? Upload it, and then go to it like this: click.php3?url=evolt.org You'll be sent straight to evolt.org. Niftyness!!!
Now you can see the possibilities here, right? We're gonna need a database to track all this counting going on. Good thing you've got mySQL installed!
Now, the following bit will be easier if you've done some databasing before. But if not, don't worry, you can do it! To make all this really easy, you can use a database interface like facemysql, available for free. Or you can do it all from the command line.
We'll set up a table in our database that looks like this:
fields(=columns): id / clicks
Yes, only 2 fields! (remember the Simple thing?)
To set up the database: we'll call the table clickcount:
id INT AUTO_INCREMENT NOT NULL PRIMARY_KEY, clicks INT
That's all. Now you need to make a bunch of rows and set the clicks all to 0(id=1,clicks=0; id=2,clicks=0; id=3 ... and so on). You can do that easily with facemySQL.
Have the database set up?
Good! That was the hard part. Here's the complete program, have a look at it:
<?//connect to dbase $db=mysql_connect("hostname","user","pass"); // username and password mysql_select_db("pedro",$db); // pedro is name of database // clickcount is the table, with 2 columns: id and clicks $sql="UPDATE clickcount SET clicks=clicks+1 WHERE (id = '$id')"; $result=mysql_query($sql,$db); header("Location: http://$url"); /* Redirect browser to web site */ exit; // just to be nice ?>
First, we update the database. And then, we send the user to the page he's going to for continued happiness in surfing.
And that's that for today. Cool hey?