• Resolved Tee Lock

    (@foxonfox)


    This issue is related to REST API, yet its connected to jetpack afaik

    GET : /sites/$site/stats/post/$post_id

    This endpoint can only return stats of a single post. Is there any way to return stats of multiple posts at once ?

    like post=123,145,156

    My blog has more than 5000 posts, so I want to update stats of them every hour, I will have to make 5000 API Calls per hour. Won’t it be an API Abuse ?

    https://wordpress.org/plugins/jetpack/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    You could use batch to return several endpoints at once:
    https://developer.wordpress.com/docs/api/1.1/get/batch/

    However, instead of refreshing data for all 5,000 posts every hour, it might be best to only trigger an update when a post is actually visited. I assume that some of your old posts don’t get visits every day.
    You could add the post ID to an array whenever it’s visited, and then query for stats for all post IDs in that array every hour. You could also refresh older posts once a day for example, and only do hourly queries for your most recent posts.

    Another alternative would be to use the old WordPress.com Stats API. It allows you to query for multiple post IDs at once. You can find out more about it here:
    https://stats.wordpress.com/csv.php
    http://wpgr.org/2013/03/02/rendering-jetpack-stats/

    I hope this helps.

    Thread Starter Tee Lock

    (@foxonfox)

    Thank you. It works.

    btw the batch method doesn’t let me use endpoint args. Like, when I try to select only data field from the post views endpoint it still loads all the data of post views endpoint call including author, blah blah and even post content. I think this slows down the response.

    Any work around for this ?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Could you give me an example of the query you ran, so I can run some tests?

    Thread Starter Tee Lock

    (@foxonfox)

    /batch/?urls[]=/sites/-removed-/stats/post/1234?fields=data&urls[]=/sites/-removed-/stats/post/2345?fields=data

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Thanks! I was able to reproduce the issue. It seems the batch endpoint doesn’t support the fields parameter yet. I took note of it, and we’ll get working on that.

    I’ll post again here as soon as I have some news!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get stats of multiple posts with a single REST API call’ is closed to new replies.