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 83 forks source link

Update views count from external app #229

Closed dnikola closed 4 years ago

dnikola commented 4 years ago

Hello

I would like to update no of views from external app.

My plan is to create custom endpoint and to pass postID value of post for which we should increment not of post. Can you tell me what function should i call, used by your plugin so I can just pass an ID of post, so your plugin can take care of rest of process?

Thanks! Nikola

cabrerahector commented 4 years ago

Hi @dnikola,

I'm not entirely sure because I haven't tried myself but you should be able to update the views count of your posts/pages from an external app (React, Android/iOS app, etc) via the REST API. To do so:

  1. You need to authenticate your app so it gets write access to the REST API.
  2. Once your app has been authenticated it should be able to send POST requests to the REST API.

Once you have all that sorted out, then this is what you need:

URL:

/wp-json/wordpress-popular-posts/v1/popular-posts

Method:

POST

Parameters:

Hope that helps.

cabrerahector commented 4 years ago

Alternatively, if you already have a custom endpoint then you may want to try doing this instead:

$views_count_update_request = new WP_REST_Request(
    'POST',
    '/wordpress-popular-posts/v1/popular-posts'
);
$params = array(
    'wpp_id' => 'YOUR-POST-ID-HERE'
);
$views_count_update_request ->set_query_params($params);

rest_do_request($views_count_update_request); // returns a WP_REST_Response object
dnikola commented 4 years ago

Hi! Thanks for your fast replay!

This second post sound like a great solution, let me test and I will let you know!

Nikola

dnikola commented 4 years ago

Hi

this is some kind of code we are trying to use

add_action('rest_api_init', function () {
  register_rest_route( 'wp/v2/posts/post_views_update', '(?P<postID>\d+)',array(
                'methods'  => 'GET',
                'callback' => 'post_view_update'
      ));
});

function post_view_update($request) {
    $views_count_update_request = new WP_REST_Request(
        'POST',
        '/wordpress-popular-posts/v1/popular-posts'
    );
    $params = array(
        'wpp_id' => $postId
    );
    $views_count_update_request ->set_query_params($params);

    rest_do_request($views_count_update_request); 
}

And calling a url like wp-json/wp/v2/posts/post_views_update/889174 put a value in table http://prntscr.com/p7hcso

than i tried to use values like

$params = array(
        'wpp_id' => $postId,
        'sampling' => '1',
        'sampling_rate' => '10'

    );

than for each visit post count is update but with value 10 instead of 1, and not every 10th, that is second row on screenshot http://prntscr.com/p7hcso

cabrerahector commented 4 years ago

That's the expected behavior, @dnikola. See here for more details: How does Data Sampling work?

Since the original question has been answered already (Update views count from external app) I'm closing this issue now. If you have any additional questions don't hesitate to ask.

cabrerahector commented 4 years ago

On a second thought, the whole Data Sampling logic isn't handled by the REST API endpoint but by the browser (see related code) so actually it might be a good idea to not use the data sampling parameters in your request or to re-implement the data sampling logic in your custom controller so it mimics the behavior from the JS code.

dnikola commented 4 years ago

Hector,

thanks for your kind replay!

May I ask you if you can explain me from this code how is calculated sampling rate

for me it looks like it is random :) ?

cabrerahector commented 4 years ago

No problem. That code basically generates a random number between 1 and N, being N the Sampling Rate (eg. 10).

If the generated random number is equal to 1 then proceed to update the views count, otherwise don't do anything.

If you want to use Data Sampling for performance reasons, you may want to consider using this solution instead: Caching Pageviews.