cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 82 forks source link

Only using it in the backend, yet still outputs JSON that takes a while to load #256

Closed 1BJK903 closed 4 years ago

1BJK903 commented 4 years ago

Hello there,

First of all, thank you for the nice plugin. It works great. I have thought of adding a bug here or simply removing the plugin, as I didn't know if there would be a solution or not, but let's try. I like the app, so I want to keep it.

Here is my issue. https://ibb.co/xjrrVDy

As you can see, it's a file (?) that is 620 bites, but it takes more than 1.3 seconds to load. This is not cool regarding performance.

I have a couple of questions: 1) Why is this happening? 2) What does that file do? Guessing it's tracking the clicks, right? Because I am not outputting any widget or whatsoever in the front-end. 3) Can we remove it from certain pages (like dequeuing?)? If so, how? 4) Maybe this question is better: can we, by default, exclude all pages to track? I only want my posts to be tracked and not my pages.

Thanks in advance for your answer.

cabrerahector commented 4 years ago

Hi there,

What does that file do?

Why is this happening?

This JS file is keeping track of page views.

It's only a few bytes in size, yes, but it also sends a POST request to the server asking it to register the current page view to the database.

The loading time you're seeing here is how long it takes the browser to load and parse the JS file itself (and since the file is quite small this should happen almost instantly) + the time it takes your server to process the AJAX request.

Can we remove it from certain pages (like dequeuing?)? If so, how?

No, there's no way to do this at the moment (that I know of that works reliably at least.)

Maybe this question is better: can we, by default, exclude all pages to track? I only want my posts to be tracked and not my pages.

Yes, you can: wpp_trackable_post_types.

1BJK903 commented 4 years ago

Hi Hector,

Thank you for the fast response. This is clear and it helps! Will there be an option coming later on to disable pages by default?

Anyhow, last question:

/**
 * Have WPP track only 'post' and 'review' post types page views, ignore the rest.
 *
 * @param  array $post_types
 * @return array
 */
function my_trackable_post_types($post_types){
    $track_these_post_types_only = array('post', 'review');
    return $track_these_post_types_only;
}
add_filter('wpp_trackable_post_types', 'my_trackable_post_types', 10, 1);

So this is for post types. But I just want the default page type. Can I use this snippet for that as well? Can you give an example?

cabrerahector commented 4 years ago

Thank you for the fast response. This is clear and it helps!

Don't mention it. Glad I could help!

Will there be an option coming later on to disable pages by default?

No need. Read below why.

So this is for post types. But I just want the default page type.

I think you're a bit confused here. The default post types WordPress includes are:

(More details here.)

By "default page type" I assume you're talking about posts here. So, based on what you mentioned on your previous comment, if you want WPP to only track posts and not pages (nor other post types) then that's what this filter hook is for.

Here's how you should use it so WPP only keeps track of posts:

/**
 * Have WPP track only posts, ignore the rest (pages, attachments, custom post types, etc.)
 *
 * @param  array $post_types
 * @return array
 */
function my_trackable_post_types($post_types){
    // Here is where you specify which post type(s) to track:
    $track_these_post_types_only = array('post');

    return $track_these_post_types_only;
}
add_filter('wpp_trackable_post_types', 'my_trackable_post_types', 10, 1);

Hope that clarifies things a bit.

1BJK903 commented 4 years ago

Hi Hector, thanks again. Maybe I wasn't clear enough, but I actually wanted to remove the plugin from tracking pages (like Contact, etc.). It is there now and I want to get rid of all non-single-posts, if that makes sense.

Can I use the snippet for that as well?

cabrerahector commented 4 years ago

Ah, well, that code snippet asks WPP not to track views unless it's a single post. The JS file will still be loaded though but it won't send a POST request to the server unless we're viewing a single post.

Does that make sense or do you need more details?

1BJK903 commented 4 years ago

I understand and that is exactly where I was at this moment haha. The next step for me would be: how can I make sure it's not even loaded on the page when it's not a single post? As you have seen, it was causing a 1.3 second delay for me, while there is nothing to track on that page....

So I could use your help there.

cabrerahector commented 4 years ago

That doesn't sound right. The script doesn't do anything unless it's executed by WPP when certain conditions are met (the current page is trackable, or there's a WPP widget on the page using it, etc.)

I'd do some testing but I'm busy at the moment (I work as a full-time remote developer so the quarantine doesn't really affect me). I'll get back to you once I find some spare time for this.

What's your site's URL, by the way?

cabrerahector commented 4 years ago

Alright, so I'm testing the code snippet now and it's working as intended for me.

Without the code snippet:

WPP is tracking everything - including pages, which is the default behavior.

Here's what the Network inspector shows when I visit my About page (I'm displaying only AJAX requests here for simplicity):

Network inspector showing WPP tracking pages

With the code snippet:

Now with the code snippet in place, here's what my Network inspector shows when I visit my About page:

Network inspector showing WPP not tracking pages, no requests are made

The plugin is no longer sending POST requests even though the JS file is still being loaded by the browser when visiting my About page:

WPP's JS in Network inspector

If you're seeing a different behavior then my guess is that you either placed the code snippet on the wrong place and it is not being run at all or that you have a caching system in place (WP Super Cache, Varnish, etc) and forgot to clear the cache after adding the code snippet to your site.

cabrerahector commented 4 years ago

Closing issue due to inactivity.