Steps to reproduce
- Enable Statistics module.
- Make sure that under /admin/config/system/statistics "Count content views" is enabled.
- Create a basic node view, add a "Content statistics: Total views" field to the view. The field display is always 0 no matter what.
See screenshot for reference.

Can also replicate on 8.5.x branch.
| Comment | File | Size | Author |
|---|---|---|---|
| #71 | drupal_2962763_test.patch | 1.9 KB | 2dareis2do |
| #59 | drupal_issues_2962763_test2.patch | 1.84 KB | 2dareis2do |
| #58 | drupal_issues_2962763.patch | 1.91 KB | 2dareis2do |
| #57 | drupal_issues_2962763_test.patch | 1.88 KB | 2dareis2do |
| #50 | statistics_counter.patch | 1.36 KB | andrew answer |
Issue fork statistics-2962763
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
andres.torres commentedComment #3
lokapujyaThere is a step missing. The content must be viewed in order to increase the count. I've tested that after viewing the full content that the number increases.
Comment #4
andres.torres commentedHi lokapujya,
Actually not even visiting the node pages from different browsers (different ip's, as logged in/out user) the counter increments. Do you think there might be a missing step in order to enable the full functionality?
Comment #5
andres.torres commentedComment #6
lokapujyaDid you try clearing cache after viewing the node? I think the statistics are cached for one hour; I saw something about that. Is the count getting updated in the database? Also, try viewing the node as a non-admin one time; That's what I did anyway.
Are you viewing the rendered view or from the admin screen?
Comment #7
cilefen commentedIt is easy to reproduce. The problem is that a cache is not invalidated. Check the caching settings on the view. If it is "tag-based", the displayed value will not change even though the stored value is incrementing properly.
Comment #8
timmillwoodComment #9
andres.torres commentedhey guys! thanks for the help! i believe i found the issue, not sure if its really a Panelizer bug since Statistics counter stops (or never works) if the Full Content view-mode is panelized. No errors found in the log.
Comment #10
cilefen commentedThere is a missing cache tag somewhere because this counter does not increment when a view is cached as tag-based.
Comment #11
lokapujyaSo, when the content is viewed, the statistics cache needs to invalidate? I guess I don't understand cache tags enough right now to fix this.
I'll try to write the test case. This could be the wrong place to put the test.
Comment #12
lokapujyaThis patch is just to see if viewing the node in this test actually affects the existing test.
This test passed. So, this is just making a point that the code eliminated in this patch is not actually working as expected.
Comment #14
msankhala commentedComment #15
lokapujyaComment #16
andres.torres commentedDisabling cache on the View and/or applying the patch seems to work, but sill having the issue when a node is panelized or depending on blocks. Is there a way to increment the counter under this situations?
Comment #17
lokapujyaThere is no patch yet. The patch in this issue is just the test case. So, setting to Active.
Comment #21
ferrete commentedIn my case this happen because I was using panelize module.
Comment #23
lubwn commentedThis is still an issue. I can confirm clearing cache works, but I do not want to clear cache too often so the site would became slower. Is there anything we can do about it? I turned off caching for views block but it does nothing.
Comment #24
drupal-ramesh commentedlubwn i have faced the same issue.Even if you turn off views block cache it won't show. After disabling entire page cache it shows correctly.i am still checking another possible ways like without disabling entire page cache to show node views count correctly.
Comment #26
sarguna raj m commented@drupal-ramesh, have you got any solutions for it. I'm also facing same issue, node view count is not getting updated on the page load. we need to clear the cache for it.
Comment #27
drupal-ramesh commentedHi sarguna raj M
Need to add a new cache tag that has to invalid when the view count changed. Each Views having node_list cache tag that is only invalid when node value changed(not on view count change).
Comment #28
lokapujyaComment #29
lokapujyaComment #30
lokapujyaI can't figure it out. But somewhere (NodeViewController?) the view needs to be invalidated. Still need a new cache tag for statistics; Don't know where that goes. Posting for discussion.
Comment #31
lokapujyaThe test in #11 needs a reroll.
Comment #32
lokapujyaComment #33
drupal-ramesh commentedHi
I added new patch to invalidate the node_list cache tag. I invalidated that node_list cache tag when node_counter table updated.
Comment #34
kapilv commentedComment #35
Madhu kumar commentedHi @KapilV , patch #34 applied cleanly but not working as expected counter not increasing, sharing screenshot for reference.
Comment #36
drupal-ramesh commentedHi
In each view i am getting node:node_id(node:1) cache tag. node_list cache tag added for all views. So invalidate node_list cache tag it will affect all views . so instead of node_list cache tag i invalidated node:node_id cache tag. So without clear cache node counts shows correctly in both node and view page.
Comment #38
vsujeetkumar commentedFixing fail test, Have a look and advise.
Comment #39
wim leers#38 is just a hard work-around with far-reaching consequences.
The real bug lies in
\Drupal\statistics\Plugin\views\field\StatisticsNumeric— it does not provide cacheability metadata whereas it should. You need something like the attached patch. It can use further refinement, especially configurability.Related:
\Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait, introduced by #2450897: Cache Field views row output.Comment #41
phjouFor me the counter was stuck at zero, but it was ublock origin blocking that feature.
Comment #42
gauravvvv commentedI tried testing #39, but I found another issue.
I have 2 basic pages in my view. both have an equal no. of views every time. if I open only one page. No. of views increases for both the pages.
Attaching screen recording for same.
Comment #43
chetanbharambe commentedHi Community Members,
I have tested this issue and working as expected for me.
Testing Steps:
# Goto: Appearance -> Apply Seven Theme
# Enable Statistics module.
# Make sure that under /admin/config/system/statistics "Count content views" is enabled.
# Create a basic node view, add a "Content statistics: Total views" field to the view. The field display is always 0 no matter what
Expected Results:
# User should see the nodes value correctly whenever the user opens the respective node like 2 times, 3 times
Please refer attached screenshots and Video for the same.
Looks good to me.
Can be a move to +1 Needs Review.
Comment #44
kostyashupenkoComment #46
vikashsoni commentedApplied patch #39 successfully applied
After patch we can able to see count of nodes after visit node
For ref sharing screenshot ....
Thanks for the patch
Comment #48
lubwn commentedAfter 2 more years I am again stumbling upon this problem. Still not solved, making statistics module obsolete.
Patch #39 does nothing on my installation.
I found a workaround:
1. Create field "Number of views" being Number (integer)
2. Write your own module to increment number on each page hit
3. Add your newly added field to views or wherever you wish
I am supplying the code I have used:
This works fine for me even with caching enabled. It is sad that Drupal got so absurdly complicated that such a minor issue takes years to solve and is not solved at all...
Comment #49
cilefen commented@lubwn Constructive comments and manual testing results as you have made here are welcome contributions. But complaining accomplishes nothing. Participate if you wish to have influence over the product.
Comment #50
andrew answer commentedHi everyone,
I found an easy way to fix this problem. The statistics module already has a 'display_max_age' setting for the counter, so I added it to the settings form using a patch. After that I set it to 0 and saved it. Clearing the entire cache is not optimal, but it is very easy to optimize for Drupal-ninja (as I think :)). I don't have time for this right now, sorry.
Best regards, Andrew
Comment #52
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
Tested on Drupal 10.1.x following the same steps in the IS but was not able to replicate.
Are there additional steps?
Comment #53
quietone commentedI also tested this on Drupal 10.1.x and I was able to reproduce the problem. That is, I had to clear cache for the counts to update.
Statistics is approved for removal. See #3266457: [Policy] Deprecate Statistics module in D10 and move to contrib in D11
This is now Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.
It will be moved to a contributed Statistics project once the project is created and the Drupal 11 branch is open.
Comment #55
2dareis2do commented@quietone I am experiencing the same.
View cache and page cache is set to not-set for time-based caching.
Tried changing to tag based and seems to be the same.
display_max_age: 0
count_content_views: 1
Admin view does seem to show correct value.
@cilefen Your explanation sounds correct.
@Andrew Answer I tried the same locally i.e. setting max-age to 0 but the result is the same.
@drupal_ramesh Thank you!
From what I can see this invalidates the node tag cache using the node id that the stat is generated right after the storage is updated.
@Wim Leers please can you explain what your patch does and how to use it? Can you also elaborate what the far reaching consequences are of ramesh's solution here?
Comment #56
2dareis2do commentedChanging to 10.2 as this appears to be a bug and statistics is scheduled to be moved to contrib in 11.x
Comment #57
2dareis2do commentedComment #58
2dareis2do commentedComment #59
2dareis2do commentedComment #60
2dareis2do commentedOk I have rerolled previous patch. This contains both elements of #38 (working) and #39 (not working)
drupal_issues_2962763_test2.patch works for me locally.
I have not included the test as my assumption here is we probably need a new test for this, rather than changing the previous one. Not sure.
Counts are updating for now. This is without actually changing page cache (under performance) and the default views max-age cache on the view. Changing the cache to cache tags on the view seems to make little difference.
Browser still needs reloading to show the view count so I guess edge cache is still in place here, again not really sure.
Comment #62
fgmComment #63
fgmComment #65
fgmConverted #59 latest patch by @2dareis2do to MR!7 https://git.drupalcode.org/project/statistics/-/commit/c00a33433c3a243d6...
Comment #66
fgm@2dareis2do 's answer on Slack :
Comment #67
2dareis2do commentedLooks like there are quite a few change in statistics module. with 10.3 release.
https://github.com/drupal/drupal/tree/10.3.1/core/modules/statistics
Patch does not apply and statistics are not updating.
Patch probably needs to be re rolled.
Comment #68
2dareis2do commentedComment #69
2dareis2do commentedUpdated for 10.3
Comment #70
2dareis2do commentedComment #71
2dareis2do commentedComment #72
fgmNote that this issue is about the contrib statistics project, not statistics in core, but the link you mention https://github.com/drupal/drupal/tree/10.3.1/core/modules/statistics is about core.
Comment #73
2dareis2do commentedRight sorry. my bad.
Am i right in thinking this started as a core issue?
To clarify, patch is for core module not contrib (as per the previous one).
Comment #74
2dareis2do commentedI guess the issue will never be fixed in core if the issue queue is now in contrib.
Comment #75
fgmYou're 100% right. Even more so now that 11.0.0 has been released and no longer contains statistics.
Updating issue to current Statistics release.
Comment #76
subir_ghoshThe module worked fine till I started using it as a contrib module (after it was moved out of core).
Now, the counter does not move. No matter what.
If the counter doesn't move, then this module is of no use really.
Comment #77
fgm@newswatch you will need to provide more details about your configuration, possibly a a URL, to receive help. Without that we cannot know what does not work on your site.
First, can you reproduce the problem on a new site with just D11 core and statistics ?
Comment #78
anybody@fgm: MR LGTM, what's the further plan here?
Comment #79
fgm@anybody : awaiting a return from @newswatch since they appear to say it doesn't work for them, but I do not see how that can happen so we need more info.
Comment #80
anybody@fgm thanks! So if I get you right, you assume the MR maybe isn't needed?
Anyway I'd like to propose that lazy builders *might* be a nice solution for this case!
https://www.hashbangcode.com/article/drupal-9-using-lazy-builders
Comment #81
subir_ghoshI installed the module on a site where it had not been installed earlier.
I waited for 3 days to check. The ticker is not rolling. All stats remain 0 for all nodes.
Comment #82
stevewilson commentedWith version 1.0.0 of Statistics module, on Drupal 10.3.10 and PHP 8.1.30, I've run the following tests - first without and then with the changes in MR !7 applied:
Clear caches;
Load a page with number of views showing;
Note number of views;
Load a different page;
Reload the first page;
Note number of views.
WITHOUT the MR !7 changes in place, on reloading the first page, the displayed number of views is unchanged from when the page was first loaded. Displayed number of views only increases after clearing caches.
WITH the MR !7 changes in place, on reloading the first page, the displayed number of views is incremented by 1 from when the page was first loaded. Further reloads result in further increments of the displayed number of views - no need to clear caches.
In other words, for me, the changes in MR !7 fix the failure to update issue.
Comment #83
subir_ghoshSince there is no documentation on changes required for shifting from core module of Drupal 10.3.0 to Statistics 1.0, I need a confirmation about thsi, please.
Drupal change record says that the following should be removed
Now, I have done that. But what should it be replaced with (if at all)?
Comment #84
stevewilson commentedFrom memory, all I did to switch from the core Statistics module to the contributed Statistics module was uninstall core Statistics, install contrib Statistics and comment out the highlighted RewriteCond line in .htaccess. I certainly didn't replace the RewriteCond line with anything else.
And for me, MR !7 resolved the count not updating issue.
I'd be keen to see MR !7 committed and a fresh release made.
Comment #87
2dareis2do commentedAs statistics is still in core (10.4) I am unhappy that this issue has been moved to contrib.
I am also unhappy at the Drupal policy of targeting the next major release. which I have not upgraded to yet because they have dropped support for statistics.
Comment #88
2dareis2do commentedActually after upgrading from 10.2 to 10.4 latest patch for 10.3 applies without issue.
Comment #89
stevewilson commentedHaving just updated to version 1.0.1, I am reminded that I need a fix for this issue.
Having manually applied the MR !7 fix, to the 1.0.1 release, the count is once again being incremented and correctly displayed whenever a page is viewed.
It is over a year since a user reported that this wasn't working for them, but no further information has been provided. Can we not agree that the fix at MR !7 resolves this issue and get it committed?
Comment #90
vitaliyb98 commentedTested with changes from MR #7
This fixed those issue with caching, LGTM
Comment #91
fgm