-
Notifications
You must be signed in to change notification settings - Fork 328
Open
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature
Description
Feature Description
This ticket encompasses the email sending logic, but does not include the HTML template which will be worked on and implemented in separate issues. This issue focusses on retrieving report data, creating a basic (un-styled or minimally styled) HTML email containing this report data and sending using WP mail infrastructure.
- Within the scheduled event, get report create and send a MVP HTML email with key report data
- No styling or layout considerations required simply the sending and batching logic
- Track success or failure in the post type
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The "Worker" (implemented in 11547) should be extended to perform the report generation and sending process so it handles:
- Generate report, assemble email template and send the email for each user in the batch using infrastructure implemented in 11566
- Update the CPT post status:
- If successfully sent, update the post status to
sentand update thepost_date. - If sending fails, update the post status to
failedand store any errors returned within the CPT post metaerror_details.
- If successfully sent, update the post status to
- Clear the transient
googlesitekit_email_reporting_worker_locktransient if present
- Subscribers receive the email report with live, up-to-date metrics on their chosen cadence (weekly, monthly, quarterly); unsubscribed users receive none; feature disabled = no sends.
- Content reflects the subscriber’s permissions:
- Admins: data from modules active on the site.
- View-only: only modules shared with that user via Dashboard Sharing.
- Unavailable/recoverable modules or empty sections are omitted cleanly without visual gaps.
- Any single report failure causes the entire report; no email is sent.
- If no module data is available, no email is sent.
- The template renders identically to the mock-based version, now populated by live data
Implementation Brief
- Update
includes/Core/Email_Reporting/Email_Reporting.phpto instantiate the full pipeline and pass them intoWorker_Task:Email_Reporting_Data_Requests(Create email reporting data requests classes #11566).Email_Report_Section_Builder(Create email reporting data and section classes #11680).Google\Site_Kit\Core\Util\Email(from Create a reusable Email class #11564).Email_Log- Adjust
Worker_Task's constructor signature to accept these services and store them.
- Extend
Worker_Task::handle_worker_action()- Within existing
$pending_idsloop, for each pending post:- Fetch raw payload with
$data_requests->get_user_payload( $user_id, $date_range ). -$user_idcan be extracted from the post, it will be set as the post author. And$date_rangefromEmail_Log::get_date_range_from_log- On
WP_Error, callmark_failed( $post_id, $error )and continue.
- On
- Build sections with
Email_Report_Section_Builder::build_sections( $raw_payload, $frequency, get_user_locale( $user_id ) ). If empty, mark failed (reason: no data) and continue. Error handling should be also covered as section building can throw an error - in this case omit sending the email for current user - Assemble/build the email template via
Google\Site_Kit\Core\Email_Reporting\Email_Template_Renderer::render()by passing the sections previously built withEmail_Report_Section_Builderas the$dataargument. On error, mark failed. - Send email using
$email_sender->send( $to, $subject, $html_body ), where$subjectcan assembled via a helper method. And for subject useYour performance at a glance for {sitename.com}(and if it is the multisite setup with subdirectories, then we should add site's directory as well).- On success:
mark_sent( $post_id )– update status to sent, clear error_details, update post_date/post_date_gmt. - On failure:
mark_failed( $post_id, $error_or_false ).
- On success:
- Remove
googlesitekit_email_reporting_worker_lock_{$frequency}transient if present
- Fetch raw payload with
- Within existing
- Add helper methods inside
Worker_Task:mark_failed( $post_id, WP_Error|string $error ): set status failed, store serialized error inEmail_Log::META_ERROR_DETAILS.- Use
Email_Log_Batch_Query::update_status()andupdate_post_meta()for status/meta updates.
- Use
mark_sent( $post_id ): set status sent, deleteerror_details, update timestamps.build_subject( $frequency ): centralize subject formatting/localization.- For example -
$subject = sprintf( __( 'Your %s Site Kit report for %s', 'google-site-kit' ), $frequency, $site_domain )
- For example -
Test Coverage
- Update existing tests for the
Worker_Taskclass to cover new scenarios:- Happy path: mock all collaborators so data requests > sections > template > email send succeed; assert CPT status becomes
sent,post_dateupdates,error_detailscleared, and lock is deleted. - Data request failure: return
WP_Errorfromget_user_payload(), ensuremark_failed()runs and worker continues to the next post. - Empty sections: return empty array from
build_sections(); confirm the post is marked failed with “no data” error. - Template render failure: make
render_template()returnWP_Error; post should be marked failed. - Send failure: stub
Email::send()to returnWP_Error; verify CPT status failed and meta updated. - Lock cleanup: regardless of outcome,
delete_transient( 'googlesitekit_email_reporting_worker_lock_{frequency}' )is called once at the end.
- Happy path: mock all collaborators so data requests > sections > template > email send succeed; assert CPT status becomes
QA Brief
- Install a plugin for seeing the CRON events, like
WP Crontrol - Setup Site Kit (initially with oi.ie to follow the sections that are expected to be present - have data) and run the scenarios
Scenario 1
- As an Admin do not install any module, default one is only active -
Search Console - Subscribe to Email Reporting from User Menu dropdown. Pick any frequency, for example I will be using
Weekly - Go to Tools > Cron Events and find initiator CRON with argument "Weekly". Click
Run nowon it, it will spawn a worker CRON scheduled to run in 59s - (Optional) Go to the database, under
wp_poststable, when sorting data by post ID (descending) you should see a new empty post with statusgsk_email_scheduled, and inwp_postmetayou will see meta data attached to that post ID_batch_id,_frequencyetc - You can wait for 1min for worker CRON to execute or also trigger it with
Run now - You should receive Email matching the Figma design for SC only Figma
Scenario 2
- As an admin activate and setup
Analyticsmodule (this will make it to 2 active modules with reports - Analytics and Search Console) - Follow the steps from Scenario 1 to trigger the email sending
- The email template should have sections like on this basic email Figma, with addition of Add to cart and purchase section at the top as
oi.iehas those events on the website
Scenario 3
- Following from scenario 2, enable
Audience Segmentationon the website - Follow the steps from previous scenario to trigger email sending
- It should have all the sections like in previous email, with addition of
New VisitorsandReturning Visitors, resembling the full report Figma (Note: Subscribers are just a placeholder, will not be present in real report as such, but custom Audiences will show there when present and have data)
Scenario 4
- Add another user, with like
editorrole for example - Share SC with the editor
- Log in as that editor and subscribe to the email repporting
- Follow the steps from previous scenarios to trigger email sending
- Verify that user is receiving only SC related data, while admin is receiving full report
Scenario 5
- Continuing on previous Scenario 4, share GA4 module with the editor
- trigger email sending like before and verify that user is receiving GA4 related metrics now as well (all shareable data should be present, which should be all the sections, like how admin receives it)
You can experiment with different website that has no purchase/add to cart events, to verify that first section is not showing on such websites
Users with no shared module acces, or that are not subscribed, should not receive an email
Changelog entry
Metadata
Metadata
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature