Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the new Audience_Settings class and datapoints. #10153

Closed
2 tasks done
benbowler opened this issue Feb 3, 2025 · 13 comments
Closed
2 tasks done

Add the new Audience_Settings class and datapoints. #10153

benbowler opened this issue Feb 3, 2025 · 13 comments
Labels
Feature: Audiences Module: Analytics Google Analytics module related issues P2 Low priority Team M Issues for Squad 2 Type: Bug Something isn't working

Comments

@benbowler
Copy link
Collaborator

benbowler commented Feb 3, 2025

Feature Description

Issue 1/3 to fix the bug #8888.

Add the new Audience_Settings class and datapoints, and corresponding GET/POST REST endpoints.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • A new Audience_Settings PHP settings class should be created.
  • The settings should be site-wide, i.e. it should use the options DB table for persistence.
  • It should manage the audience settings that are currently handled in the Modules\Analytics_4\Settings class:
    • availableAudiences
    • availableAudiencesLastSyncedAt
    • audienceSegmentationSetupCompletedBy
  • REST endpoints GET:audience-settings and POST:save-audience-settings for fetching and updating the settings should be added to the Analytics_4 module.
  • Use of the existing settings needn't be replaced as part of this issue, this will happen via Syncing audiences and custom dimensions in parallel can result in the cached audiences not being persisted #8888.

Implementation Brief

Extract Audience Settings to new Options Key

Note: this ticket creates the new settings store but does not yet implement it. This will be done later in #8888

  • Create a new settings class includes/Modules/Analytics_4/Audience_Settings.php, extending Core\Storage\Setting and implementing Setting_With_ViewOnly_Keys_Interface, using the standard settings class pattern:

    • The OPTION should be googlesitekit_analytics-4_audience_settings.
    • get_view_only_keys should return an array containing availableAudiences and audienceSegmentationSetupCompletedBy.
    • get_default should return an array containing:
      'availableAudiences' => null,
      'availableAudiencesLastSyncedAt' => 0,
      'audienceSegmentationSetupCompletedBy' => null,
    • get_sanitize_callback should implement the standard pattern copying these key setting conditions:
      if ( isset( $option['availableAudiences'] ) ) {
      if ( ! is_array( $option['availableAudiences'] ) ) {
      $option['availableAudiences'] = null;
      }
      }
      if ( isset( $option['availableAudiencesLastSyncedAt'] ) ) {
      if ( ! is_int( $option['availableAudiencesLastSyncedAt'] ) ) {
      $option['availableAudiencesLastSyncedAt'] = 0;
      }
      }
      if ( isset( $option['audienceSegmentationSetupCompletedBy'] ) ) {
      if ( ! is_int( $option['audienceSegmentationSetupCompletedBy'] ) ) {
      $option['audienceSegmentationSetupCompletedBy'] = null;
      }
      }
  • Update includes/Modules/Analytics_4.php:

    • Create a new const $audience_settings.
    • Within the register method:
      • Keep this line which will preload the GET:audience-settings datapoint.
    • Update the get_datapoint_definitions method adding two new REST datapoints:
      • GET:audience-settings, setting 'shareable' => true.
      • POST:save-audience-settings
    • Update the create_data_request method:
      • For GET:audience-settings requests:
        • Check for plugin manage options permissions using current_user_can( Permissions::MANAGE_OPTIONS );:
          • If true, return all of the settings field.
          • If false, use array_intersect_key and $audience_settings->get_view_only_keys() to filter and return only the view only keys.
      • For POST:save-audience-settings requests, update the value of audienceSegmentationSetupCompletedBy.
    • Update parse_data_response method to parse and return the responses for each of the new endpoints.

Test Coverage

