Closed dnikola closed 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:
POST
requests to the REST API.Once you have all that sorted out, then this is what you need:
/wp-json/wordpress-popular-posts/v1/popular-posts
POST
_wpnonce
(although I'm not sure it this is even necessary if your app is already authenticated so please give it a try and let me know).Hope that helps.
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
Hi! Thanks for your fast replay!
This second post sound like a great solution, let me test and I will let you know!
Nikola
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
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.
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.
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 :) ?
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.
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