Why does WP Most Popular use Javascript to track views?

When we first built WP Most Popular, we tracked views on the backend using PHP. This actually works quite well until you throw caching plugins in to the mix.

You see, simply put, caching plugins serve are designed to serve a static version of your website before any of the backend PHP functionality kicks it, so as to minimize the load that your server needs to process. It’s really effective and helps scale websites when traffic numbers peak.

On the flip side, that means when a static page is served to a visitor, as mentioned, it doesn’t pass through the backend PHP functionality and we are not able to track a view for the current page being served.

Most websites or web hosts implemented some sort of caching these days.

The solution to get around this is to use Javascript. After the static page loads, we initiate an AJAX call to the backend PHP server to log the view. Thus solving the problem of views not getting tracked on each page load.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.