Statistics
Logs and displays anonymous content viewing statistics for a Drupal site, counting how many times each node is viewed by all users including anonymous visitors.
statistics
Install
composer require 'drupal/statistics:^1.0'
Overview
The Statistics module provides anonymous analytics by counting entity views for all users, including anonymous ones. This is the contributed version of the module that was previously part of Drupal core, now maintained separately for Drupal 10.3+ and 11.
Currently, the module counts views for nodes only in full-page mode, maintaining a running grand total and daily subtotal without historical data. The module uses a dedicated front controller (statistics.php) for performance-optimized view counting via AJAX, minimizing the impact on page load times.
The module provides a "Popular content" block that displays the most viewed content today, all-time, and recently viewed content. It also integrates with Drupal's search module to factor view counts into search ranking, and with Views to expose statistics data for custom displays.
Features
- Counts content (node) views for all users including anonymous visitors
- Maintains total view count and daily view count per node
- Tracks the timestamp of the most recent view for each node
- Provides a "Popular content" block showing most viewed content by day, all time, or recently viewed
- Displays view count on node pages for users with appropriate permission
- Integrates with core Search module to influence search ranking based on popularity
- Integrates with Views module for creating custom statistics-based displays
- Supports migration from Drupal 6 and Drupal 7 statistics data
- Uses a lightweight front controller for performance-optimized AJAX-based view counting
- Automatically resets daily view counts via cron
Use Cases
Display Most Popular Articles on Homepage
Use the Popular content block to showcase your site's most viewed articles. After enabling content view counting, place the block in a sidebar or featured region and configure it to show the top 5-10 all-time most viewed content items.
Create a Trending Content View
Use Views to create a custom 'Trending' page or block showing content sorted by daily view count. Add a relationship to node_counter, sort by daycount descending, and limit results to show what's hot right now.
Boost Popular Content in Search Results
With content view counting enabled, the Statistics module automatically influences search rankings so that frequently viewed content appears higher in search results, helping users find popular content more easily.
Track Content Performance
Use Views to create an admin report showing all content with their total views, daily views, and last viewed date. This helps content editors understand which content resonates with visitors.
Migrate Statistics from Legacy Drupal Site
When migrating from Drupal 6 or 7, use the provided migrations (statistics_node_counter, statistics_settings) to preserve your existing view count data in the new site.
Tips
- The Popular content block is only visible to users with 'access content' permission and only appears when content view counting is enabled.
- For high-traffic sites, consider caching strategies as the statistics display respects the display_max_age configuration setting (default 1 hour).
- The JavaScript tracking uses jQuery AJAX and runs asynchronously to avoid impacting page load performance.
- Statistics are only tracked for nodes viewed in 'full' view mode as full pages, not for nodes displayed in views or other contexts.
- The module cleanly replaces the deprecated core Statistics module - simply install via Composer and Drupal will automatically use the contrib version.
Technical Details
Admin Pages 1
/admin/config/system/statistics
Configure statistics settings for tracking how many times content is viewed on the site.
Permissions 2
Hooks 7
hook_node_view
Attaches the statistics tracking JavaScript library to nodes when viewed in full page mode.
hook_node_links_alter
Adds view count display to node links for users with 'view post access counter' permission.
hook_cron
Resets daily view counts when 24 hours have passed since the last reset. Also calculates and stores the counter scale for search ranking.
hook_node_predelete
Cleans up statistics data when a node is deleted.
hook_ranking
Adds view count to search ranking factors when content view counting is enabled.
hook_block_alter
Removes the 'statistics_popular_block' from available blocks when content view counting is disabled.
hook_views_data
Exposes the node_counter table and its fields to Views for building custom displays.
Troubleshooting 5
Ensure 'Count content views' is enabled at /admin/config/system/statistics. Verify your web server is properly configured to route requests to statistics.php (check .htaccess for Apache or add equivalent Nginx rules).
The block is only available when 'Count content views' is enabled. Enable this setting and clear caches.
Grant the 'View content hits' permission to the appropriate roles at /admin/people/permissions.
Add a rewrite rule to your Nginx configuration to properly route POST requests to the statistics.php front controller located in the module directory.
Ensure cron is running regularly. The module's hook_cron() implementation resets daily counts every 24 hours.
Security Notes 3
- The statistics.php front controller validates input (node ID must be a positive integer) and catches exceptions to prevent information disclosure.
- View counts are only displayed to users with the 'view post access counter' permission.
- Administrative access to statistics settings requires the 'administer statistics' permission.