Ajax Loading News & Update View
-
Hello,
I am using WP Statistics 14.16.13 on a WordPress website with a custom news theme.
I have implemented an infinite scroll for single news posts. The first post is loaded normally, so WP Statistics correctly records its view.
All subsequent posts are loaded via AJAX without reloading the page.
The flow is:
- User opens:
/news/post-1/ - WP Statistics correctly records a view for
post-1. - User scrolls down and
post-2is loaded via AJAX. - Then
post-3,post-4, etc. are loaded via AJAX. - When a new post becomes the currently viewed post, I also update the browser URL using
history.replaceState():/news/post-2//news/post-3/
The problem is that WP Statistics continues to record views for the initially opened page, while the AJAX-loaded posts do not receive their own view count.
I display the post statistics using:
wp_statistics_pages('total', null, get_the_ID())For AJAX-loaded posts,
get_the_ID()is correct and returns the ID of the specific post being rendered.Each AJAX-loaded post also contains its own URL:
<div class="singleNews-block" data-url="https://example.com/news/post-2/" data-title="Post 2" >The browser URL is changed with:
history.replaceState( { url: url }, '', url );I also tried using the WP Statistics REST endpoint:
/wp-json/wp-statistics/v2/hitHowever, the request does not associate the hit with the required
post_id. Instead, the view continues to be attributed to the initially opened post.I need the following behavior:
Initial post #1 → WP Statistics +1 for post ID #1 AJAX-loaded post #2 → when it becomes the currently viewed post → WP Statistics +1 for post ID #2 AJAX-loaded post #3 → when it becomes the currently viewed post → WP Statistics +1 for post ID #3The page itself is never reloaded.
Is there an official PHP API, REST API, hook, or internal method in WP Statistics 14.16.13 that can be used to manually register a view for a specific WordPress post ID during AJAX/infinite-scroll navigation?
I specifically need a method where I can provide the
post_idand record the view for that particular post, rather than having WP Statistics determine the post from the current HTTP request URL.Could you please provide an example implementation compatible with WP Statistics 14.16.13?
Thank you.
The page I need help with: [log in to see the link]
- User opens:
You must be logged in to reply to this topic.