There are lots of analytics packages available. However many of these solutions are overkill; in forcing you to view data in highly over-complicated dashboards or collecting more than the necessary data about your website users. If you are only interested in getting a rough approximation of how many times a webpage has been viewed, HeadCount is perfect for this task.

HeadCount draws inspiration from the days when people were building primitive websites with tools like GeoCities. Many websites featured a "this website has been visited X number of times" tagline or graphic. Although HeadCount could be used to create identical visitor counters, its main objective is to give website authors a very quick and brief overview of how often webpages have been accessed.

It would not be difficult to check the HeadCount logs monthly or quarterly, and pull this data into a spreadsheet. Over time, you could apply some basic statistical analysis to determine which pages of a website are most popular. If you discover some pages are not gaining as many page views as you had hoped, then you could focus efforts on promoting them more.

Example

Here is a working example of HeadCount, with the total number of views echoed into this webpage. If you refresh your browser, you should see the count increment:
 
This webpage has been viewed 5844 times.
 

Setup

To use HeadCount, simply drag and drop the stack into your webpage and publish the page. The only dependency is that the webpage has a .php file extension. HeadCount will attempt to change the file extension for you. Just remember to delete any webpages that are previously published with a .html extension, so you don't have duplicates on the hosting server. There is no harm in having webpages with .php page extensions - plenty of stacks change page extensions to .php already. The only setting you might want to change is the Counter Filename. This is the file name and extension that your number of hits will be written into.

If testing this stack in RapidWeaver preview, the hit counter will reset each time you restart RapidWeaver.

Viewing the hits count

Once your website is published, you can easily access the number of hits by typing page-hits.txt onto the webpage address. So if for example your webpage address was:

https://example.com/products/latest/

You could access your number of hits at this address:

https://example.com/products/latest/page-hits.txt

In our working example above, you can view the the text file storing the hit count here:
https://stacks4stacks.com/headcount/page-hits.txt

It is easy to bookmark these links in your web browser or share these links with a client (so they can see how many page views a webpage has received). If you prefer for the public not to see the hit count, you can change the filename to something more complicated and harder to guess.

Limiting who is recorded

HeadCount purposefully uses a simple system of page views. It doesn't care if the visitor is a repeat visitor or is the website owner. Each time the page is loaded, it records it. If you want to exclude yourself or others from being recorded, the easiest option is to place HeadCount inside a cookie-tracking stack, like CookieManager or MiniCookie. That way, you can set HeadCount to only function, in the presence or absence of a tracking cookie.

Displaying the latest count in the webpage

If you want to display the latest hit count in the webpage, you can use this PHP code snippet:

<?php echo file_get_contents( "page-hits.txt" ); ?>

You could place it within additional content or HTML code like this:

<h3>This webpage has been viewed <?php echo file_get_contents( "page-hits.txt" ); ?> times.</h3>

When the webpage loads, the PHP code is replaced with the actual hit count, from the text file. If you have decided to rename the file used for storing the hit count, update the above code accordingly (replacing page-hits.txt with the new filename).

Relative paths are supported too. So if you wish to create your own dashboard that perhaps displays the number of hits from multiple webpages, it is feasible to do so like this:

<strong>Homepage: </strong> <?php echo file_get_contents( "../page-hits.txt" ); ?> views.<br>
<strong>About Us: </strong> <?php echo file_get_contents( "../about-us/page-hits.txt" ); ?> views.<br>
<strong>Blog: </strong> <?php echo file_get_contents( "../blog/page-hits.txt" ); ?> views.<br>
<strong>Products: </strong> <?php echo file_get_contents( "../products/page-hits.txt" ); ?> views.<br>
<strong>Contact Us: </strong> <?php echo file_get_contents( "../contact-us/page-hits.txt" ); ?> views.<br>

Reseting the counter

The count is stored in a plain text file on the hosting server. The default filename is called page-hits.txt. You can login to you hosting account via FTP or your file manager to edit or delete this file. The plain text file always resides in the same directory that the webpage is published to, making it easy to find.
 

Contribute

If you find this stack element useful in your personal or commercial web projects; please consider making a small contribution towards ongoing support and updates. There are many different ways you can contribute to the Stacks4Stacks project, and benefits for doing so.