Skip to content

Premium Analytics: add Top posts & pages stats widget#49508

Open
louwie17 wants to merge 33 commits into
trunkfrom
wooa7s-1489-port-stats-top-posts-widget
Open

Premium Analytics: add Top posts & pages stats widget#49508
louwie17 wants to merge 33 commits into
trunkfrom
wooa7s-1489-port-stats-top-posts-widget

Conversation

@louwie17

Copy link
Copy Markdown
Contributor

Fixes WOOA7S-1489

Proposed changes

Ports the Jetpack Stats "Top posts & pages" card to Premium Analytics as the jpa/stats-top-posts dashboard widget. This is the kickoff Stats card port, establishing the pattern for the rest of the Stats cards.

  • widgets/top-posts/ — new widget (framed presentation): renders the top-viewed posts/pages for the site as a LeaderboardChart (no comparison period), with each row label linking to the published post in a new tab. Period/date/count are hardcoded for v1 via attribute defaults (period: day, num: 10, date = today in the site timezone, computed at render time). Attributes flow to the inner component via props, not WidgetRootContext (whose report params are WC-Analytics-shaped).
  • Data package (packages/data):
    • useReportTopPosts hook — uses useQuery directly (stats has no comparison concept in v1), client-side post-type filtering via the name param, and returns normalized { label, value, href, type } rows (a port of Calypso's statsTopPosts normalizer minus the Calypso UI fields).
    • fetchReportTopPosts fetcher against the existing jetpack/v4/stats-app/sites/{site_id}/stats/top-posts proxy provided by the jetpack-stats-admin package — no new REST proxy and no new composer dependency. The hook's num (post count) maps to the WPCOM API's max param; the API's own num means "number of periods" and is left at its default of 1.
    • getJpaConfig() util reading the new window.jpaConfig boot config, plus getStatsApiPath() building the proxy base path from the runtime site ID.
  • Config_Data PHP class — emits window.jpaConfig = { siteId, apiRoot, nonce } (a much smaller take on stats-admin's Odyssey_Config_Data). It attaches as a before inline script on the generated *-prerequisites script handle via the *_boot_dependencies filters, which fire right after the handle is registered in both generated page variants (the full-page interceptor never fires admin_print_scripts).
  • Toolkit/test plumbing: export WidgetLoadingOverlay from widgets-toolkit, include widgets/** in tsconfig.json, stub node_modules CSS imports in the jest config, and add @testing-library/react as a devDependency.

Notes for reviewers:

  • The widget assumes Jetpack is installed alongside Premium Analytics (the stats proxy 404s otherwise) — accepted v1 constraint.
  • The widget registry/dashboard rendering pipeline (widget-modules.php, the widget dashboard stage) is landing separately; this PR follows its widgets/<name>/{widget.json,widget.ts,render.tsx} conventions so the widget is discovered once that work merges.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No. It reads existing Jetpack Stats data through the existing jetpack-stats-admin REST proxy.

Testing instructions

Automated:

  • cd projects/packages/premium-analytics && pnpm test — fetcher URL-composition tests and widget smoke tests (render, post-type filtering, empty state).
  • pnpm typecheck in the same directory.

Manual (requires the widget dashboard branch for full rendering; until then the pieces can be verified in isolation):

  • On a site with both Jetpack (connected, with stats) and Premium Analytics active, open the Premium Analytics admin page.
  • View the page source and confirm window.jpaConfig = { siteId, apiRoot, nonce } appears before the boot module import script.
  • In the browser console, fetch the proxy path from the config (<apiRoot>jetpack/v4/stats-app/sites/<siteId>/stats/top-posts?period=day&date=<today>&max=10 with the X-WP-Nonce header) and confirm it returns a days payload.
  • With the widget dashboard branch merged in, add the "Top posts & pages" widget and confirm it lists the top-viewed posts with view counts, links open the posts in a new tab, and a site with no views shows "No views in this period."

chihsuan added 30 commits June 5, 2026 15:09
chihsuan and others added 3 commits June 5, 2026 15:21
Port the Jetpack Stats "Top posts & pages" card as the jpa/stats-top-posts
dashboard widget (WOOA7S-1489):

- widgets/top-posts: framed list widget rendering the leaderboard chart
  with post-title links, hardcoded period/date/num defaults for v1
- data package: useReportTopPosts hook (useQuery directly, no comparison),
  top-posts fetcher against the existing jetpack/v4/stats-app proxy, and
  getJpaConfig() reading the new window.jpaConfig boot config
- Config_Data PHP class emits window.jpaConfig (siteId, apiRoot, nonce)
  before the boot script on both generated page variants

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@louwie17 louwie17 added the [Status] Needs Review This PR is ready for review. label Jun 10, 2026
@louwie17 louwie17 self-assigned this Jun 10, 2026
@louwie17 louwie17 added the Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Jun 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor
Are you an Automattician? The PR will need to be tested on WordPress.com. This comment will be updated with testing instructions as soon the build is complete.

@github-actions

github-actions Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Premium Analytics plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

No summary data is available for parent commit 01f7aa9, so cannot calculate coverage changes. 😴

If that commit is a feature branch rather than a trunk commit, this is expected. Otherwise, this should be updated once coverage for 01f7aa9 is available.

Full summary · PHP report · JS report

Base automatically changed from wooa7s-1319-integrate-widgets-toolkit-package-into-analytics to trunk June 10, 2026 23:16
@github-actions github-actions Bot added the Docs label Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Docs Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Package] Premium Analytics [Plugin] Premium Analytics [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants