-
AuthorPosts
-
Pankaj Sharma Moderator
Pankaj Sharma
- Join date:
- February 2015
- Posts:
- 24589
- Downloads:
- 144
- Uploads:
- 202
- Thanks:
- 127
- Thanked:
- 4196 times in 4019 posts
May 30, 2014 at 6:13 pm #198328Updating article hits when using Joomla cache
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. .Here is the Solution
Step 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:
Open <blockquote>components/com_content/models/article.php</blockquote>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('article.id');$table = JTable::getInstance('Content', 'JTable');
$table->load($pk);
$table->hit($pk);
}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.Step2. 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
Text Hits: <span id="article_hits">%s</span>
Step.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:
open the file
components/com_content/views/article/tmpl/default.php<?php
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'>
jQuery.post(
'<?php echo JURI::base(); ?>includes/increase_hits.php',
{option:'com_content',view:'article',id:'<?php echo $this->item->id; ?>'},
function(data,status){jQuery('#article_hits').html(data);},'text'
);
</script>Core file change!
This step edits a core file, which is not recommended. You should instead add this code to an override.Final step : Create increase_hits.php
Create a php file in
includes/increase_hits.php<?php
/**
* Use this script to update article hits when caching is enabledif ( $_POST['option'] == "com_content"
&& $_POST['view'] == "article"
&& is_numeric($_POST['id']))
{
// connect to the database
include_once("../configuration.php");
$cg = new JConfig;
$con = mysqli_connect($cg->host,$cg->user,$cg->password,$cg->db);
if (mysqli_connect_errno())
die('n/a');// increase hits
$query = " UPDATE `" . $cg->dbprefix . "content`
SET `hits` = `hits` + 1
WHERE `id` = " . $_POST['id'] . "
LIMIT 1;
";
mysqli_query($con,$query);// 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
mysqli_close($con);echo $new_hits['hits'];
}?>
If you are using a template that is using override of the the article layout
add the ajax script in
template/your template/html/com_content/article/default.php fileHope it helps you
Original link
-
AuthorPosts
This topic contains 1 reply, has 1 voice, and was last updated by Pankaj Sharma 10 years, 5 months ago.
We moved to new unified forum. Please post all new support queries in our New Forum