Joomla’s issues caused by caching are many, but one of these issues is extremely annoying and we are surprised how the Joomla development team hasn’t come up with a solid solution to it yet. In case you’re still wondering what the issue is, then it is no other than the conflict between hit counting and caching. Here’s the problem in details:
- You enable caching on your website (any type of caching – even Conservative Caching).
-
Joomla caches the content item or the whole page (depending on the type of caching you apply, System Caching will cache the whole page, and Conservative Caching will just cache the content item).
-
When a visitor visits a page, then Joomla checks first if the there is a cached version of the page or the requested content item, and if there is, then Joomla will serve the cached version, if not, then Joomla processes the request, serves the page, and then creates the cached version of that page/content item. If the page/content item is already cached (which is the case in about 99% of the visits), then Joomla will not count the visit as a hit as the script that runs to increment the number of visits does not run on a cached version of the content item or the page.
If you’re a technical person, then you will know that it is really hard to solve the above problem without any core modification since the problem lies in the core (if you’re not a technical problem, then you should just take our word for it). However, at itoctopus, we have devised a method that will address the caching problem without core modifications. Here’s how:
- Open the file default.php under the templates/[your-joomla-template]/html/com_content/article folder.
-
At the end of the file, add the following two lines:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script>$.get( "<?php echo(JURI::root()); ?>updateHits.php", { articleId: "<?php echo($this->item->id); ?>"} );</script>
(Note that the first line is not needed if you already have jQuery loaded.)
-
Upload this file (after extracting it) to the root folder of your Joomla website.
-
Clear your Joomla cache.
-
That’s it! That’s really it! Now your website can track the hits of your cached items.
But, how can the above method solve the problem?
The above method solves the problem because the cached content item/page will always contain the JavaScript code that will increment the number of hits, and so that code will always run even if the content item/page is cached.
But, does the above method provide 100% accuracy?
No – and we don’t think anyone out there needs to track the number of hits with a 100% accuracy since many hits come from bots (even on low traffic websites). In any case, the reason why the above method doesn’t provide 100% accuracy is because the number of hits will be incremented twice on non-cached items: 1) by Joomla’s core, and 2) by our code above. Of course, the margin of error in detecting hits is negligible but there’ll still be a margin of error.
If you want 100% accuracy when tracking hits through your Joomla website, then you should consider disabling hit tracking (note that doing that will mean a change to your Joomla core).
But, why should anyone care about the number of hits in Joomla?
Well, if you have a module that lists your “most read articles” on your website and your hit tracking is not working properly because of caching, then most likely the information displayed in the module will be wrong (your important articles will not be displayed, and your non-important articles will be displayed – at the very least the ordering of your most important articles will be wrong).
Does the above method work on K2?
No – but K2’s hit tracking doesn’t have any conflicts with caching. So you shouldn’t need it for K2.
Now, if you’re running into the same dilemma as the majority of Joomla administrators out there (e.g. hit tracking is not working when you have any kind of caching turned on), then try implementing the above – it should solve your problem and your Joomla website should track your articles’ hits reliably. If you think the above is somehow in the advanced realm (or maybe in the twilight zone), then fear not… Just contact us and we’ll do it for you for a very, very affordable fee. Note that a sufficient mix of love, friendliness, and professionalism is always added for free!