QA Brief

  1. Ensure that Basic Authentication plugin is installed and activated.

  2. Open POSTman or any similar API testing tool. Set the Authorization to basic auth and add username and password.

  3. Trigger a GET request to https://example.com/wp-json/google-site-kit/v1/modules/analytics-4/data/audience-settings, replace domain name with appropriate one. It should return following response.

  {
      "availableAudiences": null,
      "audienceSegmentationSetupCompletedBy": null,
      "availableAudiencesLastSyncedAt": 0
  }
  1. Similarly, setup a POST request to https://example.com/wp-json/google-site-kit/v1/modules/analytics-4/data/save-audience-settings, replace domain name with appropriate one and add following body as JSON payload.
  {
    "data": {
        "audienceSegmentationSetupCompletedBy": 9999,
    }
  }

Once successful, you shall receive the following payload.

{
    "availableAudiences": null,
    "availableAudiencesLastSyncedAt": 0,
    "audienceSegmentationSetupCompletedBy": 9999
}

Notice that only audienceSegmentationSetupCompletedBy can currently be updated as per the AC.

  1. Hit the GET endpoint mentioned above and there should be audienceSegmentationSetupCompletedBy set as in POST request.

Changelog entry

  • Add a new option for audience settings, with REST endpoints to retrieve and update it.
@techanvil
Copy link
Collaborator

techanvil commented Feb 5, 2025

Thanks @benbowler, this IB is looking good. A couple of small points:

  • For the sake of clarity, it would be worth specifying which settings base class we are going to use. Looking at the current pattern of usage my initial take was that it should extend Core\Storage\Setting, although extending Core\Modules\Module_Settings could be on the cards instead - to date, though, we've only used that for the module-level settings e.g. Ads\Settings, Analytics_4\Settings etc.
  • Although it should be fairly self evident, let's specify that we should implement the interface Setting_With_ViewOnly_Keys_Interface. This leads to the question of how we're going to ensure the get_view_only_keys() method is actually used for these settings considering how the current use of it is limited to the modules/(?P<slug>[a-z0-9\-]+)/data/settings endpoint, but this can be handled in Syncing audiences and custom dimensions in parallel can result in the cached audiences not being persisted #8888.

@techanvil techanvil assigned techanvil and unassigned techanvil and benbowler Feb 5, 2025
@techanvil
Copy link
Collaborator

techanvil commented Feb 5, 2025

Hey @benbowler, one more point - I've realised it would be sensible to add the REST endpoints in this issue, so the datastore additions in the subsequent #10154 can be properly tested. I've amended the AC, please can you include these in the IB and amend the estimate accordingly?

We'll want to make sure we check make appropriate use of the get_view_only_keys() method when handling the GET:audience-settings request in a view-only context.

Update: In fact, I see the title of the issue is actually defined as "Add the new Audience_Settings class and datapoints", so it looks like this just got missed from the IB.

@techanvil
Copy link
Collaborator

@benbowler, please can you expand on the GET:audience-settings datapoint in the IB? We should be clear that it should be shareable, and should use the get_view_only_keys() method to determine which keys are returned in a view-only context.

@techanvil techanvil assigned benbowler and unassigned techanvil Feb 11, 2025
@benbowler benbowler assigned techanvil and unassigned benbowler Feb 11, 2025
@benbowler
Copy link
Collaborator Author

Hey @techanvil, I've expanded the IB to handle filtering view only fields via the datapoints endpoint. Lmk your thoughts.

@techanvil
Copy link
Collaborator

Thanks @benbowler, it's an interesting idea to amend the REST route handler for datapoints, but I don't think we need to handle it at that level.

I'd suggest we instead simply handle this as a one-off in the create_data_request() case for GET:audience-settings for the time being.

If we find that we want to provide some generic handling for these and additional settings in the future, then it would make sense to introduce a common handler, but I think it would be preferable for this to be a separate route pattern rather than adding this logic to the datapoint handler; it would be preferable not to add complexity to the datapoint route handler for a specialised subset of datapoints and rather allow these to be managed via their own route.

