Send Idea Hub activities on certain Idea Hub post interactions
The Idea Hub API offers a platforms.properties.ideaActivities.create endpoint which expects information about activities related to an idea and content creation. The API is currently being finalized, so this issue is pending that work - ACs will be added as soon as possible.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The Google API client services library should be updated to at least a version so that it includes the latest state of the Idea Hub API
platforms.properties.ideaActivities.createendpoint. This work may overlap with #3916, pending whether the Beta update comes in the same release as the API enhancements needed here. - For the following Idea Hub interactions, a POST request to the above endpoint (
https://ideahub.googleapis.com/v1alpha/{parent=platforms/*/properties/*}/ideaActivities) should be sent using the respective Google API client services method (the class will likely bePlatformsPropertiesIdeaActivitiesand the method will likely becreate; aparentvalue will need to be specified similar to the ideas endpoint):- When an idea draft post is created, the request should include the respective idea ID and the
POST_DRAFTEDactivity. - When an idea post is published, the request should include the respective idea ID and the
POST_PUBLISHEDactivity, including the published post URL. - When an idea post is published, the request should include the respective idea ID and the
POST_UNPUBLISHEDactivity. - When an idea post is deleted, the request should include the respective idea ID and the
POST_DELETEDactivity.
- When an idea draft post is created, the request should include the respective idea ID and the
- In all cases, the data to send in the API request needs to be in the following shape:
{
"ideas": [
"Idea ID goes in here (not the full idea name, just the ID part)"
],
"topics": [],
"type": "one of POST_DRAFTED, POST_PUBLISHED, POST_UNPUBLISHED, POST_DELETED",
"uri": "public post URL if available (depends on the activity type, e.g. a draft post doesn't have a public URL)"
}
Implementation Brief
- Upgrade the
google/apiclient-servicesdependency to >=0.210 version.- Verify functionality of existing modules to make sure nothing is broken after the upgrade.
- Edit the
Idea_Hubclass:- Create a new private method
track_idea_activity( $post_id, $type ):- Get the associated idea name. Return early if the post doesn't have the associated idea.
- Create a new instance of the
GoogleSearchIdeahubV1alphaIdeaActivityclass.- Set ideas (using
setIdeasmethod) passing an array with the idea ID extracted from the post idea name. - Set idea topics (using
setTopicsmethod) passing an empty array. - Set the activity type (using
setTypemethod) passing the incoming$typevariable. - If the post is published, set the post URI (using
setUrimethod).
- Set ideas (using
- Get the parent slug using the
$this->get_parent_slug()method. - Create an activity entry by calling
$this->get_service( 'ideahub' )->platforms_properties_ideaActivities->create( $parent, $activity )method.
- Update the
on_idea_hub_post_status_transitionmethod to call thetrack_idea_activitymethod with:POST_PUBLISHEDtype if the new status ispublishedand it doesn't equal the old status.POST_UNPUBLISHEDtype if the old status ispublishedand it doens't equal the new status.
- Update the
POST:create-idea-draft-postcase in thecreate_data_requestmethod to call thetrack_idea_activitymethod with thePOST_DRAFTEDtype. - Add a new hook for the
pre_delete_postaction in theregistermethod (if the module is connected) that calls thetrack_idea_activitymethod with thePOST_DELETEDtype.
- Create a new private method
Test Coverage
- N/A
Visual Regression Changes
- N/A
QA Brief
- Enable errors logging on your local machine using WP_DEBUG and WP_DEBUG_LOG constants.
- Update the following lines to save the response in the debug.log file: https://github.com/google/site-kit-wp/blob/48e8fe4b54b7990cb243ad66d39506957222dc4c/includes/Modules/Idea_Hub.php#L1061-L1063
You can do it something like this:
$response = $this->get_service( 'ideahub' ) ->platforms_properties_ideaActivities ->create( $parent, $activity ); error_log( var_export( $response, true ) ); - Now repeat interactions defined in AC and verify that you see the correct responses in your debug.log file.
Changelog entry
- Use Idea Hub activities endpoint when drafting, publishing or deleting a post.
@eclarke1 @fhollis This issue and #3916 are the final launch blockers for Idea Hub. However, they're both pending some final API work. On this one, we can start writing an IB yet, but not the actual implementation. I'll update the two as soon as we can proceed.
@felixarntz do you mind reviewing IB?
@eugene-manuilov IB mostly lgtm, a few things though.
Set idea topics (using
setTopicsmethod) passing an array of topic IDs (midproperty) extracted from the post idea topics array.
We don't need to set topics, as they are inferred from the idea according to the API team. That's why I explicitly did not include setting topics in the ACs. They should always be an empty array.
If the post is published, set the post URI (using
setUrimethod).
Just double-checking on the timing here, we need to pay extra attention to that: Is the permalink already set/available at the exact point of transitioning the post status to publish? I'm sure we can somehow get it if not, but I wanna make sure we consider a potential complexity here.
Upgrade the
google/apiclient-servicesdependency to thev0.210.0version if #3916 is not implemented yet.
Please leave the API version as "TBD". The problem is that, while there already is a version of that endpoint in there, it currently still has a problem we have identified, as it requires the url parameter to be specified in each case, which is not possible for all activities. The API team is releasing the beta first and then adding the fixed activities endpoint to it, and that is probably what we should be waiting for with the implementation.
Due to the above, can you please add some very obvious note on top of the IB (maybe bold) that the execution is blocked until further notice? I'll provide an update once this can proceed.
We don't need to set topics, as they are inferred from the idea according to the API team. That's why I explicitly did not include setting topics in the ACs. They should always be an empty array.
:+1: Updated.
Just double-checking on the timing here, we need to pay extra attention to that: Is the permalink already set/available at the exact point of transitioning the post status to publish? I'm sure we can somehow get it if not, but I wanna make sure we consider a potential complexity here.
Yes, we can get it because the transition_post_status action is executed after the post is inserted/updated and after the cache for the post is regenerated.
Please leave the API version as "TBD". The problem is that, while there already is a version of that endpoint in there, it currently still has a problem we have identified, as it requires the url parameter to be specified in each case, which is not possible for all activities. The API team is releasing the beta first and then adding the fixed activities endpoint to it, and that is probably what we should be waiting for with the implementation.
Updated. I see your point, but maybe we can start working on this ticket using the available version anyway? The v0.210.0 defines the required API, so nothing blocks us from implementing it. We will prepare a PR for this ticket and will keep it open until the correct version is added. What do you think?
Thanks @eugene-manuilov - IB ✅
You make a good point, and in fact the API team has now clarified that they would like us to implement this functionality using their alpha API. They said they've made the necessary change, but we should test it before they will roll it into the Beta version of their API (since at that point breaking changes will be almost impossible to make). You should be able to do this with 0.210. Would be great to get this started sooner than later, to be able to flag early if something on the API side doesn't work as expected here. I've made a super tiny change to the IB just to reflect that (and remove the note that it is blocked that I asked you to add 😄).
I will mark #3916 as blocked by this now, since we should only update to the Beta API once the activities functionality is included there as well.
@felixarntz the activity API seems to work well but it always requires URI to be set even for non-publish activities:
{
"error": {
"code": 400,
"message": "Uri is not subpath of creator uri",
"errors": [
{
"message": "Uri is not subpath of creator uri",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}
@eugene-manuilov I've pointed that out to them already a couple days ago that requiring the URL for every activity doesn't work. They already have a fix underway, but not sure when it will be ready. It shouldn't require a client update on our end.
Maybe try again in 1-2 days if the fix is out, or you'd need to check with them. cc @aaemnnosttv
@eugene-manuilov Hi, posting on behalf of API team here, we've just had a push to prod and the URI should no longer be required.
Thanks for letting us know, @rzhw! Looks like you also released the v1beta version too. One thing that I noticed is that the Idea Activities class had been removed from the IdeaHub class: https://github.com/googleapis/google-api-php-client-services/pull/475/files#diff-05abc0e3ab8d06b7d38dfd262646f2ee61cdd929761d421888bef08e3f69432dL97-L116. Was it done intentionally?
@eugene-manuilov Yep that was intentional as we want v1beta to only contain components expected to be as stable as possible. We'll be waiting for the thumbs up from your side—once we consider Idea Activities stable enough it will be included in v1beta.
@rzhw I can confirm that the existing functionality in v0.210.0 works correctly on my local machine. I get correct responses for all activities and it doesn't require URI for non-published posts anymore.
@eugene-manuilov Great to hear, thanks for letting us know!
@eugene-manuilov Can you please add a QA Brief?
QAB is added.
QA ⚠️
@eugene-manuilov I think I need some further guidance on this. I've set up the WP_DEBUG and WP_DEBUG_LOG constants, and am seeing output in my log file.
Publishing a post ❓
- Publishing an Idea post doesn't seem to log anything.
Deleting a post ❓
- Going to
/wp-admin/edit.php?post_type=postand trashing a post (draft or published) seems to output the following (notePOST_UNPUBLISHED):
array(
'collection_key' => 'topics',
'ideas' =>
array (
0 => 'ideas/8164307130530350304',
),
'name' => 'platforms/sitekit/properties/https%3A%2F%2Foi.ie/ideaActivities/2qpcl39fmp2m8',
'topics' => NULL,
'type' => 'POST_UNPUBLISHED',
'uri' => NULL,
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
),
'processed' =>
array (
),
))
Creating a draft ✅
Output:
array(
'collection_key' => 'topics',
'ideas' =>
array (
0 => 'ideas/17821284747959312641',
),
'name' => 'platforms/sitekit/properties/https%3A%2F%2Foi.ie/ideaActivities/6chhvnr2osfi8',
'topics' => NULL,
'type' => 'POST_DRAFTED',
'uri' => NULL,
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
),
'processed' =>
array (
),
))
Unpublishing a post ✅
array(
'collection_key' => 'topics',
'ideas' =>
array (
0 => 'ideas/10051498905276705731',
),
'name' => 'platforms/sitekit/properties/https%3A%2F%2Foi.ie/ideaActivities/540jb5vh2guro',
'topics' => NULL,
'type' => 'POST_UNPUBLISHED',
'uri' => NULL,
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
),
'processed' =>
array (
),
))
Publishing a post question Publishing an Idea post doesn't seem to log anything.
@johnPhillips, can you try to log exception and see if there is an error? Just add error_log( $e->getMessage() ); in the catch section.
Deleting a post question Going to /wp-admin/edit.php?post_type=post and trashing a post (draft or published) seems to output the following (note POST_UNPUBLISHED):
It is correct because moving a post to trash is not deleting it. Try to permanently delete the post, then you should see the correct activity.
Thanks @eugene-manuilov, that's very helpful.
Can confirm the post deletion works:
array(
'collection_key' => 'topics',
'ideas' =>
array (
0 => 'ideas/9930000224243239024',
),
'name' => 'platforms/sitekit/properties/https%3A%2F%2Foi.ie/ideaActivities/4cemhj1on2t68',
'topics' => NULL,
'type' => 'POST_DELETED',
'uri' => NULL,
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
),
'processed' =>
array (
),
))
Publishing a post threw an error which we discussed was expected due to a mismatch between the URL set in development plugin settings (referrence url) and the request URL.
QA ✅