putyourlightson / craft-entry-count

Counts and displays the number of times that an entry has been viewed in Craft CMS.
MIT License
47 stars 19 forks source link

How can I add a entry count if entries don’t have URLs? #24

Closed mateostabio closed 2 years ago

mateostabio commented 4 years ago

I have an entry structure that I'd like to get an entry count on. They are all living on a page as a list. When someone clicks on a list item, it doesn't go to the entry url, it goes to a document asset link in a new tab, I'd like to be able to add an entry count when the users clicks on a list item.

Is this something that is possible to do with this plugin in a link <a> tag?

bencroker commented 4 years ago

This would require either making an AJAX request to a template in which the increment takes place before redirecting, or by setting the link to an intermediate template that performs the increment and then forwards them onwards, for example:

{# Increment the entry count #}
{% set entryId = craft.app.request.get('entryId') %}
{% if entryId %}
    {% do craft.entryCount.increment(entry.id) %}
{% endif %}

{# Perform the redirect #}
{% set redirectUrl = craft.app.request.get('redirectUrl') %}
{% if redirectUrl %}
    {% redirect(redirectUrl) %}
{% endif %}