As a WP Rocket plugin, I want to send global score to WP Rocket website
GIVEN I'm a WP Rocket website WHEN Global score calculation is finished THEN I send global score and necessary data to WP Rocket website
Scope a solution
When a Rocket Insights test completes, the plugin needs to send the calculated global score to the SaaS API. For automatic tests (homepage auto-add on activation), it should also trigger a website endpoint that forwards an event to Klaviyo for marketing.
Currently the GlobalScore class calculates scores locally and caches them. No external system can access this data. We need a new ScoreSyncService that hooks into rocket_rocket_insights_job_completed and sends the score to two endpoints: the SaaS /domain-scores API (always) and the website /klaviyo/insights-test endpoint (only for automatic tests).
The service gets injected with API client, GlobalScore, Context, and User. It checks if sync is allowed, retrieves the current score, detects if the test was automatic by checking the source metadata ('auto-added homepage' vs 'dashboard'), builds a payload with domain/score/pages/automatic flag/user ID, and fires async non-blocking requests.
Error handling should be graceful - log failures but don't break the plugin. The local score calculation continues working regardless of API availability.
Dependencies: Need the SaaS and website API endpoints ready first, plus specs for authentication and payload format. Confirm wpr_user_id is accessible via User service.
Open questions for @wp-media/product & @wp-media/engineering-wpr-plugin-team :
- Sync on every test or only when score changes?
- Do we want to do a request from WPR to Klaviyo or the SaaS should do it ?
Thanks @Miraeld
We don't need to mix-up this feature with klaviyo, here we will send only the request to SaaS side only when the global score changes for any reason with the following request details:
- domain
- average_score
- blurred? for free users, when a test finishes, if no credits left : result is blurred
- credits_left: only needed by marketing?
- paid/free (? we already have this in Licenses service and WPR website)
- automatic_test: true if the website only used the automated test / false if there was customization
- wpr_user_id? It would be needed for sending data to Klaviyo
Some Notes
- This request will be a POST request to an endpoint that is not ready yet so we will need a snippet to simulate the success/failure responses from this endpoint to help on testing then we will share this snippet with the @wp-media/engineering-wpr-addons-team to help when developing this feature, also we can start with any endpoint name for now.
- We will send this request only when the global score changes
- For
automatic_testwe need to send it with true only when add homepage automatically after fresh/update (currently we add it only with fresh installation) so SaaS can trigger this Klaviyo thing. - we will send this 1 API request when the global score changes so we may need to save the last sent score in plugin to compare with every transient clear (we save global score into transient)
Grooming
- Create a new option
wp_rocket_sent_global_scorewith default value of null. - In the following two cases compare the current global score with the option
wp_rocket_sent_global_score's value then send the request to SaaS if both are not the same:- With upgrade to 3.20.3
- Add a callback for the action
set_transient_wpr_global_score_data
- Create
GlobalScoreSaaSAPIClientclass exactly hereinc/Engine/Admin/RocketInsights/APIHandlerextending fromAbstractAPIClientclass and add a method to send the request to SaaS endpointglobalscore - Inside
inc/Engine/Admin/RocketInsights/GlobalScore.phpclass let it takeGlobalScoreSaaSAPIClientobject as dependency to be used for sending the request to SaaS - Create a new method there to compare the value of the option with the value of current global score then send the request to saas if both are different.
- In
inc/Engine/Admin/RocketInsights/Subscriber.phpclass add one callback for the two actions mentioned above. - Create external snippet to be used for testing until implementing the feature from Addons side (a snippet for every case like success and failure).
Effort
[S] to [M]
Seems fine to me, not too complicated