InMotion Hosting Support Center

Enable cache, and disable hits
When you enable caching in Joomla, article hits do not increase when cached pages are served. This is unfortunately a limitation of the caching system in Joomla, there is not "toggle switch" to enable this feature.

Hits not always accurate, but sometimes needed
For those users needing this feature, it's usually recommended that they instead switch to a more accurate tracking system, like Google Analytics. This though is an alternative, and doesn't actually present a solution to the problem at hand. Even though not 100% accurate, sometimes article hits are necessary, such as the scenario when you need to order articles based upon their popularity.

We created a solution
The article that you're reading right now is within InMotion Hosting's Support Center, and is served using Joomla. We have enabled caching in Joomla, and ourselves needed the hits on articles to increase. In this tutorial, we will outline the steps we've taken to have Joomla increase article hits event when a cached page is served.

  1. Disable article hits

    I know it sounds funny, but the first step in this entire process is to disable article hits. The solution we are implementing will use ajax to increase article hits. We don't want Joomla to increase hits at the same time our ajax call is increasing hits, this would result in double hits.

    Edit the following file and add the code highlighted in red below:


    public function hit($pk = 0)
        return true;
        $input = JFactory::getApplication()->input;
        $hitcount = $input->getInt('hitcount', 1);
        if ($hitcount)
          $pk = (!empty($pk)) ? $pk : (int) $this->getState('');
          $table = JTable::getInstance('Content', 'JTable');
        return true;

    Core file change!
    This step edits a core file, which is not recommended. You can skip this step, however this will result in hits increasing by 2 (instead of 1) when cache is not used to server a page.

  2. Enclose your hit count within a span tag

    After using ajax to make a call to increase article hits, we will update the current hit counter on the page so that it is accurate. We will use javascript for this, and javascript needs to know where on the page the hits are showing. We label the article hit counts by wrapping it within a span tag.

    Create a language override and add the changes highlighted in red:

    Language Constant:COM_CONTENT_ARTICLE_HITS
    TextHits: <span id="article_hits">%s</span>
  3. Add the ajax calls to your articles

    Now it's time to add the ajax calls to our articles. It's this ajax call that makes a request to increase article hits, and will run regardless if the page is being served via cache or not. Edit the following file and add the code highlighted in red at the end of the file:


    if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && $this->item->paginationrelative) :
      echo $this->item->pagination;
      <?php endif; ?>
      <?php echo $this->item->event->afterDisplayContent; ?> </div>
    <script type='text/javascript'>
                    '<?php echo JURI::base(); ?>includes/increase_hits.php',
                    {option:'com_content',view:'article',id:'<?php echo $this->item->id; ?>'},

    Core file change!
    This step edits a core file, which is not recommended. You should instead add this code to an override.

  4. Create increase_hits.php

    Create the following file. This is the php script that our ajax call will interact with.


     *  Use this script to update article hits when caching is enabled
     *  Setup guide can be found here:
    if (  $_POST['option'] == "com_content"
          && $_POST['view'] == "article"
          && is_numeric($_POST['id']))
      // connect to the database
      $cg = new JConfig;
      $con = mysqli_connect($cg->host,$cg->user,$cg->password,$cg->db);
      if (mysqli_connect_errno())
      // increase hits
      $query = "  UPDATE  `" . $cg->dbprefix . "content`
                  SET     `hits` = `hits` + 1
                  WHERE   `id` = " . $_POST['id'] . "
                  LIMIT 1;
      // grab the new hit count
      $query = "  SELECT  `hits`
                  FROM    `" . $cg->dbprefix . "content`
                  WHERE   `id` = " . $_POST['id'] . "
                  LIMIT 1;
      $new_hits = mysqli_fetch_assoc(mysqli_query($con,$query));
      // close the connection to the database
      echo $new_hits['hits'];

And that's it! It's the same approach we've used for our own Joomla site. If you have any problems getting this to work, feel free to post a comment below. Alternatively, post a comment too if you have another method users can implement to increase hits when cache is being used.

A special thanks to Pankaj with for the encouragement to write this article.

Joomla Community - Google Hangout #3

Continued Education in Course Joomla 3
You are viewing Section 18: Updating article hits when using Joomla cache
Section 17: DocBlocks - Documentation headers in Joomla
Section 19: Jimport

Joomla Community Google+ Hangout #3

June 3rd, 2014

Thank you @RustyJoomla for letting me speak on the Joomla Community Google+ Hangout!

Click here to watch!

Support Center Login

Social Media Login

Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-06-13 5:06 am

Great, thank you for the tips. Would this work for Joomla 2.5 and how to have the same for K2 items ?

11,186 Points
2014-06-13 7:52 am
It has not been tested but in theory it should work just fine in Joomla 2.5. Unfortunately, we are not familiar with K2.
n/a Points
2015-02-02 9:58 am


Thank you very much for sharing this article.

I tried it in our Joomla 3 site and it works fine. But I have one small problem.

