Skip to content

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

Draft
louwie17 wants to merge 12 commits into
update/pa-introduce-customize-dashboardfrom
wooa7s-1489-stats-top-posts
Draft

Premium Analytics: add Top posts & pages stats widget#49568
louwie17 wants to merge 12 commits into
update/pa-introduce-customize-dashboardfrom
wooa7s-1489-stats-top-posts

Conversation

@louwie17

Copy link
Copy Markdown
Contributor

Fixes WOOA7S-1489

Proposed changes

Adds the jpa/stats-top-posts dashboard widget to Premium Analytics, porting the Jetpack Stats "Top posts & pages" card. This is the kickoff Stats card port and establishes the pattern for the rest.

  • widgets/top-posts/ — new widget (framed presentation): renders the top-viewed posts/pages as a LeaderboardChart (no comparison period), 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 and don't fit stats).
  • 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, 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, no new composer dependency. The hook's num (post count) maps to the WPCOM API's max; the API's own num (period count) is left at its default.
    • 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), attached as a before inline script on the generated *-prerequisites handle via the *_boot_dependencies filters so it lands before the boot module import in both generated page variants.
  • Build wiring — links internal packages/* into node_modules (link: deps) so the wp-build widget phase can resolve @jetpack-premium-analytics/* imports. top-posts is the first widget to consume internal packages (hello-world does not), so this path wasn't exercised before. Also exports WidgetLoadingOverlay from widgets-toolkit and includes widgets/** in tsconfig.json / stubs node_modules CSS in the jest config.

Notes for reviewers:

  • Assumes Jetpack is installed alongside Premium Analytics (the stats proxy 404s otherwise) — accepted v1 constraint.
  • pnpm typecheck currently reports errors in the vendored widget-primitives / widget-dashboard js-packages on this base branch — these are pre-existing (reproducible on update/pa-introduce-customize-dashboard without this PR) and unrelated to the widget; none of the errors are in the widget or data-package files.

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 build — confirms top-posts builds and registers as jpa/stats-top-posts in build/widgets/registry.php.

Manual:

  • On a site with both Jetpack (connected, with stats) and Premium Analytics active, open the Premium Analytics admin page.
  • View source and confirm window.jpaConfig = { siteId, apiRoot, nonce } appears before the boot module import script.
  • Add the "Top posts & pages" widget to the dashboard 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."

@louwie17 louwie17 added Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. labels Jun 12, 2026
@louwie17 louwie17 self-assigned this Jun 12, 2026
@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the wooa7s-1489-stats-top-posts branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack wooa7s-1489-stats-top-posts
bin/jetpack-downloader test jetpack-mu-wpcom-plugin wooa7s-1489-stats-top-posts

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

github-actions Bot commented Jun 12, 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.

@louwie17 louwie17 marked this pull request as draft June 12, 2026 01:25
@jp-launch-control

jp-launch-control Bot commented Jun 12, 2026

Copy link
Copy Markdown

Code Coverage Summary

No summary data is available for parent commit d060b2d, 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 d060b2d is available.

Full summary · PHP report · JS report

retrofox added 8 commits June 12, 2026 11:38
Private, source-consumed copy of @wordpress/grid until core publishes it.
Private, source-consumed copy of @wordpress/widget-primitives until core publishes it.
Private, source-consumed dashboard engine; depends on jetpack-widget-primitives and jetpack-grid.
Renders the WidgetDashboard engine via wp-build, bundling the grid/primitives/dashboard packages.
useWidgetTypes drives discovery via /wp/v2/widget-modules; hello-world builds as a lazy-loaded script module.
Hook the page boot-dependencies filter so widget modules reach the import map; move the endpoint to the jetpack/v4 namespace.
@wordpress/grid and widget-primitives references in widget-dashboard JSDoc/comments/README, per the faithful-source sync policy.
@louwie17 louwie17 force-pushed the update/pa-introduce-customize-dashboard branch from 909996e to d060b2d Compare June 12, 2026 02:39
louwie17 and others added 3 commits June 12, 2026 12:16
Add the jpa/stats-top-posts dashboard widget (WOOA7S-1489), porting the
Jetpack Stats "Top posts & pages" card:

- widgets/top-posts: framed list widget rendering the leaderboard chart
  with post-title links; period/date/num hardcoded 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
- Link internal packages/* into node_modules so the wp-build widget phase
  can resolve @jetpack-premium-analytics/* imports (top-posts is the first
  widget to consume them; hello-world does not)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Align with the co-located __tests__ convention used by the package's
internal packages/* instead of the one-off tests/ dir.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…troller

Point the top-posts data layer at the jetpack-premium-analytics/v1/proxy
controller (stats/top-posts) instead of building the jetpack/v4/stats-app
URL with a client-side site ID. The proxy injects the connected blog ID and
authenticates the upstream WPCOM request.

Remove the now-unused site-id machinery: getStatsApiPath, the getJpaConfig
helper / window.jpaConfig type, and the Config_Data PHP emitter (and its
wiring in class-analytics.php).

Also folds in this session's widget refinements: 7-day summarized range,
overlay-label bar styling, and the "Top pages by views" title.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@louwie17 louwie17 force-pushed the wooa7s-1489-stats-top-posts branch from f1a7fee to 2899350 Compare June 12, 2026 03:22
…ts API

The Api_Proxy_Controller always forwarded to /sites/{id}/analytics/<endpoint>
on the v2 analytics API, so stats/top-posts 404'd upstream (Stats lives on the
v1.1 REST API). Route stats/* endpoints to /sites/{id}/stats/... on v1.1 (rest
base); all other endpoints keep targeting the v2 analytics namespace.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nerrad nerrad force-pushed the update/pa-introduce-customize-dashboard branch from d060b2d to 5bb6dd9 Compare June 12, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Package] Premium Analytics [Plugin] Premium Analytics [Status] In Progress [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