From 962db00aa4031208684be6f99829c8907549eeac Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Fri, 14 Aug 2026 12:45:30 +1200 Subject: [PATCH 1/6] Premium Analytics: add Regions to the Top locations "View by" control MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The widget's dropdown offered only Countries and Cities, so a site owner could reach region data only by drilling into one country at a time — while the Locations report has had a Regions tab all along. Regions mode is worldwide, like Cities: the leaderboard lists regions and the map sums them back up to their country, since Google GeoChart cannot place sub-country rows on the world map. --- .../add-wooa7s-1940-locations-regions-view | 4 ++ .../src/stories/mocks/register-stats-mocks.ts | 9 +++- .../locations/__tests__/locations.test.tsx | 49 +++++++++++++++---- .../widgets/locations/render.tsx | 48 +++++++++--------- .../stories/locations-widget.stories.tsx | 14 ++++-- .../widgets/locations/widget.ts | 14 ++++-- 6 files changed, 97 insertions(+), 41 deletions(-) create mode 100644 projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view diff --git a/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view b/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view new file mode 100644 index 000000000000..aaa5de37b70d --- /dev/null +++ b/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Locations: offer Regions in the widget's "View by" control. diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/stories/mocks/register-stats-mocks.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/stories/mocks/register-stats-mocks.ts index dff09b6a89ea..52ac384e7af4 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/stories/mocks/register-stats-mocks.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/stories/mocks/register-stats-mocks.ts @@ -1100,9 +1100,16 @@ function getLocationRows( } if ( geoMode === 'region' ) { - const countryCode = query.get( 'filter_by_country' ) || 'US'; + const countryCode = query.get( 'filter_by_country' ); const regionRows = isComparison ? REGION_COMPARISON_ROWS_BY_COUNTRY : REGION_ROWS_BY_COUNTRY; + // Unfiltered, the endpoint returns the top regions worldwide. + if ( ! countryCode ) { + return Object.values( regionRows ) + .flat() + .sort( ( a, b ) => b.views - a.views ); + } + return regionRows[ countryCode ] ?? regionRows.US; } diff --git a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx index de0948c57cad..7209ddfee05d 100644 --- a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx +++ b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx @@ -38,21 +38,27 @@ jest.mock( '@wordpress/route', () => ( { useSearch: () => ( {} ), } ) ); +const mockUseLocationViews = jest.fn( () => ( { + data: [], + comparisonData: [], + hasComparison: false, + isLoading: true, + isFetching: true, + hasData: false, + isError: false, + isPlaceholderData: false, +} ) ); + jest.mock( '../use-location-views', () => ( { __esModule: true, - default: () => ( { - data: [], - comparisonData: [], - hasComparison: false, - isLoading: true, - isFetching: true, - hasData: false, - isError: false, - isPlaceholderData: false, - } ), + default: ( ...args: unknown[] ) => mockUseLocationViews( ...( args as [] ) ), } ) ); describe( 'LocationsWidget', () => { + beforeEach( () => { + mockUseLocationViews.mockClear(); + } ); + it( 'links to the Locations report', () => { render( ); @@ -61,4 +67,27 @@ describe( 'LocationsWidget', () => { expect.stringContaining( '/reports/locations' ) ); } ); + + it.each( [ + [ undefined, 'countries' ], + [ 'country', 'countries' ], + [ 'region', 'regions' ], + [ 'city', 'cities' ], + ] as const )( 'opens the %s report tab for the %s granularity', ( geoGranularity, section ) => { + render( ); + + expect( screen.getByRole( 'link', { name: 'View all' } ) ).toHaveAttribute( + 'href', + expect.stringContaining( `section=${ section }` ) + ); + } ); + + // Regions mode is worldwide; only the country drill-down scopes it. + it( 'requests unfiltered region rows in Regions mode', () => { + render( ); + + expect( mockUseLocationViews ).toHaveBeenLastCalledWith( + expect.objectContaining( { geoMode: 'region', countryFilter: undefined } ) + ); + } ); } ); diff --git a/projects/packages/premium-analytics/widgets/locations/render.tsx b/projects/packages/premium-analytics/widgets/locations/render.tsx index 0c8f8f413709..9e4e5644a028 100644 --- a/projects/packages/premium-analytics/widgets/locations/render.tsx +++ b/projects/packages/premium-analytics/widgets/locations/render.tsx @@ -63,6 +63,14 @@ const MISSING_MAP_ERROR_MESSAGE = 'Requested map does not exist'; // load each country pays the failed draw (a brief error flash) at most once. const runtimeUnsupportedProvinceMapCountries = new Set< string >(); +type GeoGranularity = NonNullable< LocationsAttributes[ 'geoGranularity' ] >; + +const REPORT_SECTIONS: Record< GeoGranularity, string > = { + country: 'countries', + region: 'regions', + city: 'cities', +}; + function getGeoChartCountryId( countryCode: string ): string { if ( countryCode.toUpperCase() === 'TW' ) { return 'Taiwan'; @@ -91,9 +99,10 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { } = useWidgetDrillDown< DrillDownCountry >(); // The "View by" control lives in the widget host header (the - // `relevance: 'high'` attribute). City mode disables country drill-down. + // `relevance: 'high'` attribute). Only Countries mode drills down, so leaving + // the other modes would strand a selected country the user can't clear. useEffect( () => { - if ( geoGranularity === 'city' ) { + if ( geoGranularity !== 'country' ) { clearSelectedCountry(); } }, [ clearSelectedCountry, geoGranularity ] ); @@ -134,11 +143,15 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { const useCountryFallbackMap = renderGeoMode === 'region' && !! renderSelectedCountry && ! useProvinceMap; const fallbackCountry = useCountryFallbackMap ? renderSelectedCountry : undefined; - const useCityCountryMap = renderGeoMode === 'city'; - const cityCountryRows = useMemo( () => { + // Cities, and Regions outside a country drill-down, span the whole world. + // Google GeoChart can't place either row type on the world map, so both are + // summed back up to their country. + const useCountrySummaryMap = + renderGeoMode === 'city' || ( renderGeoMode === 'region' && ! renderSelectedCountry ); + const countrySummaryRows = useMemo( () => { const countryRows = new Map< string, { countryFull: string; value: number } >(); - if ( ! useCityCountryMap ) { + if ( ! useCountrySummaryMap ) { return []; } @@ -152,7 +165,7 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { } ); return Array.from( countryRows.entries() ); - }, [ data, useCityCountryMap ] ); + }, [ data, useCountrySummaryMap ] ); const handleGeoChartError = useCallback( ( error: GeoChartError ) => { const message = `${ error.message ?? '' } ${ error.detailedMessage ?? '' }`; @@ -201,9 +214,10 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { ); const geoData = useMemo( (): GeoData => { - const useLocationHeader = renderGeoMode === 'region' && ! useCountryFallbackMap; + // Only the provinces map plots sub-country rows; every other map is + // country-scoped, whatever the leaderboard beside it lists. const header: GoogleDataTableColumn[] = [ - useLocationHeader + useProvinceMap ? __( 'Location', 'jetpack-premium-analytics-pkg' ) : __( 'Country', 'jetpack-premium-analytics-pkg' ), __( 'Views', 'jetpack-premium-analytics-pkg' ), @@ -227,10 +241,10 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { ]; } - if ( useCityCountryMap ) { + if ( useCountrySummaryMap ) { return [ header, - ...cityCountryRows.map( + ...countrySummaryRows.map( ( [ countryCode, location ] ): GoogleDataTableRow => [ { v: getGeoChartCountryId( countryCode ), @@ -244,14 +258,7 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { const rows: GoogleDataTableRow[] = data.map( location => [ location.label, location.value ] ); return [ header, ...rows ]; - }, [ - cityCountryRows, - data, - fallbackCountry, - renderGeoMode, - useCityCountryMap, - useCountryFallbackMap, - ] ); + }, [ countrySummaryRows, data, fallbackCountry, useCountrySummaryMap, useProvinceMap ] ); const leaderboardData = useMemo( () => { const maxValue = getCombinedPeriodMax( @@ -389,10 +396,7 @@ export default function Locations( { attributes = {} }: LocationsWidgetProps ) {
- +
diff --git a/projects/packages/premium-analytics/widgets/locations/stories/locations-widget.stories.tsx b/projects/packages/premium-analytics/widgets/locations/stories/locations-widget.stories.tsx index 4f3c4d650bc0..f3f8ca112731 100644 --- a/projects/packages/premium-analytics/widgets/locations/stories/locations-widget.stories.tsx +++ b/projects/packages/premium-analytics/widgets/locations/stories/locations-widget.stories.tsx @@ -93,7 +93,7 @@ const meta = { }, geoGranularity: { control: 'radio', - options: [ 'country', 'city' ], + options: [ 'country', 'region', 'city' ], description: 'The "View by" toolbar attribute rendered by the widget host.', }, }, @@ -101,7 +101,7 @@ const meta = { docs: { description: { component: - 'The "Locations" widget. Shows visitor views by country or city, with country drill-down into regions, using the global dashboard date range. The Countries/Cities view is the `geoGranularity` attribute (`relevance: \'high\'`), exposed as a control by the widget host.', + 'The "Locations" widget. Shows visitor views by country, region, or city, with country drill-down into regions, using the global dashboard date range. The Countries/Regions/Cities view is the `geoGranularity` attribute (`relevance: \'high\'`), exposed as a control by the widget host.', }, }, }, @@ -123,6 +123,14 @@ export const WithComparison: StoryObj< LocationsStoryControls > = { decorators: [ withWidgetCanvas, withStoryRouter ], }; +// Regions mode — region rows worldwide in the leaderboard, aggregated by country +// on the map. +export const RegionsMode: StoryObj< LocationsStoryControls > = { + render: renderLocationsWidget, + args: { withComparison: false, geoGranularity: 'region' }, + decorators: [ withWidgetCanvas, withStoryRouter ], +}; + // Cities mode — city rows in the leaderboard, aggregated by country on the map. export const CitiesMode: StoryObj< LocationsStoryControls > = { render: renderLocationsWidget, @@ -190,7 +198,7 @@ export const WidgetDashboardWithWidget: DashboardStory = { }, geoGranularity: { control: 'radio', - options: [ 'country', 'city' ], + options: [ 'country', 'region', 'city' ], description: 'The "View by" toolbar attribute rendered by the widget host.', }, }, diff --git a/projects/packages/premium-analytics/widgets/locations/widget.ts b/projects/packages/premium-analytics/widgets/locations/widget.ts index be53c225dcaf..cd8b38bfb20a 100644 --- a/projects/packages/premium-analytics/widgets/locations/widget.ts +++ b/projects/packages/premium-analytics/widgets/locations/widget.ts @@ -12,16 +12,16 @@ import { SelectField } from '@jetpack-premium-analytics/fields'; export type LocationsAttributes = { max?: number; - geoGranularity?: 'country' | 'city'; + geoGranularity?: 'country' | 'region' | 'city'; }; /** * Widget type definition. * - * Ported from the Jetpack Stats "Locations" module. v1 ships Countries mode - * (with region drill-down) and Cities mode via the `location-views/{geoMode}` - * endpoint. City rows are listed in the leaderboard and summarized on the map - * by country. + * Ported from the Jetpack Stats "Locations" module. Countries, Regions, and + * Cities modes all read the `location-views/{geoMode}` endpoint; Countries mode + * additionally drills down into one country's regions. Region and city rows are + * listed in the leaderboard and summarized on the map by country. * * Data: fetched via the PA proxy at `stats/location-views/{country|region|city}`. * Date range comes from WidgetRoot's reportParams (the shared dashboard date @@ -49,6 +49,10 @@ export default { label: __( 'Countries', 'jetpack-premium-analytics-pkg' ), value: 'country', }, + { + label: __( 'Regions', 'jetpack-premium-analytics-pkg' ), + value: 'region', + }, { label: __( 'Cities', 'jetpack-premium-analytics-pkg' ), value: 'city', From 04566ecdac601fa6be4a73514bac3575a5678daf Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Fri, 14 Aug 2026 13:28:11 +1200 Subject: [PATCH 2/6] Address review: type the report-section map and cover the drill-down reset - REPORT_SECTIONS values are typed to the report's own tab ids so a slug typo fails the build; ReportLink takes a bare string and would not. - Fall back to the Countries tab when a persisted layout carries a granularity outside the union. - Test the round trip the reset actually guards: drill into a country, switch to Regions, and come back to Countries listing every country again. - Fix an it.each title whose placeholders read in the opposite order to its tuple. --- .../locations/__tests__/locations.test.tsx | 62 +++++++++++++++++-- .../widgets/locations/render.tsx | 13 +++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx index 7209ddfee05d..cc76976a6810 100644 --- a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx +++ b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx @@ -2,6 +2,7 @@ * External dependencies */ import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import type { AnchorHTMLAttributes, ReactNode } from 'react'; /** * Internal dependencies @@ -38,16 +39,24 @@ jest.mock( '@wordpress/route', () => ( { useSearch: () => ( {} ), } ) ); -const mockUseLocationViews = jest.fn( () => ( { +// Google Charts loads asynchronously and is outside this widget's concern. +jest.mock( '@jetpack-premium-analytics/widgets-toolkit', () => ( { + ...jest.requireActual( '@jetpack-premium-analytics/widgets-toolkit' ), + GeoChart: () =>
, +} ) ); + +const LOADING_STATE = { data: [], - comparisonData: [], hasComparison: false, isLoading: true, isFetching: true, hasData: false, isError: false, isPlaceholderData: false, -} ) ); + refetch: () => {}, +}; + +const mockUseLocationViews = jest.fn( () => LOADING_STATE ); jest.mock( '../use-location-views', () => ( { __esModule: true, @@ -56,7 +65,8 @@ jest.mock( '../use-location-views', () => ( { describe( 'LocationsWidget', () => { beforeEach( () => { - mockUseLocationViews.mockClear(); + mockUseLocationViews.mockReset(); + mockUseLocationViews.mockReturnValue( LOADING_STATE ); } ); it( 'links to the Locations report', () => { @@ -73,7 +83,7 @@ describe( 'LocationsWidget', () => { [ 'country', 'countries' ], [ 'region', 'regions' ], [ 'city', 'cities' ], - ] as const )( 'opens the %s report tab for the %s granularity', ( geoGranularity, section ) => { + ] as const )( 'opens the %s granularity on the %s report tab', ( geoGranularity, section ) => { render( ); expect( screen.getByRole( 'link', { name: 'View all' } ) ).toHaveAttribute( @@ -90,4 +100,46 @@ describe( 'LocationsWidget', () => { expect.objectContaining( { geoMode: 'region', countryFilter: undefined } ) ); } ); + + // Without the reset, the drill-down survives the trip through Regions and + // Countries mode comes back scoped to one country instead of listing them all. + it( 'drops a drilled-down country when switching to Regions', async () => { + mockUseLocationViews.mockReturnValue( { + ...LOADING_STATE, + data: [ + { + key: 'US:United States', + label: 'United States', + countryCode: 'US', + countryFull: 'United States', + value: 10, + region: '', + }, + ], + isLoading: false, + isFetching: false, + hasData: true, + } as unknown as typeof LOADING_STATE ); + + const { rerender } = render( ); + await userEvent.click( + screen.getByRole( 'button', { name: 'View regions in United States' } ) + ); + + expect( mockUseLocationViews ).toHaveBeenLastCalledWith( + expect.objectContaining( { geoMode: 'region', countryFilter: 'US' } ) + ); + + rerender( ); + + expect( mockUseLocationViews ).toHaveBeenLastCalledWith( + expect.objectContaining( { geoMode: 'region', countryFilter: undefined } ) + ); + + rerender( ); + + expect( mockUseLocationViews ).toHaveBeenLastCalledWith( + expect.objectContaining( { geoMode: 'country', countryFilter: undefined } ) + ); + } ); } ); diff --git a/projects/packages/premium-analytics/widgets/locations/render.tsx b/projects/packages/premium-analytics/widgets/locations/render.tsx index 9e4e5644a028..d5e17540811c 100644 --- a/projects/packages/premium-analytics/widgets/locations/render.tsx +++ b/projects/packages/premium-analytics/widgets/locations/render.tsx @@ -64,8 +64,12 @@ const MISSING_MAP_ERROR_MESSAGE = 'Requested map does not exist'; const runtimeUnsupportedProvinceMapCountries = new Set< string >(); type GeoGranularity = NonNullable< LocationsAttributes[ 'geoGranularity' ] >; +// Tab ids owned by the Locations report; `ReportLink` takes a bare string, so +// naming them here is what catches a typo at build time. +type LocationsReportSection = 'countries' | 'regions' | 'cities'; -const REPORT_SECTIONS: Record< GeoGranularity, string > = { +const DEFAULT_REPORT_SECTION: LocationsReportSection = 'countries'; +const REPORT_SECTIONS: Record< GeoGranularity, LocationsReportSection > = { country: 'countries', region: 'regions', city: 'cities', @@ -396,7 +400,12 @@ export default function Locations( { attributes = {} }: LocationsWidgetProps ) {
- +
From 2c0155afe126b29c916fad013f842bf07340afb9 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Fri, 14 Aug 2026 13:43:17 +1200 Subject: [PATCH 3/6] Address review: normalize the stored granularity at the widget boundary The report-link fallback only covered one of the two consumers: the same unvalidated value became the geoMode path segment, so a stale layout would still request stats/location-views/ and render an error under a link that had quietly fallen back to Countries. Normalizing once covers both and lets the fallback go. Also type the test's hook fixture off the hook itself, so a new LocationView field breaks the test rather than being cast away, and assert the map still renders in Regions mode. --- .../widgets/locations/__tests__/locations.test.tsx | 10 ++++++++-- .../premium-analytics/widgets/locations/render.tsx | 14 ++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx index cc76976a6810..b535a0472d14 100644 --- a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx +++ b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx @@ -45,7 +45,11 @@ jest.mock( '@jetpack-premium-analytics/widgets-toolkit', () => ( { GeoChart: () =>
, } ) ); -const LOADING_STATE = { +// Typed off the hook itself so a new field on `LocationView` breaks this +// fixture instead of being silently cast away. +type LocationViewsState = ReturnType< typeof import('../use-location-views').default >; + +const LOADING_STATE: LocationViewsState = { data: [], hasComparison: false, isLoading: true, @@ -119,7 +123,7 @@ describe( 'LocationsWidget', () => { isLoading: false, isFetching: false, hasData: true, - } as unknown as typeof LOADING_STATE ); + } ); const { rerender } = render( ); await userEvent.click( @@ -135,6 +139,8 @@ describe( 'LocationsWidget', () => { expect( mockUseLocationViews ).toHaveBeenLastCalledWith( expect.objectContaining( { geoMode: 'region', countryFilter: undefined } ) ); + // Regions mode keeps the map alongside the leaderboard. + expect( screen.getByTestId( 'geo-chart' ) ).toBeInTheDocument(); rerender( ); diff --git a/projects/packages/premium-analytics/widgets/locations/render.tsx b/projects/packages/premium-analytics/widgets/locations/render.tsx index d5e17540811c..d305e4d221fb 100644 --- a/projects/packages/premium-analytics/widgets/locations/render.tsx +++ b/projects/packages/premium-analytics/widgets/locations/render.tsx @@ -68,7 +68,6 @@ type GeoGranularity = NonNullable< LocationsAttributes[ 'geoGranularity' ] >; // naming them here is what catches a typo at build time. type LocationsReportSection = 'countries' | 'regions' | 'cities'; -const DEFAULT_REPORT_SECTION: LocationsReportSection = 'countries'; const REPORT_SECTIONS: Record< GeoGranularity, LocationsReportSection > = { country: 'countries', region: 'regions', @@ -393,19 +392,18 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { */ export default function Locations( { attributes = {} }: LocationsWidgetProps ) { const max = attributes?.max ?? 10; - const geoGranularity = attributes?.geoGranularity ?? 'country'; + // Attributes are persisted, so a stale layout can carry a granularity this + // widget no longer knows. Normalize once, before it becomes both the endpoint + // path segment and the report tab. + const storedGranularity = attributes?.geoGranularity ?? 'country'; + const geoGranularity = storedGranularity in REPORT_SECTIONS ? storedGranularity : 'country'; return (
- +
From 7776c285ed227f238f0ac598f62b0059e51f5148 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Fri, 14 Aug 2026 13:55:11 +1200 Subject: [PATCH 4/6] Address review: use an own-property check for the stored granularity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `in` also accepts inherited keys, so `toString` would have passed normalization and reached the endpoint as a path segment — the same failure the normalization was added to close. Also correct a fixture comment: the typing protects the row literals passed to mockReturnValue, not the empty-data constant it sat above. --- .../widgets/locations/__tests__/locations.test.tsx | 4 ++-- .../packages/premium-analytics/widgets/locations/render.tsx | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx index b535a0472d14..e11ac0e69cd4 100644 --- a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx +++ b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx @@ -45,8 +45,8 @@ jest.mock( '@jetpack-premium-analytics/widgets-toolkit', () => ( { GeoChart: () =>
, } ) ); -// Typed off the hook itself so a new field on `LocationView` breaks this -// fixture instead of being silently cast away. +// Typed off the hook so the mocked state and the rows passed to +// `mockReturnValue` are type-checked rather than cast away. type LocationViewsState = ReturnType< typeof import('../use-location-views').default >; const LOADING_STATE: LocationViewsState = { diff --git a/projects/packages/premium-analytics/widgets/locations/render.tsx b/projects/packages/premium-analytics/widgets/locations/render.tsx index d305e4d221fb..cf14be282c79 100644 --- a/projects/packages/premium-analytics/widgets/locations/render.tsx +++ b/projects/packages/premium-analytics/widgets/locations/render.tsx @@ -396,7 +396,11 @@ export default function Locations( { attributes = {} }: LocationsWidgetProps ) { // widget no longer knows. Normalize once, before it becomes both the endpoint // path segment and the report tab. const storedGranularity = attributes?.geoGranularity ?? 'country'; - const geoGranularity = storedGranularity in REPORT_SECTIONS ? storedGranularity : 'country'; + // `in` would also accept inherited keys such as `toString`, which would then + // reach the endpoint as a path segment. + const geoGranularity = Object.prototype.hasOwnProperty.call( REPORT_SECTIONS, storedGranularity ) + ? storedGranularity + : 'country'; return ( From 2a45e4f3c91d3810ca42d44463802310c6122314 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Fri, 14 Aug 2026 16:39:09 +1200 Subject: [PATCH 5/6] Add the plugin changelog entry for the Regions option A package-only entry reaches the package CHANGELOG and nowhere else, so a user-visible control gaining an option stays invisible to the people who read the plugin changelog. --- .../changelog/add-wooa7s-1940-locations-regions-view | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view diff --git a/projects/plugins/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view b/projects/plugins/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view new file mode 100644 index 000000000000..02c8e48359a8 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Top locations: add Regions to the "View by" control, listing the top regions worldwide. From 63667650ec715fe29c1ad33d5e9ee05a96d2e2e6 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Tue, 18 Aug 2026 09:24:03 +1200 Subject: [PATCH 6/6] Address review: name the default granularity and derive the country filter from it Also aligns the package changelog prefix with the card name and drops the literal 'undefined' from the granularity test titles. --- .../changelog/add-wooa7s-1940-locations-regions-view | 2 +- .../widgets/locations/__tests__/locations.test.tsx | 6 ++++-- .../premium-analytics/widgets/locations/render.tsx | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view b/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view index aaa5de37b70d..17cc7068bd70 100644 --- a/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view +++ b/projects/packages/premium-analytics/changelog/add-wooa7s-1940-locations-regions-view @@ -1,4 +1,4 @@ Significance: minor Type: added -Locations: offer Regions in the widget's "View by" control. +Top locations: offer Regions in the widget's "View by" control. diff --git a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx index e11ac0e69cd4..27ad7de78475 100644 --- a/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx +++ b/projects/packages/premium-analytics/widgets/locations/__tests__/locations.test.tsx @@ -83,12 +83,14 @@ describe( 'LocationsWidget', () => { } ); it.each( [ - [ undefined, 'countries' ], + [ 'default', 'countries' ], [ 'country', 'countries' ], [ 'region', 'regions' ], [ 'city', 'cities' ], ] as const )( 'opens the %s granularity on the %s report tab', ( geoGranularity, section ) => { - render( ); + render( + + ); expect( screen.getByRole( 'link', { name: 'View all' } ) ).toHaveAttribute( 'href', diff --git a/projects/packages/premium-analytics/widgets/locations/render.tsx b/projects/packages/premium-analytics/widgets/locations/render.tsx index cf14be282c79..490d21d86a99 100644 --- a/projects/packages/premium-analytics/widgets/locations/render.tsx +++ b/projects/packages/premium-analytics/widgets/locations/render.tsx @@ -73,6 +73,7 @@ const REPORT_SECTIONS: Record< GeoGranularity, LocationsReportSection > = { region: 'regions', city: 'cities', }; +const DEFAULT_GEO_GRANULARITY: GeoGranularity = 'country'; function getGeoChartCountryId( countryCode: string ): string { if ( countryCode.toUpperCase() === 'TW' ) { @@ -119,7 +120,7 @@ function LocationsInner( { max, geoGranularity }: LocationsInnerProps ) { reportParams, max, geoMode, - countryFilter: geoMode === 'region' ? activeSelectedCountry?.code : undefined, + countryFilter: activeSelectedCountry?.code, } ); const [ renderLocationState, setRenderLocationState ] = useState< RenderLocationState >( { geoMode, @@ -395,12 +396,12 @@ export default function Locations( { attributes = {} }: LocationsWidgetProps ) { // Attributes are persisted, so a stale layout can carry a granularity this // widget no longer knows. Normalize once, before it becomes both the endpoint // path segment and the report tab. - const storedGranularity = attributes?.geoGranularity ?? 'country'; + const storedGranularity = attributes?.geoGranularity ?? DEFAULT_GEO_GRANULARITY; // `in` would also accept inherited keys such as `toString`, which would then // reach the endpoint as a path segment. const geoGranularity = Object.prototype.hasOwnProperty.call( REPORT_SECTIONS, storedGranularity ) ? storedGranularity - : 'country'; + : DEFAULT_GEO_GRANULARITY; return (