@techanvil techanvil assigned benbowler and unassigned techanvil Feb 11, 2025
@benbowler benbowler assigned techanvil and unassigned benbowler Feb 12, 2025
@techanvil
Copy link
Collaborator

Thanks for updating the IB @benbowler, that's LGTM.

IB ✅

@techanvil techanvil removed their assignment Feb 12, 2025
@ankitrox ankitrox assigned ankitrox and unassigned ankitrox Feb 14, 2025
@ankitrox ankitrox removed their assignment Feb 19, 2025
@techanvil techanvil assigned techanvil and ankitrox and unassigned techanvil Feb 24, 2025
@ankitrox ankitrox assigned techanvil and unassigned ankitrox Feb 24, 2025
@techanvil
Copy link
Collaborator

techanvil commented Feb 25, 2025

Hi @ankitrox, I've merged the PR for this issue, however the QAB still needs an update. As per my previous comment:

The QAB will also need an update as it currently covers updating all the values.

See also: #10245 (comment)

Furthermore, I've noticed the brief states that audienceSegmentationSetupCompletedBy won't change, which is also no longer correct and needs an update.

Please can you update the QAB accordingly?

@techanvil techanvil assigned ankitrox and unassigned techanvil Feb 25, 2025
@ankitrox
Copy link
Collaborator

Hi @techanvil ,

Thank you for review and merging the PR.

I've updated the QAB by taking into account that only audienceSegmentationSetupCompletedBy setting could be updated.

@ankitrox ankitrox removed their assignment Feb 25, 2025
@techanvil
Copy link
Collaborator

Thanks @ankitrox. Please note, I made a small tweak to simplify the POST payload.

@ankitrox
Copy link
Collaborator

Please note, I made a small tweak to simplify the POST payload.

Thank you @techanvil . It looks fine now.

@wpdarren wpdarren self-assigned this Feb 26, 2025
@wpdarren
Copy link
Collaborator

QA Update: ⚠

@ankitrox @techanvil admittedly, it has been a long time since I used POSTman, but here's an observation. When I run step 3, to trigger a GET request, the output is different than the QAB.

{
    "availableAudiences": null,
    "availableAudiencesLastSyncedAt": 0,
    "audienceSegmentationSetupCompletedBy": null
}

I set up Site Kit for this particular site, along with Audience Segmentation.

According to the QAB audienceSegmentationSetupCompletedBy should be 0.

Image

Please could you check the QAB, or, let me know if this user error. Thank you.

@techanvil
Copy link
Collaborator

Thanks @wpdarren, this was a mistake in the QAB - audienceSegmentationSetupCompletedBy should indeed initially be null, its default value. I've updated the QAB accordingly.

FYI, setting up Audience Segmentation has no bearing on this particular issue because the new settings haven't been integrated into the wider codebase yet, this will be done in #8888.

@wpdarren
Copy link
Collaborator

QA Update: ✅

Verified:

  • Following the QAB steps, I can confirm that I received the following GET response.
  {
      "availableAudiences": null,
      "audienceSegmentationSetupCompletedBy": null,
      "availableAudiencesLastSyncedAt": 0
  }
  • Following the QAB steps, I can confirm that I received the following POST response.
{
    "availableAudiences": null,
    "availableAudiencesLastSyncedAt": 0,
    "audienceSegmentationSetupCompletedBy": 9999
}
  • Following the QAB steps, I can confirm that I received the following GET response.
{
    "availableAudiences": null,
    "availableAudiencesLastSyncedAt": 0,
    "audienceSegmentationSetupCompletedBy": 9999
}

Looks good. See screenshots below. Note. The code in the QAB has a typo in step 4. Removing the comma from the payload code and this runs perfectly in POSTman. It errored out when I left it in.

Screenshots

Image
Image
Image

@wpdarren wpdarren removed their assignment Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Audiences Module: Analytics Google Analytics module related issues P2 Low priority Team M Issues for Squad 2 Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants