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).
- Set up Site Kit with the
audienceSegmentationfeature flag enabled, and connect an Analytics property which has some data. - Set up Key Metrics, selecting a metric which depends on a custom dimension.
- Click Enable groups on the Audience Segmentation Setup CTA Banner to set up the feature.
- Once the dashboard has loaded, use the WP CLI to delete the
availableAudiencesandavailableCustomDimensionssettings.
wp option patch delete googlesitekit_analytics-4_settings availableAudiences
wp option patch delete googlesitekit_analytics-4_settings availableCustomDimensions
- Reload the dashboard. You should see requests in the network tab being made to the
POST:sync-audiencesandPOST:sync-custom-dimensionsendpoints. If the property has got the new/returning visitor audiences set up with data, you may also see a couple of failed requests to thesave-resource-data-availability-dateendpoint (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 tosave-resource-data-availability-datesucceed, try again from point 3. - Query the Analytics settings via the WP CLI. You should see
availableAudiencesisNULLor 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
Actual:
Expected:
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-audiencesandPOST:sync-custom-dimensionsshould 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-audienceswith different timing.
Implementation Brief
Update Use of Audience Settings
-
Update
includes/Modules/Analytics_4.php:- Create a new const
$audience_settings. - Within the
registermethod:- Instantiate and instance of the new
.../Analytics_4/Audience_Settingsclass to the const$audience_settings. - Within the current
$this->get_settings()->on_change(callback:- Update the following line to get the
availableAudiencesfrom the new$audience_settingssettings instance. - https://github.com/google/site-kit-wp/blob/c2e25335021b5c4482aa3ee97eb6d5ac3ad417a6/includes/Modules/Analytics_4.php#L259
- Update this block to update the
availableAudiencesin the new$audience_settingssettings instance. https://github.com/google/site-kit-wp/blob/390cbd5f9b6242272ef6f1ce792f8f3d64869d6c/includes/Modules/Analytics_4.php#L271-L287
- Update the following line to get the
- Update the filter on
pre_update_option_googlesitekit_analytics-4_settingsto nullify the settings within the $audience_settings instance.
- Instantiate and instance of the new
- Update the
set_available_audiencesmethod to store the audience list to the new audience settings options key. - Update the following line to retrieve audiences from the new audience settings options key: https://github.com/google/site-kit-wp/blob/ea76d2b720af2695c68fb51e219e0497de78fcd0/includes/Modules/Analytics_4.php#L579
- Update
get_debug_fieldsto use the new audience settings for audience related debug fields: - Update
set_available_audiencesto set the available audience to the new audience settings options:
- Create a new const
-
Update
includes/Modules/Analytics_4/Settings.php:- Remove the following fields from the
get_view_only_keysandget_defaultmethods:availableAudiencesavailableAudiencesLastSyncedAtaudienceSegmentationSetupCompletedBy
- Remove the following lines from
get_sanitize_callback: https://github.com/google/site-kit-wp/blob/ea76d2b720af2695c68fb51e219e0497de78fcd0/includes/Modules/Analytics_4/Settings.php#L199-L215
- Remove the following fields from the
-
Update
includes/Modules/Analytics_4/Resource_Data_Availability_Date.phpto use the Audience settings to retrieveavailableAudiences.
Update Analytics Module JS infrastructure
-
Update
assets/js/modules/analytics-4/datastore/audiences.js:- Remove the existing definition of
getAvailableAudiencesas this will be replaced by the new definition of this selector inassets/js/modules/analytics-4/datastore/audience-settings.jscreated in #10154.
- Remove the existing definition of
-
Within
assets/js/modules/analytics-4/datastore/index.jsimport and combine theassets/js/modules/analytics-4/datastore/audience-settings.jsstore 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.tversion. - 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.
availableAudiencesavailableAudiencesLastSyncedAtaudienceSegmentationSetupCompletedBy
- The migration should run for the
Test Coverage
- Create a test file for the migration script.
- No additional JS tests required.
QA Brief
- Verify that the issue reported here is resolved.
- 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
-
Set up Site Kit and connect an Analytics property which has some data.
-
Set up Key Metrics, selecting a metric which depends on a custom dimension, you can refer screenshot below.
-
Click Enable groups on the Audience Segmentation Setup CTA Banner to set up the feature.
-
Once the dashboard has loaded, use the WP CLI to delete the
availableAudiencesandavailableCustomDimensionssettings.
wp option patch delete googlesitekit_analytics-4_audience_settings availableAudiences
wp option patch delete googlesitekit_analytics-4_settings availableCustomDimensions
- Reload the dashboard. You should see requests in the network tab being made to the
POST:sync-custom-dimensionsendpoints. Ensure that following requests are not errored and served successfully (if they exists in browser’s network tab):
POST:sync-audiencesPOST:sync-custom-dimensionsPOST: save-resource-data-availability-date
- Query the Analytics settings via the WP CLI. You should see
availableAudiencespopulated.
# 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
- 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
- Merge pull request #10653 from google/fix/#10089-#8888-audience-settings Save audience settings when audience groups are enabled — committed to google/site-kit-wp by hussain-t 2 years ago
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:
GET/POSTdatapoints will be defined in #10153, so their definition can be removed from this IB.getAvailableAudiences->getAudienceSettingsAvailableAudiences,getAudienceSegmentationSetupCompletedBygetAudienceSettingsAudienceSegmentationSetupCompletedByandsetAudienceSegmentationSetupCompletedBysetAudienceSettingsAudienceSegmentationSetupCompletedBy, 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:Hi @benbowler, thanks for the update. A few points on the revised IB:
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()andset_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
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
We don’t usually prefix a GET request with
get-in the datapoint name, and we don’t tend to useset-in the POST datapoint name. I’d also suggest we provide a generalPOST audience settingsdatapoint that would be more future proof than the specificPOST:set-audience-settings-audience-segmentation-setup-completed-bydatapoint.Naming wise, I’d suggest the following for consistency:
We could restrict the
POSTdatapoint to only allow updating theaudienceSegmentationSetupCompletedBysetting for now, but it would leave it open for future settings.We should also update the
saveAudienceSettings(),resetAudienceSettings()andisSavingAudienceSettings()actions and selector that operate on the user audience settings to includeUserin their names.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.