site-kit-wp: Syncing audiences and custom dimensions in parallel can result in the cached audiences not being persisted

Bug Description

When making parallel requests to sync both audiences and custom dimensions (POST:sync-audiences and POST:sync-custom-dimensions), it can result in the cached audiences not being persisted. It seems the call to sync-audiences saves the audiences, but the parallel call to sync-custom-dimensions then deletes them.

Steps to reproduce

Here is a straightforward way to exhibit the bug. Please note this was discovered during development of #8160 and so these instructions will only work once that issue’s PR is merged (or by checking out the branch for the PR, see https://github.com/google/site-kit-wp/pull/8885).

  1. Set up Site Kit with the audienceSegmentation feature flag enabled, and connect an Analytics property which has some data.
  2. Set up Key Metrics, selecting a metric which depends on a custom dimension.
  3. Click Enable groups on the Audience Segmentation Setup CTA Banner to set up the feature.
  4. Once the dashboard has loaded, use the WP CLI to delete the availableAudiences and availableCustomDimensions settings.
wp option patch delete googlesitekit_analytics-4_settings availableAudiences
wp option patch delete googlesitekit_analytics-4_settings availableCustomDimensions
  1. Reload the dashboard. You should see requests in the network tab being made to the POST:sync-audiences and POST:sync-custom-dimensions endpoints. If the property has got the new/returning visitor audiences set up with data, you may also see a couple of failed requests to the save-resource-data-availability-date endpoint (see screenshot below). Once this bug is fixed these requests should not fail. Note that the bug doesn’t occur every time - if you see these calls to save-resource-data-availability-date succeed, try again from point 3.
  2. Query the Analytics settings via the WP CLI. You should see availableAudiences is NULL or empty, rather than being an array of audiences as expected. Again, the bug doesn’t occur every time so if you do see an array of audiences, try again from point 3.
# Retrieve all settings.
wp option get googlesitekit_analytics-4_settings

# Retrieve the individual `availableAudiences` setting.
wp option pluck googlesitekit_analytics-4_settings availableAudiences

Screenshots

image

Actual: image

Expected: image

Additional Context

  • PHP Version: any
  • OS: any
  • Browser: any
  • Plugin Version: pre-release 1.130.0
  • Device: any

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

Acceptance criteria

  • Parallel requests to POST:sync-audiences and POST:sync-custom-dimensions should not interfere with each other.
  • Specifically, cached audiences should not be deleted by a parallel call to sync-custom-dimensions.
  • It should be ensured the reverse can’t happen too, i.e. the available custom dimensions can’t be deleted by a parallel call to sync-audiences with different timing.

Implementation Brief

Update Use of Audience Settings

Update Analytics Module JS infrastructure

  • Update assets/js/modules/analytics-4/datastore/audiences.js:

    • Remove the existing definition of getAvailableAudiences as this will be replaced by the new definition of this selector in assets/js/modules/analytics-4/datastore/audience-settings.js created in #10154.
  • Within assets/js/modules/analytics-4/datastore/index.js import and combine the assets/js/modules/analytics-4/datastore/audience-settings.js store created in #10154.

    • As the new store implements the selectors with the same names as before, no additional changes are required for the use of these selectors with the features components.

