2

I'm trying to get the most popular entries of my site in a certain time period, for example 24h, based on the amount of views (clicks) it gets. I'm using the entry count plugin to do the counting but I can't get it working like I want. It can get a most viewed of all time array, but I need a most viewed of the last 24h. I've followed the course that the plugin is based on, but I only get errors when trying to customize it... Can anyone help me out? Thanks!

  • Hi Jan, I am sure native Twig can help out here. Can you show the code you are working with? – Nutmeg Nov 22 '16 at 4:16
  • Hi Nutmeg, Using the entry count plugin I call the "counted' entries like so: {% set countedEntries = craft.entryCount.entries %} and this is the first problem, I can't seem to get them ordered by any way possible except by count number (tried by title, dateCreated, dateUpdated, id). So that's the first hurdle I need to pass. Once that's done, I need to calculate the amount that an article has been viewed in the last 30h, and sort the articles by that amount, setting a limit of 10 – Jan Nov 22 '16 at 7:59
2

The Entry Count plugin does not record the individual times at which each entry was counted, therefore there is no way to filter by a specific time period. For that you'll need to find another solution.

UPDATE:

As you've requested it in the comments, the only way to get this to work without the existing plugin would be to reset all entry counts once a day using a cron job. For sure not ideal but it would require a minimal amount of code refactoring.

To set this up you would need to add an anonymous controller method (that requires a key to be accessed) to reset all entry counts:

public function actionResetAll()
{
    // verify query parameter key
    if (craft()->request->getParam('key') != 'xyzxyzxyzxyzxyzxyz') {
        die('Unauthorised key');
    }

    craft()->entryCount->resetAll();
}

As well as a service method to do the work:

public function resetAll()
{
    // delete all records from DB
    EntryCountRecord::model()->deleteAll();

    // log reset
    EntryCountPlugin::log('All entry counts reset.');
}
| improve this answer | |
  • Considering it is the course example, Ben - would it be possible (for students) to add this feature or the EntryModel to the plugin? – Nutmeg Nov 22 '16 at 19:35
  • Sure, although it would require quite a bit of code refactoring. – Ben Croker Nov 23 '16 at 7:42
  • I'm working on this again, is there any other way to get this done? Of are you working on it already @BenCroker? – Jan Nov 30 '16 at 15:59
  • Have added an update to my answer along with some code samples. Beyond this you're going to essentially have to write some code :) – Ben Croker Dec 4 '16 at 8:24

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.