Skip to content

Commit

Permalink
Odyssey Stats: Add API support for Location stats (#40852)
Browse files Browse the repository at this point in the history
* Add API support for Location views stats

* changelog
  • Loading branch information
Initsogar authored Jan 6, 2025
1 parent 38f7c63 commit ebd9c30
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Stats: added API support for location stats
25 changes: 25 additions & 0 deletions projects/packages/stats-admin/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ public function register_rest_routes() {
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

// Get Location stats.
register_rest_route(
static::$namespace,
sprintf( '/sites/%d/stats/location-views/(?P<geo_mode>country|region|city)', Jetpack_Options::get_option( 'id' ) ),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_location_stats' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);
}

/**
Expand Down Expand Up @@ -974,6 +985,20 @@ public function get_devices_stats_time_series( $req ) {
);
}

/**
* Get Location stats.
*
* @param WP_REST_Request $req The request object.
* @return array
*/
public function get_location_stats( $req ) {
$params = $req->get_params();
$geo_mode = $params['geo_mode'];
unset( $params['geo_mode'] );

return $this->wpcom_stats->get_views_by_location( $geo_mode, $params );
}

/**
* Dismiss or delay stats notices.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Stats: added API support for location stats
13 changes: 13 additions & 0 deletions projects/packages/stats/src/class-wpcom-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ public function get_views_by_country( $args = array() ) {
return $this->fetch_stats( $args );
}

/**
* Get site's views by location.
*
* @param string $geo_mode The type of location to fetch views for (country, region, city).
* @param array $args Optional query parameters.
* @return array|WP_Error
*/
public function get_views_by_location( $geo_mode, $args = array() ) {
$this->resource = sprintf( 'location-views/%s', $geo_mode );

return $this->fetch_stats( $args );
}

/**
* Get site's followers.
*
Expand Down

0 comments on commit ebd9c30

Please sign in to comment.