Collect site statistics

by Gisle Hannemyr

This recipe explains how to install and enable the core statistics module, and how to inspect the page view-counter and a block that displays popular content.

Table of contents

Introduction

If you want to have a page views-counter on your website to get a rough idea about the number of page views your contents get, or display lists of popular content, you can use the core Statistics module.

The module provides two ways to display the statistics data:

  1. A page views-counter that shows how many times a particular page has been viewed.
  2. A "Popular content"-block that may be placed in a region.

technicalEuropean privacy regulation (GDPR) requires consent for the collection of information that links personal information such as the visitor's IP-address to page views. To protect the privacy of the visitors to your site, the Statistics module does not collect this and therefore, it cannot display demographic data.

Installing and enabling statistics

The Statistics module is in core, but unistalled by default. To install, navigate to Manage » Extend and locate "Statistics" in the list of modules available. Place a checkmark in the checkbox to the left of the name. Then scroll down to the bottom of the page and click "Install".

To enable collection of statistics, navigate to Manage » Configuration » System » Statistics, and check the "Count content views" checkbox.

Display page views-counter

The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, first enable statistics as described above.

To control visibility, navigate to Manage » People » Permissions, and locate the permissions ("View content hits") in the "Statistics" section of the page. Give it to the roles that shall see the counter (usually only "Administrator").

noteThe counter will not be displayed if the count is zero. Make sure you've have at least one view of a page before you look for it.

You may have to clear the cache to see the counter. Navigate to Manage » Configuration » Development » Performance and press "Clear all caches".

Display popular content

The module provides a "Popular content"-block that may displays these three statistics lists:

To show the block, first enable statistics as described above, then:

  1. Navigate to Structure » Block Layout.
  2. Decide in what region to place the block and click ("Place block") for that region.
  3. Locate the block "Popular content" and click "Place block"
  4. Configure how many results to show for each statistics list.
  5. Configure the visibility of the block.

Notes

To save resources, the visible page views-counter and "Popular content" block is only updated when the cache is cleared.


Last update: 2021-02-21.