Skip to content

Commit 9315a27

Browse files
kangzjdognose24
andauthored
Odyssey Stats: Add API support for Email Stats (#35703)
* clear cache on purchase or with force_refresh get params * add email summary endpoint * add api for email stats * fix up project versions * revert unnecessary change * revert unnecessary change * revert unnecessary change * changelog * Update annotations --------- Co-authored-by: Dognose <[email protected]>
1 parent a2392b0 commit 9315a27

File tree

7 files changed

+173
-5
lines changed

7 files changed

+173
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Stats: added support for Email stats

projects/packages/stats-admin/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"autotagger": true,
5353
"mirror-repo": "Automattic/jetpack-stats-admin",
5454
"branch-alias": {
55-
"dev-trunk": "0.15.x-dev"
55+
"dev-trunk": "0.16.x-dev"
5656
},
5757
"textdomain": "jetpack-stats-admin",
5858
"version-constants": {

projects/packages/stats-admin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@automattic/jetpack-stats-admin",
4-
"version": "0.15.3",
4+
"version": "0.16.0-alpha",
55
"description": "Stats Dashboard",
66
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/stats-admin/#readme",
77
"bugs": {

projects/packages/stats-admin/src/class-main.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Main {
2222
/**
2323
* Stats version.
2424
*/
25-
const VERSION = '0.15.3';
25+
const VERSION = '0.16.0-alpha';
2626

2727
/**
2828
* Singleton Main instance.

projects/packages/stats-admin/src/class-rest-controller.php

+160
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,50 @@ public function register_rest_routes() {
323323
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
324324
)
325325
);
326+
327+
// Get email stats as a list.
328+
register_rest_route(
329+
static::$namespace,
330+
sprintf( '/sites/%d/stats/emails/(?P<resource>[\-\w\d]+)', Jetpack_Options::get_option( 'id' ) ),
331+
array(
332+
'methods' => WP_REST_Server::READABLE,
333+
'callback' => array( $this, 'get_email_stats_list' ),
334+
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
335+
)
336+
);
337+
338+
// Get Email opens stats for a single post.
339+
register_rest_route(
340+
static::$namespace,
341+
sprintf( '/sites/%d/stats/opens/emails/(?P<post_id>[\d]+)/(?P<resource>[\-\w]+)', Jetpack_Options::get_option( 'id' ) ),
342+
array(
343+
'methods' => WP_REST_Server::READABLE,
344+
'callback' => array( $this, 'get_email_opens_stats_single' ),
345+
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
346+
)
347+
);
348+
349+
// Get Email clicks stats for a single post.
350+
register_rest_route(
351+
static::$namespace,
352+
sprintf( '/sites/%d/stats/clicks/emails/(?P<post_id>[\d]+)/(?P<resource>[\-\w]+)', Jetpack_Options::get_option( 'id' ) ),
353+
array(
354+
'methods' => WP_REST_Server::READABLE,
355+
'callback' => array( $this, 'get_email_clicks_stats_single' ),
356+
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
357+
)
358+
);
359+
360+
// Get Email stats time series.
361+
register_rest_route(
362+
static::$namespace,
363+
sprintf( '/sites/%d/stats/(?P<resource>[\-\w]+)/emails/(?P<post_id>[\d]+)', Jetpack_Options::get_option( 'id' ) ),
364+
array(
365+
'methods' => WP_REST_Server::READABLE,
366+
'callback' => array( $this, 'get_email_stats_time_series' ),
367+
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
368+
)
369+
);
326370
}
327371

328372
/**
@@ -666,6 +710,122 @@ public function get_wordads_stats( $req ) {
666710
);
667711
}
668712

713+
/**
714+
* Get Email stats as a list.
715+
*
716+
* @param WP_REST_Request $req The request object.
717+
* @return array
718+
*/
719+
public function get_email_stats_list( $req ) {
720+
switch ( $req->get_param( 'resource' ) ) {
721+
case 'summary':
722+
return WPCOM_Client::request_as_blog_cached(
723+
sprintf(
724+
'/sites/%d/stats/emails/%s?%s',
725+
Jetpack_Options::get_option( 'id' ),
726+
$req->get_param( 'resource' ),
727+
$this->filter_and_build_query_string(
728+
$req->get_params()
729+
)
730+
),
731+
'v1.1',
732+
array( 'timeout' => 5 )
733+
);
734+
default:
735+
return $this->get_forbidden_error();
736+
}
737+
}
738+
739+
/**
740+
* Get Email opens stats for a single post.
741+
*
742+
* @param WP_REST_Request $req The request object.
743+
* @return array
744+
*/
745+
public function get_email_opens_stats_single( $req ) {
746+
switch ( $req->get_param( 'resource' ) ) {
747+
case 'client':
748+
case 'device':
749+
case 'country':
750+
case 'rate':
751+
return WPCOM_Client::request_as_blog_cached(
752+
sprintf(
753+
'/sites/%d/stats/opens/emails/%d/%s?%s',
754+
Jetpack_Options::get_option( 'id' ),
755+
$req->get_param( 'post_id' ),
756+
$req->get_param( 'resource' ),
757+
$this->filter_and_build_query_string(
758+
$req->get_params()
759+
)
760+
),
761+
'v1.1',
762+
array( 'timeout' => 5 )
763+
);
764+
default:
765+
return $this->get_forbidden_error();
766+
}
767+
}
768+
769+
/**
770+
* Get Email clicks stats for a single post.
771+
*
772+
* @param WP_REST_Request $req The request object.
773+
* @return array
774+
*/
775+
public function get_email_clicks_stats_single( $req ) {
776+
switch ( $req->get_param( 'resource' ) ) {
777+
case 'client':
778+
case 'device':
779+
case 'country':
780+
case 'rate':
781+
case 'link':
782+
case 'user-content-link':
783+
return WPCOM_Client::request_as_blog_cached(
784+
sprintf(
785+
'/sites/%d/stats/clicks/emails/%d/%s?%s',
786+
Jetpack_Options::get_option( 'id' ),
787+
$req->get_param( 'post_id' ),
788+
$req->get_param( 'resource' ),
789+
$this->filter_and_build_query_string(
790+
$req->get_params()
791+
)
792+
),
793+
'v1.1',
794+
array( 'timeout' => 5 )
795+
);
796+
default:
797+
return $this->get_forbidden_error();
798+
}
799+
}
800+
801+
/**
802+
* Get Email stats time series.
803+
*
804+
* @param WP_REST_Request $req The request object.
805+
* @return array
806+
*/
807+
public function get_email_stats_time_series( $req ) {
808+
switch ( $req->get_param( 'resource' ) ) {
809+
case 'opens':
810+
case 'clicks':
811+
return WPCOM_Client::request_as_blog_cached(
812+
sprintf(
813+
'/sites/%d/stats/%s/emails/%d?%s',
814+
Jetpack_Options::get_option( 'id' ),
815+
$req->get_param( 'resource' ),
816+
$req->get_param( 'post_id' ),
817+
$this->filter_and_build_query_string(
818+
$req->get_params()
819+
)
820+
),
821+
'v1.1',
822+
array( 'timeout' => 5 )
823+
);
824+
default:
825+
return $this->get_forbidden_error();
826+
}
827+
}
828+
669829
/**
670830
* Dismiss or delay stats notices.
671831
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: enhancement
3+
4+
Add support for Email stats

projects/plugins/jetpack/composer.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)