Display page hits/view counter on page

Is there an existing snippet of extra that allows you to display page hits/views on the front-end? I have tried HitsPage, but I’m not sure there’s an ability to store the value as a placeholder and use it elsewhere, which I need. I’ve also tried MostPopular, but there is very limited documentation on how to get it working for page views.

Well, HitsPage stores the value inside a template variable. You can easily display it on the frontend (on the same resource), as mentioned here.

You could also use snippets like pdoResources and print the TV in the template, or even just something like

$res = $modx->getObject('modResource', $myid); //or a getCollection for multiple
//do stuff
return $res->getTVValue('hits);
1 Like

Could you provide steps as well as sample code? I’ve tried using HitsPage and tried creating a TV that stores the value and display it, but I haven’t been able to get it to work. That documentation states that:

Upon installation, HitsPage creates two new Template Variables, “HitsPage” and “hits”. At the time of this writing, the “hits” TV is redundant.

however, no template variables got created upon my installation.

What MODX version are you using?

I’m using MODX version 3.0.1-pl

This documentation seems to be outdated.
Use the instructions on the Github page.

By default “HitsPage” doesn’t use a TV, but stores the value in the properties field of the resource.

Thank you, I took a look at the github page, but it’s still not clear to me how to store the value as a placeholder and use it elsewhere. Also, how are resource properties accessed?

To output the page counter on another page, call the snippet [[!HPCount]] and use syntax like this:

[[!HPCount]]

{%hp-14%} <!-- outputs the counter of the page with the ID = 14 -->

{%hp-14%} gets replaced with the actual number in the “HitsPage” plugin.


If you want to write custom code to output the page counter, use code like this:

<?php
use MODX\Revolution\modResource;
$resourceId = 14; // to output the counter of the page with the ID = 14
$page =  $modx->getObject(modResource::class, $resourceId);
$view = $page->getProperty('hitts', 'hitspage', 0); //(key, namespace, default_value)
return $view;

Using code with the syntax {%hp-14%} breaks my website and prevents the entire page from rendering.

The Hits extra is the one that I would like to use and seems like it does what I need by including the option to store the hits value in a placeholder, however, I am unable to download it through the package manager. Is this extra not compatible with MODX 3.0.1-pl?

When you look at the page here

https://extras.modx.com/package/hits?version=1.3.0-pl

it says

Supported Versions
2.0 - <2.4

That’s why it won’t show, when using the package manager in MODX 3.

The extra might still work in MODX 3 though. I haven’t tested it.
To test it, you can download the transport-package directly from the page I linked above.
Then drag-drop the transport-package into the grid in the package manager and install it.