Fix activity kit stats: switch to get_total_post_views() for per-kit view counts - #3591
Fix activity kit stats: switch to get_total_post_views() for per-kit view counts#3591Piyopiyo-Kitsune wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses incorrect “0 views” reporting on the Activity Kit Stats REST endpoint by switching Jetpack Stats fetching to a per-post-ID views API, ensuring newly published kits are included even when they aren’t in the site-wide “top posts” list.
Changes:
- Update the stats REST handler to fetch view counts by explicit kit post IDs via
WPCOM_Stats::get_total_post_views(). - Adjust parsing to match the
views/postsresponse shape (including using uppercaseID). - Add an “Activity Library” item to the theme’s site navigation menus.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| wp-content/themes/pub/wporg-learn-2024/functions.php | Adds an “Activity Library” entry to the Learn navigation menu. |
| wp-content/plugins/wporg-learn/inc/activity-kit-rest.php | Switches Jetpack view retrieval to a per-post-ID endpoint and updates response parsing for accurate per-kit view counts. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…_posts() The stats page was showing 0 views for all kits despite Jetpack being connected and the site receiving thousands of daily page views. Root cause: get_top_posts() returns the top N most-viewed posts on the entire site, ranked by accumulated all-time views. With max=1000 and years of established content (courses, lessons, learning pathways) outranking them, newly published activity kits never appear in that list and silently return 0. Fix: Switch to get_total_post_views() which calls the stats/views/posts endpoint and fetches view counts for specific post IDs directly, regardless of site-wide ranking. The kit post IDs are already known from the get_posts() query, so they are passed as a comma-separated post_ids parameter. Also update the response key parsing: the views/posts API returns uppercase 'ID' for the post identifier, whereas top-posts used lowercase 'id'. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
bd0ddb6 to
e9e1ee0
Compare
|
Code review findings The core change is correct — 1. Blocker:
|
Address three issues from obenland's code review: 1. Blocker – remove num > 30: The views/posts API rejects any num > 30 with a 422. The previous 'all' branch sent num=36 (months) and the '90d' branch sent num=90 — both always returned a WP_Error, so every kit showed 0 views on the default page load. Fix: cap each call at 30 days and issue multiple windows for longer ranges (7d=1 call, 30d=1 call, 90d/all=3 × 30-day calls offset by 0/30/60 days), then sum the results. 2. Remove silent period param: views/posts only accepts post_ids, num, date, and offset — any other arg is discarded by the WPCOM JSON API framework. Passing period='month' encoded an intent the API could never honor and mirrored the structure of get_jetpack_download_clicks() in a misleading way. 3. Chunk post_ids at 100: the endpoint accepts at most 100 IDs per call. With posts_per_page=-1 the library can grow past that limit and silently lose data. Use array_chunk(, 100) and merge the maps. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for the thorough review — all three points addressed in f73fd26. 1. Blocker — 2. 3. 100-ID cap on The
|
Previously both '90d' and 'all' fell through to the same 3×30-day case, so selecting 'All time' returned identical data to 'Last 90 days'. Give 'all' its own 6-window case (offsets 0/30/60/90/120/150 days ≈ 6 months) so the two ranges return meaningfully different data. Also update the JS rangeLabel() comment and label from 'All time (max 90 days)' to 'All time (max ~6 months)' to keep the UI in sync. Extending further (e.g. 1 year = 12 windows) is possible but multiplies sequential API calls by the same factor; 6 is a reasonable ceiling for an admin-only dashboard with a small kit count. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Problem
The Activity Kit Stats page shows 0 views for all kits despite Jetpack being connected and the site receiving thousands of daily page views (confirmed via Jetpack Stats).
Root cause
get_jetpack_post_views()was callingWPCOM_Stats::get_top_posts()— which returns the top 1,000 most-viewed posts on the entire site, ranked by accumulated all-time view count. Activity kits went live on Aug 11 and have only 1–2 days of traffic. With years of established courses, lessons, and learning pathways outranking them, the kits don't appear in the top 1,000 and the function silently returns 0 for every kit.This was confirmed by checking the Jetpack Stats admin page, which shows the site receiving ~5,000–8,000 views per day but only lists long-standing content (Beginner WordPress User, Introduction to WordPress, etc.) as top posts — no activity kits in sight.
Fix
Switch to
WPCOM_Stats::get_total_post_views(), which hits thestats/views/postsendpoint and fetches view counts for specific post IDs directly, regardless of site-wide ranking. The kit post IDs are already known from theget_posts()query, so they are passed as apost_idscomma-separated parameter.Also corrects the response key: the
views/postsAPI uses uppercase'ID'for the post identifier;top-postsused lowercase'id'.Testing
🤖 Generated with Claude Code