SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to count link clicks?

    I'd like to put a link on a webpage and show a count of the number of clicks that link has received.

    The link might look like this:

    SitePoint
    Clicks: 537
    How do I do that?

    I'm slowly teaching myself a little PHP, so I'm not looking for the full code. Some pointers on how to do it would be great?

    Thanks!
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,060
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Would the link be to an external site or a separate page with your site?

  3. #3
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pullo, it would link to an external site. I won't be able to add any code to the site it links to.
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,060
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Ok then.
    Well, one way to go about it would be to use JavaScript to add an onclick event listener to the link in question.
    When the link is clicked, this event listener could then fire off a quick AJAX request to a PHP script that increments your link click count by one (either in a txt file or database).
    Following this, you could then direct your visitor to their intended goal.

    You could use either JavaScript or PHP to add the "356 Clicks so far" text to the link on page load.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I think there are 2 solutions to it.

    1. You can save the click count in database and increment it with a small request when the link is clicked and before redirecting it to the external site.

    2. Upload a txt file on server and update it with the count, when the link is clicked.
    PHP Programming Its more than just writing code

  6. #6
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Ok then.
    Well, one way to go about it would be to use JavaScript to add an onclick event listener to the link in question....
    Thanks Pullo, I'm sorry I don't know Javascript, and will not be learning it, at least until I have a good grasp of PHP.

    Quote Originally Posted by Pullo View Post
    ..You could use either JavaScript or PHP to add the "356 Clicks so far" text to the link on page load.
    Thanks again, if I can figure out how to store the number of clicks in a variable, I'm comfortable with outputting that number on a HTML page.

    Quote Originally Posted by jimmybrion View Post
    Hello,

    I think there are 2 solutions to it.

    1. You can save the click count in database and increment it with a small request when the link is clicked and before redirecting it to the external site.

    2. Upload a txt file on server and update it with the count, when the link is clicked.
    Thanks Jimmy. With no. 2.

    "Upload a txt file on server and update it with the count"
    I know how to do that.

    "when the link is clicked"
    How would my script know when the link is clicked? To put it another way, if I put the code to update the count in a text file inside an if statement. What condition should I use for the if statement?

    Assuming the link gets a lot of clicks, would using a text file like that be an efficient way to do it, or would it slow down my site?
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,060
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Proudirish.com View Post
    "when the link is clicked"
    How would my script know when the link is clicked? To put it another way, if I put the code to update the count in a text file inside an if statement. What condition should I use for the if statement?
    I'm sorry to say that you won't get around using JavaScript for this.
    PHP is a server-side language that spits out a load of HTML to be served up by the browser, whereas JavaScript is a client-side language which can dynamically influence the way your user interacts with the page.

    Quote Originally Posted by Proudirish.com View Post
    Assuming the link gets a lot of clicks, would using a text file like that be an efficient way to do it, or would it slow down my site?
    Nah, probably not.
    If you get to the point where you have enough visitors to slow your site by clicking on this link, whether to use a text file or not will be the least of your worries.

    ---

    Edit:
    Just thinking out loud, you could code the link like this:

    HTML Code:
    <a href="linkcounter.php?www.sitepoint.com">Sitepoint</a>
    And have linkcounter.php update the count, grab your destination from the query string, then set the header to redirect you to your original goal.

  8. #8
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want something that's relatively easy to set up and monitors clicks and downloads - try 'Click Counter'
    I've been using it to count downloads for a few months and it appears to be reliable. A free version is available.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post

    Edit:
    Just thinking out loud, you could code the link like this:

    HTML Code:
    <a href="linkcounter.php?www.sitepoint.com">Sitepoint</a>
    And have linkcounter.php update the count, grab your destination from the query string, then set the header to redirect you to your original goal.
    Yes, I will also recommend the same.
    PHP Programming Its more than just writing code

  10. #10
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    ...And have linkcounter.php update the count, grab your destination from the query string, then set the header to redirect you to your original goal.
    Thanks, will try to do it that way.

    Quote Originally Posted by 2ndmouse View Post
    If you want something that's relatively easy to set up and monitors clicks and downloads - try 'Click Counter'
    I've been using it to count downloads for a few months and it appears to be reliable. A free version is available.
    Thanks for the suggestion, looks like it might be an option.
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency

  11. #11
    SitePoint Member
    Join Date
    Feb 2013
    Location
    Chandigarh, India, India
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are two ways to do this one save the click count in database and increment it with a small request when the link is clicked and before redirecting it to the external site and second is that Upload a txt file on server and update it with the count, when the link is clicked.


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
  •