Node hits getting cached for anonymous user We are using Drupal 8.8.5. For every page we are trying to provide a counter which increments whenever user hits that node. These are nodes are available to anonymous users. So whenever an anonymous user hits the node, the view counter on that node should be updated to show the no of total hits . So lets say Tom and Hary are anonymous users. Tom hits node1. On top of Node1 it says &quot;View 1&quot; times. Now when Hary hits Node1 it should sat &quot;View 2&quot; times. Again if Tom hits node1 it should day &quot;View 3&quot; times.
I checked How can I show node Hits (statistics)? and made the necessary code changes in basic.theme and node.html.twig. Still, it shows Viewed O times.
This is the code I have in the basic.theme file.
function basic_preprocess_node(&amp;$variables) {
  $variables['node_view_count'] = 0;
  if (!empty($variables['node'])) {
    // Get the number of times the current node has been viewed.
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = \Drupal::service('statistics.storage.node')
      -&gt;fetchView($variables['node']-&gt;id());
    if ($statistics instanceof StatisticsViewsResult) {
      $variables['node_view_count'] = $statistics-&gt;getTotalCount();
    }
  }
}

This is the code I added in the node.html.twig file.
Viewed {{ node_view_count }} times

I believe there might be a caching issue once it starts working. For each hit, the values should be updated.
On a side note, when I am using the Visitors module, the report is showing an updated page count even for anonymous users. I had to install the Advanced Page Expiration module, though.
How do I increment the node hit for anonymous user visits in each page and show it updated?
Ok i have resolved this using a database query and a custom block. Pre-req: Installing visitors module.
Then having this query in the custom clock
public function build() {
  $node = \Drupal::request()-&gt;attributes-&gt;get('node');
  $uidValue = $node-&gt;getOwnerId();
  $urlValue = $node-&gt;toUrl()-&gt;toString();
  $database = \Drupal::database();
  **$query = $database-&gt;query(&quot;SELECT COUNT(visitors_path) AS visitors_path FROM 
(visitors) WHERE visitors_path='$urlValue' &quot;);**
  $result = $query-&gt;fetchAll();
  $nodecounthit = $result{0}-&gt;visitors_path;
  echo &quot;Viewed &quot;.$nodecounthit.&quot; times&quot;;
//$nodeCount=MyBlock::test();
      }

this function ensured results were not cached.
 public function getCacheMaxAge() 
  {
    return 0;
  }