Results 1 to 6 of 6

Thread: What is the best way to count clicks on links?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default What is the best way to count clicks on links?

    I would like to keep a running total of how many times people click on various links on the page so that I can see which vendors are most popular.

    Is the method shown at evolt.org the best way to do this?

    http://evolt.org/how_to_count_clicks_in_your_website

    Thanks for any advice on this. Mahalo.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are two basic ways to approach this:

    1. Set up code on each page that will save to a file. It will be easier to just see how many times people load a page, rather than click a link, but you could include a variable in the link as well if you actually want to track which link people are using (like "page.php?link=14", then on page.php have code to save that link 14 was used).
    It's easy enough-- just add some php to the top that will get the contents of a text file, add 1 to it, then save the file again. (Name the file name.php.txt, or something, to keep track.)

    2. Use a site-wide stats setup. I'm not sure how you'd do this yourself, but most hosts do have some sort of stats already there, so you just need to figure out how to access them and then you should be able to find things like most visited pages, etc., and likely stats on each page, maybe even on which pages frequently are used to go to other pages. If you want something more like this, google for packages and see what you can find. Installation would require some sort of server configuration, and some hosts may not give you enough access to install/change everything you need, depending on the stats package.


    Both are decent options, but it depends on what you want. In short, the first option will allow you simpler stats so you don't have to deal with a lot, if you just want to keep track of a few things. You must set it up and organize it manually, though, so that's a pain. The sitewide option is better if you want more information available and have a little more time to go through the data, and you can deal with setting up something a little more complex on the server.


    The only other way to approach this would be to run every "page" on your site through a single script (like how a forum works) and have that track activity. That might in some ways be the "best" except for the extreme hassle in setting it up, though it would give you the most control.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    I do have Google Analytics on the site but that is not what I am after. I have a field in the members table called `clicks` which I want to increment evey time a viewer clicks on that particular member's name. I want to be able to say to each member, 1213 (or whatever) people have clicked on your name in the member list and viewed your page. I want that total to be a discrete number I can pull from a database using php, not a trend I look at in a graph.

    It should be a simple thing. I just am not sure of the exact code to count the clicks.

  4. #4
    Join Date
    Jul 2009
    Location
    Coquitlam BC Canada
    Posts
    46
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    you could also use a database. when someone clicks on a link, update +1 in the database. I think you can do it like this:
    create a table called link_clicks
    fields:
    link_url varchar(255) primary key
    clicks int
    ______
    for the example: run query
    INSERT INTO link_clicks VALUES('http://www.google.com',0);
    ____________
    Code:
    <html>
    <head>
    <script>
    function click(url)
    {
    location.href="link.php?link=".url;
    }
    </script>
    </head>
    <body>
    <a href="http://www.google.com/" onclick="click(this.getAttribute('href'))">google</a>
    </body>
    </html>
    for link.php
    Code:
    <?
    /*connection data(your own) store connection in $cxn*/
    $sql="UPDATE link_click SET clicks+=1 WHERE link_url='$_GET[link]'";
    $sql2=mysql_query($sql);
    header('Location:'.$_GET[link]);
    ?>
    should work

  5. The Following User Says Thank You to fg123 For This Useful Post:

    kuau (09-26-2009)

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ok, then you will need to code this yourself, rather than using a sitewide stats app.
    As I sidenote, I completely forgot about google analytics, which is basically the same as anything you'd have on your server without the hassle of setting it up. (You won't have access to bandwidth or actual numbers from the server itself, but it will give a good overall picture.)

    As for the code you need, it's pretty simple... just have a bit of code on the page (profile) doing a hit-counter. You could even use one you can find on google, but they're often big and annoying. The easiest way to do it yourself is to setup a database (as above) and store the value for each user (if you want to mess with it, in the same table you already have for users-- add a column "profilehits" and start them all at 0). Then when the page loads just update+1 for that user in the database, like the code in the post above. Alternatively you could use files, but since it sounds like you're using dynamic database-driven pages already, that's a much better/cleaner way to do it. And it'll let you use the database later if you want to make the system more complex, rather than being stuck with a bunch of text files with numbers in them.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (09-26-2009)

  8. #6
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear Fg123 & Daniel:

    I finally figured it out. I'm embarassed to realize how simple it was to accomplish what I wanted. I was thinking about it backwards. It was really page loads I was after, but for some reason I was thinking I had to count the clicks on the previous page (duh!). All I had to do was add an UPDATE command to the result page.

    fg123, you gave an excellent solution to the exact question I asked, which made me realize that my thinking was off. But I do need to count clicks like that elsewhere so thanks very much for solving that one.

    Daniel, you seem to know better than I do what I am tryng to do. Mahalo plenty!! e

Bookmarks

Posting Permissions

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