Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Counting click-throughs

by Petras (Friar)
on Nov 12, 2003 at 05:36 UTC ( #306470=perlquestion: print w/replies, xml ) Need Help??

Petras has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks!

I'm helping a friend of mine with his website. He runs a non-profit organization whose website links to several websites orphanages and child protection/relief groups. Each link has a small banner for the organization linked to. He's asked me to find a way to track how many click throughs each link has recieved. I know how to write a CGI to track how many times each banner has been displayed, but I'm not sure how to use perl to track the click-through counts. Any ideas?


Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

Replies are listed 'Best First'.
Re: Counting click-throughs
by Aristotle (Chancellor) on Nov 12, 2003 at 05:42 UTC
    Well, not that this has anything to do with Perl, but the usual method for doing this is to use a script on the server that redirects to the link target. Instead of sending people to, you send them to where the link.cgi takes note of the click and then sends a redirect to the given target.

    Makeshifts last the longest.

Re: Counting click-throughs
by barrd (Canon) on Nov 12, 2003 at 14:43 UTC
    Hiya Petras,
    Thought I'd carry on a little from where Brer Aristotle left off. I've done many types of this over the years and would like to give a wee bit of advice. I started out by using flatfiles and the exact same technique as above to keep a count (the link went through a CGI opened the appropriate text file and then auto increment the number). However this gets messy pretty quickly and is hard to maintain.

    Step in relational Db. I use MySQL but any other SQL Db would suffice. the reason I do this is to allow better control via a backend admin script to control all the elements of the promotion. Below is a basic typical setup I might have for an SQL table containing the fields;

    • UID (Unique ID)
    • Name of promotion (or in your case charity)
    • Clicks (number of hits)
    • Image (usually just a name or path)
    • URL link
    • Header text - optional (HTML)
    • Footer text - optional (HTML)
    • Created (date promotion started)
    • LastMod (date link was last clicked in this case)
    • Active ("Yes" or "No")

    This means that your front end (HTML) page interacts directly with your back end admin script (via the Db) meaning that if you are capable of doing this (dynamic page building) not have to edit any hard coded HTML. I gave that up a "long time ago"... ;)

    Anyway hopefully that might have given you an insight to a more proactive way of handling this situation. If you have any other questions either reply to this post or /msg barrd in the ChatterBox.

    Best of luck...

Re: Counting click-throughs
by Theo (Priest) on Nov 12, 2003 at 20:25 UTC
    Check with his ISP. My ISP keeps stats. One set is call "Exit Pages", showing what page someone was viewing when they left my site. Sounds like exactly the thing you're looking for.

    Hmmm, maybe not. OK, try this: assuming you do have those kinds of stats available, have each link of interest point to a page that just does an immediate transfer to the site of interest. That page would be logged as the exit page and if appropriatly named, would tell him where the visitor was going.

    The refresh meta tag looks like this:

    <META HTTP-EQUIV="refresh" CONTENT="0; URL=">
    (This does seem like a clumsy work-around but I think it's workable)

    (so many nodes and so little time ... )
    Note: All opinions are untested, unless otherwise stated

Re: Counting click-throughs
by chanio (Priest) on Nov 13, 2003 at 06:02 UTC
    I was thinking of doing something similar to this.

    There is a more generic way, I think.

    Using the Redirect function of modperl. It is said that modperl works quicker that a normal script. So, I think that in your script you might have more time to check that the address of the redirect was one that you where wanting to count. Those interesting addresses might be in a DB and if they are not there, then you don't need to count those redirections...

      Time doesn't have to be an issue - when I write something like this, the script that is called to count does the redirect as the first thing, then closes STDOUT. At that point, the user is already redirected, and your script can take all the time it needs to analyse and record the click.

      -- zigdon

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://306470]
Approved by Aristotle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-10-19 21:25 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (207 votes). Check out past polls.