Implement RRM `GET:publications` REST endpoint
Feature Description
The Reader Revenue Manager module should include a GET:publications endpoint that will return all available publications which are available for the current site using the “https://subscribewithgoogle.googleapis.com/v1/publications” SwG API endpoint.
This issue should also be responsible for implementing the Module_With_Service_Entity interface and the check_service_entity_access method in the Modules\Reader_Revenue_Manager class to ensure that the current user has access to the subscribewithgoogle Google service, thus preventing non-owner admins not having access to the publication make any potential changes to the module.
- See relevant section in the design doc.
- See #5671 which was a related effort for the former Thank with Google epic.
- This endpoint will be quite similar (if not the same) to the TwG
GET:publicationsendpoint.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The
GET:publicationsendpoint should be implemented in the Reader Revenue Manager module which should return all available publications which are available for the current site. - The endpoint should call the “https://subscribewithgoogle.googleapis.com/v1/publications” SwG API endpoint to get publication information.
- It should use the
filterparameter of the SwG API endpoint to return publications relevant to the current site only, based on the following criteria:- The current Search Console property ID should be used, with the relevant permutations (
httpandhttpsfor URLs,wwwandnon-wwwin any case).- The
Core\Util\URL::permute_site_url()&Core\Util\URL::permute_site_hosts()methods should be used to include permutations for Punycode and Unicode variations.
- The
- If it is a domain property (
sc-domain:…), the filter parameter should use permutations of the domain followingsc-domain:and be either:domain = "example.com"ORdomain = "www.example.com”
- If it is a URL property (
http://…orhttps://…), the filter parameter should use permutations of the full URL and be either:site_url = "https://example.com/"ORsite_url = "http://example.com/"ORsite_url = "https://www.example.com/"ORsite_url = "http://www.example.com/"
- The current Search Console property ID should be used, with the relevant permutations (
- The Reader Revenue Manager module class (being implemented in #8785) should implement the
Module_With_Service_Entityinterface and thecheck_service_entity_accessmethod to ensure that the current user has access to thesubscribewithgoogleGoogle service, thus preventing non-owner admins not having access to the publication make any potential changes to the module.
Implementation Brief
Note: This is the same REST endpoint that was implemented in the former Thank with Google module, which can be looked at by going back into Git history here.
Make the following additions to the Modules/Reader_Revenue_Manager class introduced via https://github.com/google/site-kit-wp/issues/8785.
- [x] Implement the
setup_services()method, the returned array should include asubscribewithgoogleservice which is an instance of theGoogle_Service_SubscribewithGoogleclass introduced via #8783. - [x] Ensure the class implements the
Module_With_Service_Entityinterface.- In the
check_service_entity_access()implementation, make a$subscribewithgoogle->publications->listPublications()request with apageSizeof1. - If this succeeds, return
true. - Follow the example of
Analytics_4::check_service_entity_access()to ensure the method returnsfalseif a403is returned, and maps any other exception to an error.
- In the
- [x] Implement
get_datapoint_definitions()to return an array containing the definition ofGET:publications, which should depend on thesubscribewithgoogleservice. - [x] Implement
create_data_request(). This should make a$subscribewithgoogle->publications->listPublications()request with thefilteroption set as per the AC. See the oldThank_With_Googleimplementation for how to do this. - [x] Implement
parse_data_response()to return$response->getPublications()as an indexed array.
Test Coverage
- Add PHPUnit test coverage for the service entity access and the new endpoint.
Create account in publisher center
- Go to https://publishercenter.google.com/ login/register using google account.
- It will open a the new modal to add publication details as below.
- Once successfully created, it will create an publication ID, which should be seen in the URL as query parameter. Example
https://publishercenter.google.com/?publication=CAow6J6vDA
QA Brief
- Turn on the
rrmModulefeature flag. - Go to Site Kit settings and activate the Reader Revenue Manager module.
- In the Site Kit dashboard, run the following command in browser console. Make sure that request succeeds with 200 response and returns an array of publications that your Google account has at the publisher center (https://publishercenter.google.com/) that are associated with the current site URL.
- You may create new publication(s) with the current site URL to see this array populated.
googlesitekit.api.get( 'modules', 'reader-revenue-manager', 'publications', undefined, {
useCache: false,
} )
Changelog entry
- Add Reader Revenue Manager API functionality to retrieve publications.
AC :white_check_mark:
IB ✅
QA Update ⚠️
Hi @ankitrox , I've activated the rrm flag and module and ran the script. I hit an error when I ran the script in Browser console. The details section shows the error. I am sending you the credentials to my test site as well for your verification.
googlesitekit.api.get( 'modules', 'reader-revenue-manager', 'publications', undefined, {
useCache: false,
} )
@kelvinballoo As per this thread and conversation on slack, you will have to run this code to provide the necessary permission for endpoint to work.
googlesitekit.data.dispatch( 'core/user' ).setPermissionScopeError( {
code: 'missing_required_scopes',
data: {
status: 403,
scopes: [ 'https://www.googleapis.com/auth/subscribewithgoogle.publications.readonly' ],
skipModal: true,
skipDefaultErrorNotifications: true,
redirectURL: location.href,
},
} );
Please run this code in browser console and then the given snippet in QAB.
Can you please test this and let me know if it works?
Thanks
QA Update ⚠️
Hi @ankitrox , I did the above and here are the outcomes:
-
I executed the script you mentioned and I would routed to grant some access. After that, I executed the QAB script. It executed fine with status 200 but there was no details returned about the publication. The array was [0]
Array is [0]Status is 200 (though the response tab is blank:
-
Checking on publisher central, I do have the publication with the correct URL and it's verified as well
Let me know if I missed any step. Happy to jump on a call to clarify anything if that's faster.
@kelvinballoo As per our conversation over slack DM, you are getting some error during Site Kit setup.
However; when I request the API, I am getting the response as shown in screenshot attached. can you please make sure these steps are completed in RRM in publisher centre?
QA Update ✅
Tested this and it's looking good:
-
After running the QAB script in browser console, the publication request succeeds with 200 response and returns an array of publications that my Google account has at the publisher center (https://publishercenter.google.com/) that are associated with the current site URL.
-
The publications returned matched the ones in Publisher Central
-
I've created 3 publications and all of the 3 are pulled in as long as the URL matches with my site URL
-
I've created 1 publication with a wrong URL to ensure it's not being picked up and it was as expected
Moving this ticket to Approval.
Note: It's important to check done for 'Add code to your site' in Publisher central for the publications to be pulled in.