Implement User Surveys REST Controller
See original GitHub issueFeature Description
In #3355, we defined a number of actions that rely on REST endpoints that do not yet exist. This issue is for adding the necessary handling for those on the server side.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- A
Google\Site_Kit\Core\User_Surveys\REST_User_Surveys_Controllerclass should be implemented, similar to theREST_Feature_Tours_Controller- Dependencies should be provided as needed via the constructor
- Should have a test case added for it (see
REST_Feature_Tours_ControllerTest)
- It should register the following REST routes and corresponding remote endpoints on the Site Kit service:
POST:core/user/data/survey-trigger- Requires a
triggerIDas a non-empty string request parameter - Forwards request to the Site Kit service as
POST:/survey/trigger/with JSON-encoded body:site_id: Current site ID.site_secret: Current site secret.access_token: Current user’s access token.trigger_context: An object containing the following fields:trigger_id: The trigger ID, as received from the client.language: Current user’s WP locale
- Response should be returned back to the client
- Requires a
POST:core/user/data/survey-event- Requires an
eventobject passed from the client - Requires a
sessionobject passed from the client withsession_idandsession_tokenfields - Forwards request to the Site Kit service as
POST:/survey/event/with JSON-encoded body:site_id: Current site ID.site_secret: Current site secret.access_token: Current user’s access token.session: Object withsession_idandsession_tokenfields received from the clientevent: Object with data passed from the client
- Response should be returned back to the client
- Requires an
- All routes must require the user to be authenticated
- All routes require that the site is connected via the proxy
Implementation Brief
Google_Proxy class
- Add a new public method
send_survey_trigger( $credentials, $access_token, $trigger_id ).- It should call the
Google_Proxy::requestmethod passing/survey/trigger/as the first argument, the incoming$credentialsobject as the second argument, and an array with the following fields as the third argument:'access_token' => $access_token`, 'json_request' => true, 'body' => array( 'trigger_context' => array( 'trigger_id' => $trigger_id, 'language' => ... // current user locale ), ), - Return results of the
requestmethod call without modifications.
- It should call the
- Add a new public method
send_survey_event( $credentials, $access_token, $session, $event ).- It should follow the logic from the
send_survey_triggermethod but use/survey/event/as the first parameter and the following array as thebodyproperty of the array passed as the third parameter:'session' => $session, 'event' => $event,
- It should follow the logic from the
REST_User_Surveys_Controller class
- Create a new class
REST_User_Surveys_Controllerin theincludes/Core/User_Surveysfolder. UseREST_Feature_Tours_Controlleras an example in terms of registering routes, the methods to implement, etc.- Add a public constructor that accepts only the
Authentication $authenticationargument. - Add a new protected method
get_rest_routesthat returns an array ofREST_Routeobjects for the endpoints defined in the AC. Each REST route should do the following:- Verify that the current user is authenticated (using
$authentication->is_authenticated()) and proxy is used (using$authentication->credentials()->using_proxy()) in thepermission_callbackproperty. - Define the
argsproperty with the expected schema for the incoming arguments. See how we did it for the user input settings: https://github.com/google/site-kit-wp/blob/43dd2936086ea4b9bda71d038abb4b343fe981e5/includes/Core/REST_API/REST_Routes.php#L318-L348 - Call the appropriate method of the
Google_Proxyclass (defined above) to send a request to the proxy server and return a response from it.- Use
Google_Proxyclass instance by calling$authentication->get_google_proxy()to get the instance of theGoogle_Proxyclass. - Use
$authentication->get_oauth_client()->get_access_token()to get the access token. - Use
$authentication->credentials()to get the credentials object.
- Use
- Verify that the current user is authenticated (using
- Add a new public method
registerthat adds a new hook for thegooglesitekit_rest_routesfilter. The hook should merge the incoming $routes with the REST routes returned by theget_rest_routesmethod. See how it’s done in the feature tours controllers: https://github.com/google/site-kit-wp/blob/43dd2936086ea4b9bda71d038abb4b343fe981e5/includes/Core/Feature_Tours/REST_Feature_Tours_Controller.php#L76-L81
- Add a public constructor that accepts only the
- Register the
REST_User_Surveys_Controllerclass in the hook for theinitaction defined in thePlugin::registermethod. You can do it like this: https://github.com/google/site-kit-wp/blob/43dd2936086ea4b9bda71d038abb4b343fe981e5/includes/Plugin.php#L191
Test Coverage
- Create new phpunit tests for the
REST_User_Surveys_Controllerclass. SeeREST_Feature_Tours_ControllerTestfor inspiration on how to structure this file.- Ensure the ACs are met in these unit tests.
Visual Regression Changes
- N/A
QA Brief
- Check the
Google_Proxyclass and verify that it has two new public methods:send_survey_triggerandsend_survey_event. Those methods should satisfy requirements defined for forwarded requests to the proxy server defined in the AC. - Check the
REST_User_Surveys_Controllerclass and verify that it registers two endpoints:POST:core/user/data/survey-triggerandPOST:core/user/data/survey-event. Also, validate that those endpoints satisfy requirements defined in the AC and call the appropriate methods from theGoogle_Proxyclass.
Changelog entry
- Add REST routes for user survey endpoints.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
How to Build Data-Driven Surveys With React + REST API ...
We'll use SurveyJS' feature of populating choices from a RESTful service,. Utilizing two REST APIs, the RESTCountries API, and TheMealDB API ...
Read more >7 APIs for Surveys
1. Alchemer API. Alchemer provides enterprise online survey tools. · 2. Survicate API. Survicate is a Platform for collecting customer insights.
Read more >REST API
SurveyLab allows you to access your data with the REST API interface. Access to API is ... In order to use the API,...
Read more >REST API Examples
In this use-case example we'll cover how to create a campaign, add contacts, an... ... Looking to create an Image Heatmap question via...
Read more >How to use our RESTful API
Use our API to integrate your apps, automate things, give the data extra meaning, or preform smart data-based actions & make your business...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@aaemnnosttv @eugene-manuilov I’ve updated the ACs here to also require passing the
site_secretto the proxy - originally it only required passingsite_id. Looking at the PR #3440, this is already the case anyway (since the regularGoogle_Proxymethod for sending a request does just that), so this doesn’t require any additional work on the plugin side. I was gonna open a new issue, but it looks like that’s not needed.@eugene-manuilov – yes, correct. I’ve updated it in the ACs, thanks for checking!
@tofumatt I agree although I’m not sure if this was previously discussed (@eugene-manuilov?). My guess is that it was made private by default since we didn’t have a use for it outside of that class yet.
@eugene-manuilov if the IB needs further iteration how do you feel about taking over and then assigning to me for review? The IB is looking pretty close at first glance, but as @tofumatt said the issue is a bit timely and it’s important we move this forward today.