Premium Analytics: port Stats Search Terms widget#49985
Conversation
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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 🤖 🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so: 🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so: Follow this PR Review Process:
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. |
Code Coverage SummaryNo summary data is available for parent commit aedf21b, 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 aedf21b is available. Coverage check overridden by
Covered by non-unit tests
|
ec5da44 to
ea30566
Compare
…erns (#49987) * Premium Analytics: document widget conventions and Stats porting patterns Consolidates conventions surfaced during code review of the Locations (#49513), Devices (#49928), and Search Terms (#49985) ports into shared agent guidance so future batch ports have a single source of truth. - `.agents/rules/widgets.md` — adds Two-component structure section (outer WidgetRoot / inner useWidgetRootContext, reportParams from context) - `AGENTS.md` — adds package-wide pitfalls (alias naming, English comments) and new Widget pitfalls (presentation source of truth, attribute type locality, button type, inline styles, toolkit helpers); new Stats widgets section covering data hooks, data access path, max=0 semantics, loading strategy, comparison placeholders, and visual conventions - `.agents/skills/widget-audit.md` — extends checklist with presentation consistency, ghost attributes, alias names, WidgetRenderProps<T> import, inline styles, button type, i18n scope, and English comments Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * changelog: add entry for widget conventions docs update Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * Premium Analytics: clarify widget guidance * Premium Analytics: refine widget guidance --------- Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…erns (#49987) * Premium Analytics: document widget conventions and Stats porting patterns Consolidates conventions surfaced during code review of the Locations (#49513), Devices (#49928), and Search Terms (#49985) ports into shared agent guidance so future batch ports have a single source of truth. - `.agents/rules/widgets.md` — adds Two-component structure section (outer WidgetRoot / inner useWidgetRootContext, reportParams from context) - `AGENTS.md` — adds package-wide pitfalls (alias naming, English comments) and new Widget pitfalls (presentation source of truth, attribute type locality, button type, inline styles, toolkit helpers); new Stats widgets section covering data hooks, data access path, max=0 semantics, loading strategy, comparison placeholders, and visual conventions - `.agents/skills/widget-audit.md` — extends checklist with presentation consistency, ghost attributes, alias names, WidgetRenderProps<T> import, inline styles, button type, i18n scope, and English comments Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * changelog: add entry for widget conventions docs update Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * Premium Analytics: clarify widget guidance * Premium Analytics: refine widget guidance --------- Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
chihsuan
left a comment
There was a problem hiding this comment.
I ran the three stories in Storybook and none of them render the widget with data. We should feeding fixtures.
Also left a few suggestions/questions.
| * @param root0 - Render props. | ||
| * @param root0.max - Maximum number of rows to display. |
There was a problem hiding this comment.
nit:
| * @param root0 - Render props. | |
| * @param root0.max - Maximum number of rows to display. | |
| * @param props - Render props. | |
| * @param props.max - Maximum number of rows to display. |
There was a problem hiding this comment.
Done, applied your suggestion.
| * @param root0 - Render props. | ||
| * @param root0.attributes - Widget attributes (max, reportParams). |
There was a problem hiding this comment.
nit
| * @param root0 - Render props. | |
| * @param root0.attributes - Widget attributes (max, reportParams). | |
| * @param props - Render props. | |
| * @param props.attributes - Widget attributes (max, reportParams). |
There was a problem hiding this comment.
Done, applied your suggestion.
|
|
||
| const report = primary.data as StatsNormalizedReport< StatsSearchTermsItem > | undefined; | ||
| const rawItems = report?.data?.[ 0 ]?.items ?? []; | ||
| const items = rawItems.map( toSearchTermView ).slice( 0, max > 0 ? max : undefined ); |
There was a problem hiding this comment.
Just to confirm: does the API support a max parameter so we don’t need to handle this on the client side?
There was a problem hiding this comment.
The Stats search-terms endpoint doesn't expose a max or num param for limiting items per period — it returns all terms up to a backend cap. Client-side slicing is the correct approach here.
| currentShare: maxValue > 0 ? ( term.views / maxValue ) * 100 : 0, | ||
| previousShare: 0, | ||
| delta: 0, | ||
| } ) ) as LeaderboardChartData; |
There was a problem hiding this comment.
Is it possible to extract this builder as a function or use useMemo< LeaderboardChartData > so we don't need to use as here?
There was a problem hiding this comment.
Done — switched to useMemo<LeaderboardChartData>() so the generic types the return value and the as cast is no longer needed.
| } | ||
|
|
||
| /* Constrain the picker preview tile to a 4:3 aspect ratio. */ | ||
| :global([inert]:not([inert="true"])) .root { |
There was a problem hiding this comment.
I'm curious why this is needed? And do you think if any potential upstream improvements can avoid this?
There was a problem hiding this comment.
This targets the widget picker's preview tile. The dashboard sets inert (without inert="true") on the preview container to disable pointer events during the "pick a widget" flow. The selector constrains the widget to a 4:3 aspect ratio so it fits cleanly in the tile rather than overflowing to full height. The same pattern is used in the other widgets (devices, top-platforms). Upstream improvement would be a CSS class the host applies to the preview container instead — happy to file a follow-up ticket if that's useful.
| .widgetHeader { | ||
| padding: var(--wpds-dimension-padding-md) var(--wpds-dimension-padding-lg); | ||
| min-height: 32px; | ||
| font-size: 13px; |
There was a problem hiding this comment.
| font-size: 13px; | |
| font-size: var(--wpds-typography-font-size-md); |
There was a problem hiding this comment.
Done, applied your suggestion.
|
|
||
| .widgetHeader { | ||
| padding: var(--wpds-dimension-padding-md) var(--wpds-dimension-padding-lg); | ||
| min-height: 32px; |
There was a problem hiding this comment.
| min-height: 32px; | |
| min-height: var(--wpds-dimension-size-md); |
There was a problem hiding this comment.
Done, applied your suggestion.
| currentValue: term.views, | ||
| previousValue: 0, | ||
| currentShare: maxValue > 0 ? ( term.views / maxValue ) * 100 : 0, | ||
| previousShare: 0, |
There was a problem hiding this comment.
Why do we hardcode previousValue/previousShare/delta: 0 here?
There was a problem hiding this comment.
The Stats search-terms API only returns current-period data — there's no comparison endpoint for search terms, so previous-period values aren't available. Hardcoding them to 0 is intentional; LeaderboardChart uses delta only to show the trend arrow/sparkline, which stays hidden when delta is 0. If comparison support is added to the API in future, we can wire it up here.
registerReportMocks() covers Woo analytics paths out of the box but not Stats proxy paths (/proxy/v1.1/stats/*). Stats widgets need their endpoint wired into routeStatsReport() in register-report-mocks.ts. Discovered via PR #49985 review feedback. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
chihsuan
left a comment
There was a problem hiding this comment.
Built it locally and all three stories render correctly. Thanks @dognose24 👍
One thing I noticed while reviewing the WidgetDashboardWithWidget story:
When rows exceed the tile height you get a scrollbar instead of the rows fitting the space. Not a regression here. It comes from the shared LeaderboardChart (__content { overflow: auto }), so Top Posts behaves the same. Worth a follow-up at the chart / widgets-toolkit layer so it fixes both widgets in one place. Not blocking.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add itemLabel wrapper with padding (mirrors top-platforms pattern) so the overlay bar gets its height from the label element. Pass loading prop and use chart's built-in emptyStateText. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- widget.ts: add `as const` to attributes type field so it narrows to literal 'integer' rather than string (fixes StoryWidgetMetadata mismatch) - use-search-term-views: cast reportParams to StatsReportParams (same pattern as use-location-views) Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…is source of truth) Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…empty state Remove manual empty/loading early returns that bypassed the header. Use WidgetLoadingOverlay for initial load, and LeaderboardChart's built-in emptyStateText for no-data state (same pattern as top-platforms). Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…with devices) Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Mirrors the top-platforms pattern to guarantee unique React keys. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- Storybook: add Stats proxy mock handler so stories render real data. registerReportMocks() now intercepts /proxy/v1.1/stats/* in addition to the existing /proxy/v2/analytics/reports/* coverage; adds search-terms fixture data (primary + comparison). - render.tsx: fix JSDoc @param tags (root0 → props); use useMemo<LeaderboardChartData> generic instead of `as` cast. - style.module.css: replace hardcoded 32px/13px with WPDS tokens (--wpds-dimension-size-md / --wpds-typography-font-size-md). Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
registerReportMocks() covers Woo analytics paths out of the box but not Stats proxy paths (/proxy/v1.1/stats/*). Stats widgets need their endpoint wired into routeStatsReport() in register-report-mocks.ts. Discovered via PR #49985 review feedback. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
useStatsSearchTerms already fetches both primary and comparison periods when compare_from/compare_to are present. useSearchTermViews now reads the comparison result, builds a label→views map, and pairs each primary term with its comparison view count. render.tsx uses previousViews for previousValue/previousShare/delta so LeaderboardChart shows trend arrows and sparklines in comparison mode. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
0199035 to
ccd14e8
Compare
LeaderboardChart requires withComparison={true} to render delta arrows
and previous-period bars. Wire hasComparison from useSearchTermViews.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Long search terms caused the bar height to grow when the widget was narrow. Add overflow/text-overflow to the label so it ellipsis-clips. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Why
Port the Jetpack Stats Search Terms module into Premium Analytics as a standalone widget (`jpa/search-terms`), closing WOOA7S-1496.
The data layer (`useStatsSearchTerms`, `sanitizeStatsSearchTermsResponse`, fixtures, tests) was already complete on trunk. This PR adds only the widget UI layer.
What
Notes
Search terms data requires organic search traffic from indexed sites. On sites with no search traffic for the selected period, the widget correctly shows an empty state. Selecting a longer date range (e.g. last year) will surface terms if the site has historical search data.
Test plan
🤖 Generated with Claude Code