Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: add Geochart regions/cities #98055

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const OPTION_KEYS = {
CITIES: 'cities',
};

type StatQueryType = 'country' | 'region' | 'city';
type GeoMode = 'country' | 'region' | 'city';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name to avoid confusion between statType and statQueryType. Geomode makes a bit more sense and it's the name of the argument of useLocationViewsQuery().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense :)


const STAT_QUERY_TYPES: Record< string, StatQueryType > = {
const GEO_MODES: Record< string, GeoMode > = {
[ OPTION_KEYS.COUNTRIES ]: 'country',
[ OPTION_KEYS.REGIONS ]: 'region',
[ OPTION_KEYS.CITIES ]: 'city',
Expand Down Expand Up @@ -78,13 +78,13 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
},
};

const statQueryType = STAT_QUERY_TYPES[ selectedOption ];
const geoMode = GEO_MODES[ selectedOption ];

const {
data = [],
isLoading: isRequestingData,
isError,
} = useLocationViewsQuery< StatsLocationViewsData >( siteId, statQueryType, query );
} = useLocationViewsQuery< StatsLocationViewsData >( siteId, geoMode, query );

const changeViewButton = ( selection: SelectOptionType ) => {
const filter = selection.value;
Expand Down Expand Up @@ -174,7 +174,7 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
metricLabel={ translate( 'Visitors' ) }
loader={ isRequestingData && <StatsModulePlaceholder isLoading={ isRequestingData } /> }
splitHeader
heroElement={ <Geochart query={ query } skipQuery /> }
heroElement={ <Geochart data={ data } geoMode={ geoMode } skipQuery /> }
mainItemLabel={ optionLabels[ selectedOption ]?.headerLabel }
toggleControl={ toggleControlComponent }
showMore={
Expand Down
34 changes: 28 additions & 6 deletions client/my-sites/stats/geochart/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class StatsGeochart extends Component {
statType: PropTypes.string,
query: PropTypes.object,
data: PropTypes.array,
geoMode: PropTypes.string,
kind: PropTypes.string,
postId: PropTypes.number,
skipQuery: PropTypes.bool,
Expand All @@ -33,6 +34,7 @@ class StatsGeochart extends Component {
};

static defaultProps = {
geoMode: 'country',
kind: 'site',
numberLabel: '',
};
Expand Down Expand Up @@ -103,18 +105,23 @@ class StatsGeochart extends Component {
};

drawData = () => {
const { currentUserCountryCode, data, translate, numberLabel } = this.props;
const { currentUserCountryCode, data, geoMode, translate, numberLabel } = this.props;
if ( ! data || ! data.length ) {
return;
}

const mapData = map( data, ( country ) => {
const mapData = map( data, ( location ) => {
let code = location.countryCode;
if ( geoMode !== 'country' ) {
code = `${ location.countryCode } ${ location.label }`;
}

return [
{
v: country.countryCode,
f: country.label,
v: code,
f: location.label,
},
country.value,
location.value,
];
} );

Expand All @@ -141,6 +148,10 @@ class StatsGeochart extends Component {
domain: currentUserCountryCode,
};

if ( geoMode !== 'country' ) {
options.displayMode = 'markers';
}

const regions = [ ...new Set( map( data, 'region' ) ) ];

if ( 1 === regions.length ) {
Expand Down Expand Up @@ -196,8 +207,19 @@ class StatsGeochart extends Component {
export default connect( ( state, ownProps ) => {
const siteId = getSelectedSiteId( state );
const statType = ownProps.statType ?? 'statsCountryViews';
const currentUserCountryCode = getCurrentUserCountryCode( state );
const { postId, query, kind } = ownProps;

// Skip data fetching if it was explicitly passed as a prop
if ( ownProps.data ) {
return {
currentUserCountryCode,
data: ownProps.data,
siteId,
statType,
};
}

const data =
kind === 'email'
? getEmailStatsNormalizedData(
Expand All @@ -212,7 +234,7 @@ export default connect( ( state, ownProps ) => {
: getSiteStatsNormalizedData( state, siteId, statType, query );

return {
currentUserCountryCode: getCurrentUserCountryCode( state ),
currentUserCountryCode,
data,
siteId,
statType,
Expand Down
Loading