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?


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, 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 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 method to handle the clicks (get DB row for URL, add 1, save it)
  • Create a template variable 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 before you start), but a nice first dive into Craft plugin/module development.

  • Thank you for the reply!
    – Stephenio
    Mar 16 '20 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.