Migration

  • Create a migration named with the version number of the expected release, modelled on existing migrations (e.g.):
    • The migration should run for the n.e.x.t version.
    • The migration should get all settings from the Analytics settings (implemented in #10153, any of the following keys are present, their values should be added to the new audience settings options and removed from the Analytics settings.
      • availableAudiences
      • availableAudiencesLastSyncedAt
      • audienceSegmentationSetupCompletedBy

Test Coverage

  • Create a test file for the migration script.
  • No additional JS tests required.

QA Brief

  1. Verify that the issue reported here is resolved.
  2. Run a smoke test on various AS features to ensure everything is working normally and that no issues have been introduced due to this change.

QA:Eng Brief

  1. Set up Site Kit and connect an Analytics property which has some data.

  2. Set up Key Metrics, selecting a metric which depends on a custom dimension, you can refer screenshot below.

  3. Click Enable groups on the Audience Segmentation Setup CTA Banner to set up the feature.

  4. Once the dashboard has loaded, use the WP CLI to delete the availableAudiences and availableCustomDimensions settings.

wp option patch delete googlesitekit_analytics-4_audience_settings availableAudiences
wp option patch delete googlesitekit_analytics-4_settings availableCustomDimensions
  1. Reload the dashboard. You should see requests in the network tab being made to the POST:sync-custom-dimensions endpoints. Ensure that following requests are not errored and served successfully (if they exists in browser’s network tab):
  • POST:sync-audiences
  • POST:sync-custom-dimensions
  • POST: save-resource-data-availability-date
  1. Query the Analytics settings via the WP CLI. You should see availableAudiences populated.
# Retrieve all settings.
wp option get googlesitekit_analytics-4_audience_settings

# Retrieve the individual `availableAudiences` setting.
wp option pluck googlesitekit_analytics-4_audience_settings availableAudiences
  1. Run a smoke test around different AS features to make sure everything is working normally and no issues introduced due to this change.

Changelog entry

  • Fix issue where syncing audiences and custom dimensions in parallel could result in the cached audiences not being persisted.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20

Commits related to this issue

Most upvoted comments

While working on #10089, we identified a few regressions introduced by changes made as part of this ticket. We’ve moved this ticket back to execution to allow for thorough testing and resolution of the issues. Please refer to this comment and the related Slack discussion for more context.

cc: @wpdarren @ankitrox

@hussain-t As discussed, I will assign this ticket to myself while you look into another one. This ticket was dependent on 2 sub-issue, of which one is approved and other is merged and in QA, so this one can be started.

CC: @ivonac4

Thanks @benbowler! I’ve reviewed both of those new issues and sent them back your way with a bit of feedback.

As to this issue’s IB, it’s looking almost ready to roll, with just a couple of points to consider/address:

  • The GET / POST datapoints will be defined in #10153, so their definition can be removed from this IB.
  • Re. the rename of getAvailableAudiences -> getAudienceSettingsAvailableAudiences, getAudienceSegmentationSetupCompletedBy getAudienceSettingsAudienceSegmentationSetupCompletedBy and setAudienceSegmentationSetupCompletedBy setAudienceSettingsAudienceSegmentationSetupCompletedBy, taking a look at this again I do feel these renamed selectors are starting to get a bit unwieldy and so I’d reiterate on this point I made previously:

I don’t think this is necessary, really. We already use hand crafted selectors/actions that are indistinguishable from the auto generated ones in a lot of cases. Not that “we already do that” is necessarily a good reason to do something, but I don’t think keeping the same names would be a problem. Also, we do already have the user-scoped audience settings, and the suggested names could be a bit confusing what with there being two sets of audience settings that they could belong to at first glance.

Hi @benbowler, thanks for the update. A few points on the revised IB:

  • Update includes/Modules/Analytics_4.php:

It’s worth a note in the above section that we should also update this line to retrieve the audience settings from the new class instance:

https://github.com/google/site-kit-wp/blob/c2e25335021b5c4482aa3ee97eb6d5ac3ad417a6/includes/Modules/Analytics_4.php#L259

We also need to update usage of audience settings in get_debug_fields() and set_available_audiences():

https://github.com/google/site-kit-wp/blob/c33c9ba507df8493a789f6086a69589dbf29ebb3/includes/Modules/Analytics_4.php#L582

https://github.com/google/site-kit-wp/blob/c33c9ba507df8493a789f6086a69589dbf29ebb3/includes/Modules/Analytics_4.php#L2467-L2472

  • Preload the GET:get-audience-settings path like so.

Interesting, it looks like the above line is a stale remnant from when we had the user-scoped audience settings located in the Analytics 4 module. This was removed in the following PR, but the preload for the endpoint was missed. We should simply be able to keep the same line as it will apply to the new datapoint.

https://github.com/google/site-kit-wp/pull/9143/files#diff-090952b401bbcfab89340ec0e493b5ea942c199f0c73168668f230f1a50a2b82

  • GET:get-audience-settings
  • POST:set-audience-settings-audience-segmentation-setup-completed-by

We don’t usually prefix a GET request with get- in the datapoint name, and we don’t tend to use set- in the POST datapoint name. I’d also suggest we provide a general POST audience settings datapoint that would be more future proof than the specific POST:set-audience-settings-audience-segmentation-setup-completed-by datapoint.

Naming wise, I’d suggest the following for consistency:

  • GET:audience-settings
  • POST:save-audience-settings

We could restrict the POST datapoint to only allow updating the audienceSegmentationSetupCompletedBy setting for now, but it would leave it open for future settings.

  • Update references to getAudienceSettings to getUserAudienceSettings and update all current uses of this selector within the app.

We should also update the saveAudienceSettings(), resetAudienceSettings() and isSavingAudienceSettings() actions and selector that operate on the user audience settings to include User in their names.

In my update I suggest using new unique named selectors/action for these settings so as to distinguish that these aren’t the auto generated selectors/action from a settings store but we could keep the same name if preferred:

  • getAudienceSettingsAvailableAudiences
  • getAudienceSettingsAudienceSegmentationSetupCompletedBy
  • setAudienceSettingsAudienceSegmentationSetupCompletedBy

I don’t think this is necessary, really. We already use hand crafted selectors/actions that are indistinguishable from the auto generated ones in a lot of cases. Not that “we already do that” is necessarily a good reason to do something, but I don’t think keeping the same names would be a problem. Also, we do already have the user-scoped audience settings, and the suggested names could be a bit confusing what with there being two sets of audience settings that they could belong to at first glance.