site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Implement RRM `GET:publications` REST endpoint

Open nfmohit opened this issue 1 year ago • 2 comments

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.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The GET:publications endpoint 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 filter parameter 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 (http and https for URLs, www and non-www in 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.
    • If it is a domain property (sc-domain:…), the filter parameter should use permutations of the domain following sc-domain: and be either:
      • domain = "example.com" OR
      • domain = "www.example.com”
    • If it is a URL property (http://… or https://…), the filter parameter should use permutations of the full URL and be either:
      • site_url = "https://example.com/" OR
      • site_url = "http://example.com/" OR
      • site_url = "https://www.example.com/" OR
      • site_url = "http://www.example.com/"
  • The Reader Revenue Manager module class (being implemented in #8785) should implement the Module_With_Service_Entity interface and the check_service_entity_access method 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.

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 a subscribewithgoogle service which is an instance of the Google_Service_SubscribewithGoogle class introduced via #8783.
  • [x] Ensure the class implements the Module_With_Service_Entity interface.
    • In the check_service_entity_access() implementation, make a $subscribewithgoogle->publications->listPublications() request with a pageSize of 1.
    • If this succeeds, return true.
    • Follow the example of Analytics_4::check_service_entity_access() to ensure the method returns false if a 403 is returned, and maps any other exception to an error.
  • [x] Implement get_datapoint_definitions() to return an array containing the definition of GET:publications, which should depend on the subscribewithgoogle service.
  • [x] Implement create_data_request(). This should make a $subscribewithgoogle->publications->listPublications() request with the filter option set as per the AC. See the old Thank_With_Google implementation 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.
Screenshot 2024-07-05 at 5 26 39 PM
  • 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 rrmModule feature 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.

nfmohit avatar Jun 04 '24 02:06 nfmohit

AC :white_check_mark:

kuasha420 avatar Jun 18 '24 09:06 kuasha420

IB ✅

nfmohit avatar Jun 20 '24 12:06 nfmohit

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,
} )
Screenshot 2024-07-08 at 17 55 34

kelvinballoo avatar Jul 08 '24 13:07 kelvinballoo

@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

ankitrox avatar Jul 08 '24 17:07 ankitrox

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] Screenshot 2024-07-09 at 13 41 56

    Status is 200 (though the response tab is blank: Screenshot 2024-07-09 at 13 42 32 Screenshot 2024-07-09 at 13 42 40

  • Checking on publisher central, I do have the publication with the correct URL and it's verified as well

    Screenshot 2024-07-09 at 13 42 15

Let me know if I missed any step. Happy to jump on a call to clarify anything if that's faster.

kelvinballoo avatar Jul 09 '24 09:07 kelvinballoo

Screenshot 2024-07-09 at 5.59.55 PM.png Screenshot 2024-07-09 at 6.01.08 PM.png

@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?

ankitrox avatar Jul 09 '24 16:07 ankitrox

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.

    Screenshot 2024-07-10 at 12 47 47
  • 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.

Screenshot 2024-07-10 at 12 56 06

kelvinballoo avatar Jul 10 '24 08:07 kelvinballoo