site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Implement GA4 logic in server-side callback for creating a new Analytics account

Open felixarntz opened this issue 5 years ago • 7 comments

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_callback a new action should be triggered right before redirecting the user googlesitekit_analytics_handle_provisioning_callback.
  • Then in the Analytics_4 class 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 respective Analytics_4\Settings settings.
    • 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_callback action right before the final redirect in the Analytics::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 the Analytics_4 class.
    • In the method body, create a new ga4 property and web datastream using new helper methods.
    • Save new propertyID and webDataStreamID in 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::register method that calls the new handle_provisioning_callback method for the googlesitekit_analytics_handle_provisioning_callback action.

Test Coverage

  • N/A

Visual Regression Changes

  • N/A

QA Brief

  • Check the Analytics_4 class that it has the handle_provisioning_callback method which is connected to the googlesitekit_analytics_handle_provisioning_callback action in the register method.
  • Check that the Analytics_4::handle_provisioning_callback method 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 the googlesitekit_analytics_handle_provisioning_callback action at the very end and before the success redirect.

Changelog entry

  • Update Analytics account provisioning to create a corresponding GA4 property as well.

felixarntz avatar Apr 29 '21 02:04 felixarntz

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 avatar May 11 '21 15:05 tofumatt

@tofumatt ok, updated it. Could you please check it one more time?

eugene-manuilov avatar May 11 '21 16:05 eugene-manuilov

Thanks!

IB ✅

tofumatt avatar May 13 '21 14:05 tofumatt

Un-assigning myself as I'm not sure my PHP is currently up to the task!

johnPhillips avatar May 26 '21 14:05 johnPhillips

@eugene-manuilov please add a QAB 👍

aaemnnosttv avatar Jun 04 '21 15:06 aaemnnosttv

QAB is added.

eugene-manuilov avatar Jun 04 '21 16:06 eugene-manuilov

QA: ✅

asvinb avatar Jun 07 '21 08:06 asvinb