putyourlightson / craft-entry-count

Counts and displays the number of times that an entry has been viewed in Craft CMS.
MIT License
47 stars 19 forks source link

Not seeing any entries #33

Closed wolispace closed 1 month ago

wolispace commented 1 month ago

Bug Report

I know this isn't a super-important plugin, but it does still exist so Im wondering why I see no entries on the page that lists all counts?

/entry-count?site=default

Removing or changing the ?site= makes no difference.

I see in the template: /vendor/putourlightson/craft-entry-count/src/templates/index.twig

{% set entries = craft.entryCount.entries.all() %}

And when I {% dd entries %} I just get an empty array []

When I run this sql I see I have a lot of entries counted:

select id,entryId,count,dateUpdated from entrycount order by dateCreated desc limit 10;
id  entryId count   dateUpdated
911 494721  9   2024-09-26 04:12:12
910 494754  2   2024-09-24 02:35:42
908 493959  57  2024-09-30 06:53:45
907 493889  64  2024-09-26 03:56:39
906 493792  55  2024-09-26 01:20:15
905 493186  63  2024-09-26 00:17:16
904 493098  46  2024-09-25 16:44:06
903 492066  51  2024-09-25 16:45:42
902 492070  85  2024-09-30 07:07:50
901 493046  83  2024-09-25 12:04:36

Looking in the debug console I see this query is being run:

SELECT *
FROM `entrycount`
ORDER BY `count` DESC

This returns data as above, when I run it manually.

We uses this plugin to find out which entries are popular.

Plugin Version

3.0.1

Craft CMS Version

4.11.5

PHP Version

8.0.30

bencroker commented 1 month ago

Have you tested this after using the increment function?

{% do craft.entryCount.increment(entry.id) %}
{% set count = craft.entryCount.count(entry.id) %}
Entry count: {{ count }}
wolispace commented 1 month ago

No I havnt yet, I must admit I havnt read any doco yet or seen how it was implemented.

But my question is: Should I be seeing the existing logged entry counts on the /entry-count?site=default page?

I can see records in the DB, with current dates. Should I see values in entries after {% set entries = craft.entryCount.entries.all() %}?

or has something in a craft4 upgrade stopped this from happening and Im the first to notice?

Im not seeing an related errors on the logs.

Ill go diving onto your code to try and debug it with xdebug, but just checking its still working for you and its probably something unique to our setup.

bencroker commented 1 month ago

You should, provided those entries exist as live entry in the primary site. Before you start code-diving, try incrementing the count of an existing entry first.

wolispace commented 1 month ago

Yes.. when I add

{% set count = craft.entryCount.count(entry.id) %}
Entry count: {{ count }}

To my template I see the count increase each time the page is refreshed.

And running this sql:

select id,entryId,count,dateUpdated from entrycount order by dateUpdated desc limit 10;

I see the same count, and I see it increment in the most recently updated row.

So the incrementing is working, and we definitely have records in the DB.. but I get an empty array when trying to view the page /entry-count?site=default (with or without the site)

Looking in EntryCountService.php I see:

        // Return entry query
        return Entry::find()
            ->id($entryIds)
//            ->site('*')
            ->fixedOrder();

On a whim, I enabled the remarked-out line so it gets site('*') and bingo! we see results on the page.

So the reason we don't see any data on the page is because the counts are recorded for a specific site site=goodliving.

Intriguingly, the twig that relies on the Entry count data, is working just fine:

{% set popularArticles =
  craft.entryCount.entries.section('goodlivingArticles').postDate('and', '>= ' ~ weeksAgo).limit(
    6
  ).with(
    ['contentBuilder', 'teaserImage', 'goodlivingHeroImage']
  ).all()
%}

I cannot imagine how this use of craft.entryCount.entries... finds what we expect when other craft.entryCount.entries... return nothing.

Maybe you can re-instate the ->site('*') ?

Ideally add a Sites filter to the top, so we can choose the counts for different sites... but thats probably asking a bit too much :-)

bencroker commented 1 month ago

Ah yes, that explains it. The plugin is intended for educational purposes and only works with single site use-cases.