Pageviews: Show Off Your Views Counts in WordPress

Pageviews is a simple and lightweight views counter for your WordPress posts and pages. It’s based on JavaScript, talks to a third-party microservice, and is perfect for high-traffic sites and ones with large amounts of content.

The Pageviews Plugin on WordPress.org
The Pageviews Plugin on WordPress.org

The Story

We’re a hosting company and most of our customers join us with existing sites with large numbers of plugins and integrations, hoping to increase their overall speed. One of the most common performance problems we encountered while working with news sites and blogs, is with view counter plugins, such as WP-PostViews, Post Views Counter and many more.

Most of these plugins operate using AJAX requests to the site’s admin-ajax.php to increment and retrieve counts. This technique by-passes page caching plugins, reverse proxies, etc., and while it’s generally not a bad solution, there are three main issues with the whole approach:

  1. The site’s admin-ajax.php loads the full WordPress environment, themes and plugins. All of this is too much, just to increment or get a count.
  2. Caching is king when it comes to performance. Firing a non-cached request to admin-ajax.php for every page view is checkmating the king.
  3. Most plugins increment counts using the standard WordPress metadata APIs, which are not atomic, and that means they are not accurate in high-concurrency environments.

Of course accuracy is not as important in most cases, such plugins are often used to attract visitors’ attention to certain entries, sometimes to rank posts in “popular” lists, so it’s not a big deal if the counter is off by 5%.

The performance issues, however, are much more severe, and can cause serious load and service disruption on high-traffic websites. But our customers loved their counters, so we had no other option, but to create our own solution.

Counters as a Service

It was obvious that processing the counters had to be done away from the original WordPress site. It allows us to be independent from any other active plugins, that may or may not perform an update/license check during admin_init. It allows us to be completely JavaScript-based and compatible with any caching and reverse proxy setups.

Most importantly, it allows us to perform requests much faster, since we don’t have to load the WordPress environment at all, not even a SHORTINIT. At the time of writing, the service part is based on a Python application running on uWSGI and Nginx with a PostgreSQL cluster.

The Pageviews Plugin

The WordPress plugin is really simple and lightweight. It simply connects the WordPress site to our processing service, places some special markup on the page where counts will appear on a page load.

The Pageviews Counter in Action
The Pageviews Counter in Action

By default, the plugin adds a views icon and count to the end of each post, but it’s relatively easy to customize as well — special functions allow developers to place counts wherever they want in their template files:

// functions.php:
add_action( 'after_setup_theme', function() {
    add_theme_support( 'pageviews' ); // disable default behavior
});

// single.php inside the loop:
Views: <?php do_action( 'pageviews' ); ?>

For a more detailed customization guide visit the documentation on GitHub.

Extensions

Switching from one counter plugin to another is painful, because you lose all those precious counts. This is why we’re working on importers from most popular counter plugins, as well as analytics services such as Google Analytics, Jetpack, and others.

In addition to importers, we’re also working on a popular posts widget, live counter updates and some other pretty cool things. If you’d like to import your counts from another plugin or service into Pageviews, or help test out our other plugin extensions, please send us an e-mail at support@pressjitsu.com.

Download

The plugin is licensed under the GPL and is available for free at the WordPress.org plugins directory. You may install it directly from the Plugins section in your WordPress dashboard, by searching for “pageviews”. You may also grab the latest development version from GitHub.

Get Pageviews

If you have any questions or suggestions, feel free to open a forum thread on WordPress.org, or post an issue or pull request on GitHub.