Implement GA4 logic in server-side callback for creating a new Analytics account
In the server-side callback where a new Analytics account has been created, if the ga4setup flag is active, a corresponding GA4 property should be created as well.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- In
Analytics::handle_provisioning_callbacka new action should be triggered right before redirecting the usergooglesitekit_analytics_handle_provisioning_callback. - Then in the
Analytics_4class a callback should be added to that action which creates a corresponding new GA4 property and GA4 web data stream and stores their data in the respectiveAnalytics_4\Settingssettings.- Since the respective data points for creating a GA4 property and creating a GA4 web data stream already rely on the current website (e.g. for name and URL), it's not really necessary to have any additional context on which account/property/profile were created. It is just a new one for the current site.
- If any of these API requests fails, it should be ignored / not result in a user-facing error or blocking behavior.
Implementation Brief
- Trigger the new
googlesitekit_analytics_handle_provisioning_callbackaction right before the final redirect in theAnalytics::handle_provisioning_callback()method.- The action should retrieve account ID, property ID, profile ID, and internal web property ID as action arguments.
- Extract the property creation script into a new private method
create_property( $accountID ): https://github.com/google/site-kit-wp/blob/8da6a9176fd4c5dd3ef6100d3d1b9e75ecb91635/includes/Modules/Analytics_4.php#L229-L233 - Extract the web data stream creation script into a new private method
create_webdatastream( $propertyID ): https://github.com/google/site-kit-wp/blob/8da6a9176fd4c5dd3ef6100d3d1b9e75ecb91635/includes/Modules/Analytics_4.php#L244-L248 - Add a new private method
handle_provisioning_callback( $accountID )to theAnalytics_4class.- In the method body, create a new ga4 property and web datastream using new helper methods.
- Save new
propertyIDandwebDataStreamIDin the Analytics4 settings. - Suppress any error triggered during the creation of new property and web data stream because the ga4 API is still in the alpha phase. Add a to-do note to remove error suppression once ga4 API becomes stable.
- Add a new hook to the
Analytics_4::registermethod that calls the newhandle_provisioning_callbackmethod for thegooglesitekit_analytics_handle_provisioning_callbackaction.
Test Coverage
- N/A
Visual Regression Changes
- N/A
QA Brief
- Check the
Analytics_4class that it has thehandle_provisioning_callbackmethod which is connected to thegooglesitekit_analytics_handle_provisioning_callbackaction in theregistermethod. - Check that the
Analytics_4::handle_provisioning_callbackmethod creates a new property and web data stream for the provided account ID. - Check the
Analytics::handle_provisioning_callback()method and make sure that it triggers thegooglesitekit_analytics_handle_provisioning_callbackaction at the very end and before the success redirect.
Changelog entry
- Update Analytics account provisioning to create a corresponding GA4 property as well.
Looks good, just one question:
Suppress any error triggered during the creation of new property and web data steam.
I'm guessing we want to do this because the API is new/unstable? But maybe we should mention why in the IB or make it explicit there to include a comment why we're silencing errors rather than surfacing them, or we could get weird bugs in the future. Or, at least, developers might be confused when they see the code and wonder why we're doing that.
What do you think @eugene-manuilov?
@tofumatt ok, updated it. Could you please check it one more time?
Thanks!
IB ✅
Un-assigning myself as I'm not sure my PHP is currently up to the task!
@eugene-manuilov please add a QAB 👍
QAB is added.