Count the number of times an element has been viewed.
Basic tracking looks like this...
{% do craft.viewCount.increment(elementId) %}
However, there are several more advanced ways to increment a view counter.
If you need to track multiple aspects of the same element, it's easy to do so! Simply specify an optional key when setting up the tracking mechanism.
Once you've started logging views, you'll likely want to know which items have been viewed the most...
{% set articles = craft.entries.section('articles') %}
{% do craft.viewCount.sort(articles) %}
You can add a "Total Views" field, which displays a read-only total for each element.
They do exactly what you think...
- EVENT_BEFORE_VIEW
- EVENT_AFTER_VIEW
Within the EVENT_BEFORE_VIEW
event, you can prevent the view from being counted.
If you haven't already, flip through the complete plugin documentation.
And if you have any remaining questions, feel free to reach out to us (via Discord is preferred).
On behalf of Double Secret Agency, thanks for checking out our plugin! 🍺