In case there's a pagebreak and the user navigates to other pages in the same article the hit count is increased.

So if there are 3 pages in a article, for every user visit to that article, count is increased by 3.

We have made the step 3 changes in the template overriden default.php and we are using Jooma shine's JSN Boot template.

Can you please suggest some idea to fix this? Thanks in advance.


Thank you.

n/a Points
2015-07-13 11:41 am

Tested this on Joomla 2.5.28 and it did not work. Any suggestions?

31,565 Points
2015-07-13 2:44 pm
As this is in the Joomla 3 category, there may have been some changes since your version. Are you getting any errors when performing this?
n/a Points
2015-07-14 6:54 pm

I know this has not yet been tested with 2.5, as mentioned by JeffMa, so I just wanted to verify his comment. No errors after implementing the hacks, but I read further on a Joomla Forum that the failure to update hits may very well be due to the delay in the caching time. So, I set the cache to 1 minute, as opposed to the default 15 minutes, and the page hits did update without any need to hack any files. So, my suggestion would be to wait for the site to cache and then check the page hits again. You may not need to do anything.

n/a Points
2015-09-02 8:43 am
WHERE   `id` = " . $_POST['id'] . "

SQL Injection - welcome!

Did you ever heard about sanitising inputs?

28,417 Points
2015-09-02 10:35 pm
Hello Dmitry,

Thanks for the input. Your point is well taken. I will request to have the article updated with that in mind. For anyone interested in the topic, check out this post.

If you have any further questions or comments, please let us know.

Arnel C.
n/a Points
2015-09-29 6:16 pm

So it is safe to do what article says, or not. I am new at this, I am trying to learn and I am confused a bit, from Dmitry's post :)

7,033 Points
2015-09-29 11:35 pm
Hello Elias,

You should be fine for the most part. If you are worried about security you may read the following thread about sanitizing your sql code.

Best Regards,
TJ Edens
n/a Points
2015-09-30 2:50 am

Thank you for your reply. I succesfully did your method and it works like a charm. However I have some questions if you don't mind.

1. I didn't use the first edit here (components/com_content/models/article.php) becouse I am afraid to edit core files. Can I use an override for this? 

2. You say that I can skip that step but this will result in hits increasing by 2 (instead of 1). That's true but only at the first time you read an article. The next time you read the same article, the counter increases normally by 1. That's what I see when I test it. Am I wrong with that?  My point is...Will I have problem becouse I skipped the first step?

3. I did the language overrides in both two languages that I use. Is this wrong?

Thank you



28,417 Points
2015-09-30 2:47 pm

1) I would back up all your files first, then go ahead with the overwrite. If anything happens after that, then you can always return to the backup point.

2) If you are finding that your numbers are correct, except for the first time, then it should be okay. There may be differences in Joomla installation versions, plugins, etc. that may affect how this is operating. The most important thing is that you are getting consistent results from your testing. If you continually have the correct number AFTER your run your test, then you should use what you have with no issue.

3)It shouldn't really matter the languages, if you made the correction in the core files.

If you have any further questions or comments, please let us know.

Arnel C.
n/a Points
2015-09-30 4:12 am

In addition to my previous post I would kindly like to add a strange (for me) thing.

I did your method succesfully in my dev site. But in my live site this doesn't work. Keep in mind that theese 2 sites are online.The dev site is in a subfolder and it's the clone of my live one. In the dev site I do my...homework, testing this and that.

The strange part is that in dev site the method works ok. In the live it's not! What do you think is cousing this? I miss somthing for sure. I checked every step and I everything is good. Something else is cousing this. What's your opinion? Can you point me at the right direction (what to check, etc)?

Thank you once again

28,417 Points
2015-09-30 2:50 pm
Hello Elias,

I'm not sure why your code wouldn't work in live versus the dev site. I would think that there has to be some difference in the code that you're applying on the dev, that is not the same in the live. It may have to do with server references or it might have to do with support services being different from one server to another. It's hard to say. I would recommend checking the error logs to see if anything has been captured that could account for the problem you're seeing.

I hope this helps to answer your question, please let us know if you require any further assistance.

Arnel C.
n/a Points
2015-09-30 2:55 pm

I think I found the...guilty. I applied the code to templates/mytemplate/html/com_content/article/default.php

This file was allready there at my template. So I add the code there. It works now.Hope I didn't make mistake.

Once again Thank you.

Best regards


n/a Points
2015-09-30 9:38 pm

It's not working for me on Joomla 3.4. Is there some parts you're supposed to enter individual site details, or are you supposed to put the code as is?

28,417 Points
2015-10-01 10:17 am
Hello Sam,

sorry for the problems with Joomla 3.4. Yes, you are supposed to insert server relevant information where the code indicates a connection or server reference. For example, the database connection information would be required. Other than that, the code should be used as it is provided above.

I hope this helps to answer your question, please let us know if you require any further assistance.

Arnel C.

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

17 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!