HELLO
I AM
JOHN

I've been breaking making websites for over 15 years.

Simple Link Tracking with PHP – Part I

Posted on July 10, 2007 at 7:10 am

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:

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:

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:

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.

The next function also does as the function name implies.

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

Written by

John is a web-developer by day, dashing, handsome, and death-defying super-hero by night. Based out of Silver Spring, MD, he does his best to make things do things other than the things they were supposed to on a daily basis, in the process, making the world a better place.

There are 4 comments.

Comment by jordan

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

JUL20 2007 19:34:00
Comment by digifuzz

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! 😉

JUL23 2007 05:51:00
Comment by Damo

How would you show a link with more clicks than others in a certain div? Like i could show the hits and browsers in a div by placing your php code in that div but how to display the most ‘popular’ link per say?

Otherwise thank you very much and its appreciated.

NOV14 2013 13:21:00
Comment by ngita

Hello, thanks for the post, clear and just what i am looking for, i just wanna know where is the part two?

Thanks again

DEC19 2014 13:15:00

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

By submitting a comment you grant digifuzz.net a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.