Skip to content

HumanShapes/craft-viewcount

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

View Count plugin for Craft CMS

Count the number of times an element has been viewed.


Track views via Twig, PHP, or AJAX

Basic tracking looks like this...

{% do craft.viewCount.increment(elementId) %}

However, there are several more advanced ways to increment a view counter.

Allow multiple counters on the same element

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.

Sort by "most viewed"

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) %}

Display view totals in the control panel

You can add a "Total Views" field, which displays a read-only total for each element.

Events

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.


Anything else?

We've got other plugins too!

Check out the full catalog at doublesecretagency.com/plugins

About

View Count plugin for Craft CMS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 92.2%
  • Twig 7.6%
  • CSS 0.2%