dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: Counting hits on links

  1. #1
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    397

    Counting hits on links

    Hello all.

    I have a bunch of links on my page and I want to know how many times people clicked on each of them. I'd like to hear your opinions what would be the best way considering user comfort, development difficulty, server load and robustness.

    I thought of three possible ways:

    1) Add an onclick event for each of the links that would send an AJAX request to a counter application on my server. Here, I don't know if the user would have to wait for the counter app's response in case it was slow.

    2) Add an onclick event for each of the links that would store the information about which link was clicked to a cookie. The next time the use would hit the page, the information from the cookie would be collected. The downside I see is that so far, my page is static, which I consider a great advantage and I would have to give up on that.

    3) Add a layer. The links would not really point to the sites themselves but rather to something like my.domain.com/hitcounter/URL, which would save the hit and redirect the user. This would definitely be easy to do but the user comfort would be most hindered.

    What do you think?

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    1. It shouldn't have to but delivery wouldn't be guaranteed. Kinda like UDP vs TCP.
    2. #1 is no more static than this.
    3. Some variation on this would be best. Instead of adding a layer consider just incorporating it into the link via CGI or such.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  3. #3
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    397
    Quote Originally Posted by ray326
    Instead of adding a layer consider just incorporating it into the link via CGI or such.
    Thanks for the comment. I don't get what you mean by "incorporate it into the link" though.

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    href="cgi-bin/send?http://anothersite.com/page.html" and send would be an analyzer and redirector.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  5. #5
    Join Date
    Sep 2005
    Posts
    73
    Hi.
    Create a PHP (or asp) simple counter and save it as counter.php
    you can find a counter script here:
    http://www.stevedawson.com/article0007.php
    Make sure you change the 'count_file.txt' to $page

    Add to your links:
    onClick="count('PageName'); return false;"

    The calling function:
    function count(pn)
    {document.location = "counter.php?page=" + pn ;}

    ADD in the PHP page:
    $page = $_GET['page'] ;
    Open the log file name $page.".txt"
    read it, delete it, add 1 and rewrite it.
    and redirect to $page.

    GoodLuck!

  6. #6
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    397
    Quote Originally Posted by Lazer
    (Shortened)
    Create a simple counter.

    Add to your links:
    onClick="count('PageName'); return false;"

    The calling function:
    function count(pn)
    {document.location = "counter.php?page=" + pn ;}

    in the PHP page:
    Open the log file, add 1
    and redirect to $page.
    OK, if I got this correct then this would look like the link was normally pointing to the page (the status bar would show so) and with Javascript disabled, it would even work like that.

    Returning False from onclick would prevent from the link to be followed, rather, the counter/redirecter CGI would be activated. It has the advantage that I'd not have to dwelve into AJAX and the transparency of a normal link to the target page. The only downside as opposed to the AJAX method (presumably) is, that if my counter CGI was slow in response, then the user would have to wait.

    I think I'll go this route. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles