[SOLVED] Counter for survey or page views

Hi everyone,

I’m wondering what the best way would be to count how many times a page has been viewed to store it in the backend and use it for e.g. showing a list with the “most viewed” articles.

Another, similar question, I want to develop a survey/poll content element where I can add new question (nodetype) and show which question got the most clicks.

In both cases I’d add a property like “counter”. I’m now just curious what would be the best way to achieve this or if I have to develop a flow plugin for this.

Afterwards I’d use fusion to determine which article should be shown or prerender the results of the poll/survey.

Thank you everyone!

I haven’t got a fully fledged “do it like this answer”, but I would strongly advice against storing the counter in node properties. Maybe in a (persistent) cache or in a separate DB table, but not as property. It will give you trouble at some point :smiley:

1 Like

First of all you probably don’t want to update the view counter every time the page is loaded directly - that would basically mean that you can’t cache anything.

A safer solution is to decouple the counter from the page (node) and fire an AJAX request that updates some counter (and for the endpoint I would write an HTTP Component or invoke some controller directly).
You can always sync that back to some node property and IMO that makes sense especially if you want to sort after the number of views for example. But if you keep the “unique source of truth” separate, you can defer that and only sync every x minutes for example so that the node cache is not flushed all the time

Thank you for your feedback!
I thought that I might be able to do that with nodeTypes only, but your comments make definitely sense!


Here you go.
I have added here a first basic/simple version. Feel free to use it and make PRs with improvements.

You need a Javascript which makes the ajax requests but the rest should work.