[SOLVED] Counter for survey or page views

(Alexander Kappler) #1

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!

Irregular database error: "The optimistic lock on an entity failed"
[SOLVED] Controller for all "Page" node-typed elements
[SOLVED] Controller for all "Page" node-typed elements
(Christian Müller) #2

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:

(Bastian Waidelich) #3

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

(Alexander Kappler) #4

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

(Alexander Kappler) #6

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.