0

Joomla core articles hits counter only works when the full article is shown (fulltext).

But if only the introtext is displayed, like in a Category Blog view or in a Featured Articles Blog view, the hit counter doesn't work.

This is a very limited behaviour, especially since many times we don't even use the "read more" button and the whole article is shown in the "introtext".

So I would like to count how many times an article has been shown (in full view, or just as part of a category blog view or a featured articles view).

Any ideas?

1

You could create a plugin which was triggered by an event such as onContentPrepare, like this:

<?php
defined( '_JEXEC' ) or die;

class plgContentHits extends JPlugin
{
     public function onContentPrepare($context, &$article, &$params, $page = 0)
     {
        $parts = explode(".", $context);
        if (isset($parts[0]) && $parts[0] == "com_content" && isset($parts[1]) && $parts[1] != "article" && isset($article->id))
        {
            $table = JTable::getInstance('Content', 'JTable');
            $table->hit($article->id);
        }
        return true;
    }
}
?>

You can use the $context to check that it's com_content (rather than com_contact for example), and what view is being shown. You have to not count a hit if it's a single article, because that's counted by default.

I haven't debugged this approach fully, but you may be able to use it as the basis for a solution.

|improve this answer|||||
  • 1
    FYI, isset() can receive multiple parameters. Since you are using all && in your conditional expression, you can condense it to: if (isset($article->id, $parts[0], $parts[1]) && $parts[0] == "com_content" && $parts[1] != "article") Another technique would be to use a regular expression instead of exploding if so inclined (maybe something akin to: regex101.com/r/4Pg4mx/1). – mickmackusa Dec 11 '19 at 21:58
  • 1
    @mickmackusa Thanks, I didn't realise that - useful to know! – Robbie Jackson Dec 11 '19 at 22:59
  • Hmmm. Not sure you want to check for com_content. I may be reading too much into it, but the OP seems to want a count of how many times the introtext was shown to a user, and a module could still display the introtext on any page at all. And then there are independent components like K2 or the like that could be displaying it. – Arlen Dec 17 '19 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.