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!

Hi there,

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

Thanks Pullo, it would link to an external site. I won’t be able to add any code to the site it links to.

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.

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 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.

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.

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?

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.

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:

<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.

1 Like

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.

Yes, I will also recommend the same.

Thanks, will try to do it that way.

Thanks for the suggestion, looks like it might be an option.

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.