<p>Yes, it would be possible. I don't think there's an easy (plugin) solution for this purpose, though. If your project has an active <a href="https://docs.craftcms.com/v3/extend/module-guide.html#preparation" rel="nofollow noreferrer">module</a>, you could use that to save the click count and retrieve it from the DB.</p>

<ul>
<li>Make sure your module works (in the default Craft project, it's as simple as uncommenting the module line in <code>config/app.php</code>)</li>
<li>Create a <a href="https://docs.craftcms.com/v3/extend/migrations.html#creating-migrations" rel="nofollow noreferrer">migration</a> to create a table in which you store the click counts (per url, or by ID if each link has a unique ID)</li>
<li>Create a model to interact with your table</li>
<li>Create a <a href="https://docs.craftcms.com/v3/extend/controllers.html#app" rel="nofollow noreferrer">controller</a> method to handle the clicks (get DB row for URL, add 1, save it)</li>
<li>Create a <a href="https://docs.craftcms.com/v3/extend/updating-plugins.html#template-variables" rel="nofollow noreferrer">template variable</a> to get the click count for a URL</li>
</ul>

<p>After that, all that's left is writing some JS to listen for link clicks and POST the URL to your controller method.</p>

<p>All in all, not very easy if you're new to PHP (be sure to read <a href="https://docs.craftcms.com/v3/extend/plugin-guide.html" rel="nofollow noreferrer">this</a> before you start), but a nice first dive into Craft plugin/module development.</p>

CC BY-SA 4.0
2020-03-16T17:40:45.253Z
Thank you for the reply!
Stephenio
CC BY-SA 4.0
2020-02-14T22:51:35.563Z
2020-02-14T22:51:35.563Z
Paul
<p>So I'm new to PHP and ok with Javascript. I have a page on my site with a long list of external links, would it be possible to add a counter to be displayed next to each link that shows how many times it has been clicked?</p>

<database><php><javascript><json>
Click counter for links (craft v2)
# Click counter for links (craft v2)

(Asked by: username_0 on 2020-02-12)

So I'm new to PHP and ok with Javascript. I have a page on my site with a long list of external links, would it be possible to add a counter to be displayed next to each link that shows how many times it has been clicked?

**Tags:** database, php, javascript, json

---

## 1 answers

### Answer by username_1 on 2020-02-14. Score: 1 | Accepted answer

Yes, it would be possible. I don't think there's an easy (plugin) solution for this purpose, though. If your project has an active [module](https://docs.craftcms.com/v3/extend/module-guide.html#preparation), you could use that to save the click count and retrieve it from the DB.

* Make sure your module works (in the default Craft project, it's as simple as uncommenting the module line in `config/app.php`)
* Create a [migration](https://docs.craftcms.com/v3/extend/migrations.html#creating-migrations) to create a table in which you store the click counts (per url, or by ID if each link has a unique ID)
* Create a model to interact with your table
* Create a [controller](https://docs.craftcms.com/v3/extend/controllers.html#app) method to handle the clicks (get DB row for URL, add 1, save it)
* Create a [template variable](https://docs.craftcms.com/v3/extend/updating-plugins.html#template-variables) to get the click count for a URL

After that, all that's left is writing some JS to listen for link clicks and POST the URL to your controller method.

All in all, not very easy if you're new to PHP (be sure to read [this](https://docs.craftcms.com/v3/extend/plugin-guide.html) before you start), but a nice first dive into Craft plugin/module development.

> **Comments:**
> **username_0**: Thank you for the reply!  

---

