Implement submitChanges and validateCanSubmitChanges logic for GA4 store
The GA4 store in JS modules/analytics-4 should receive a submitChanges and validateCanSubmitChanges implementation to handle GA4 data submissions correctly. Since for now all GA4 behavior will be controlled through existing Analytics UI, it should also be incorporated into the similar action/selector respectively of the modules/analytics store.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The
modules/analytics-4store should receive its own implementation ofsubmitChangesandvalidateCanSubmitChanges(via thecreateModuleStorecall), with these functions themselves being implemented inassets/js/modules/analytics-4/datastore/settings.js(similar to how it is for themodules/analyticsstore). Both of the new function implementations should follow the same patterns used in the similar functions in themodules/analyticsstore. - The
submitChangesfunction should do the following:- If the store's
getPropertyIDisPROPERTY_CREATE, a new GA4 property should be created using thecreatePropertyaction. Afterwards, the new property should be set. - If the store's
getWebDataStreamIDis not valid (a new utility functionisValidWebDataStreamIDshould be added toassets/js/modules/analytics-4/utils/validation.jsfor this purpose), a new GA4 web data stream should be created using thecreateWebDataStreamaction. Afterwards, the new web data stream ID and measurement ID from it should be set. - Then, if settings have changed, the changes should be saved using
saveSettings, and on success theanalytics-4cache should be invalidated (no need to include the legacy data API cache here, since it isn't used for anyanalytics-4API requests). - If any error occurs during the above, it should not be returned by the function - the function should in that case simply return the success response (empty object) early. This is necessary since the GA4 APIs are only in alpha, and errors shouldn't prevent any user-facing processes to fail/be blocked, also given that GA4 is not essential at this point to use Site Kit.
- If the store's
- The
validateCanSubmitChangesfunction should do the following:- Return early if the GA4 API is not working (i.e.
isAdminAPIWorking === false). This is necessary to include since the GA4 APIs are only in alpha, and errors shouldn't prevent any user-facing processes to fail/be blocked, also given that GA4 is not essential at this point to use Site Kit. - Fail if the store currently
isDoingSubmitChanges. - Fail if for the store
! haveSettingsChanged. - Fail if the store's
getPropertyIDis not a valid GA4 property selection.
- Return early if the GA4 API is not working (i.e.
Implementation Brief
Overview
- Create a new 'settings' file in
assets/js/modules/analytics-4/datastore/ - Use the
settingsstore in the Analytics module as a guide - The settings store should export two functions:
submitChangesandvalidateCanSubmitChanges - We will also need a new utility,
isValidWebDataStreamID, which should be added toassets/js/modules/analytics-4/utils/validation.js - Please write test coverage for these new functions
isValidWebDataStreamID utility
- Should accept a
webDataStreamID - Should test that it is numeric and return
trueif it is - Should return
falseif it is not numeric - Should return
falseif it isundefined. - Please add tests for this util. If it's useful, there are fixtures which can be used in
assets/js/modules/analytics-4/datastore/__fixtures__/. - Examples of this sort of validation can be found in the Analytics-4 and Analytics modules' utilities.
submitChanges function
- Again, familiarise yourself with how this is implemented in the Analytics store, as we will re-use the same patterns.
- Select the
propertyIDfrom the store using thegetPropertyIDselector - If the
propertyIDisPROPERTY_CREATE, then a new GA4 property should be created using thecreatePropertyaction. Afterwards, the new property should be set. - Select the
webDataStreamIDfrom the store - Run it through the
isValidWebDataStreamIDutility, and if it is not valid, create a new GA4 web data stream using thecreateWebDataStreamaction. Then set the new web data stream ID and measurement ID in the store usingsetWebDataStreamIDandsetMeasurementID. - Determine if the settings have changed using
haveSettingsChanged, and if so, the changes should be saved usingsaveSettings. - On a successful save, invalidate the cache using
API.invalidateCache. - Note the ACs above with regard to errors: just return the success response early.
validateCanSubmitChanges function
- Call
isAdminAPIWorkingand return early if this isfalse. - Return false / fail if any of the following criteria are met (consider using
strictSelectfor this, an example can be found in the AnalyticsvalidateCanSubmitChangesimplementation):- the store currently
isDoingSubmitChanges. ! haveSettingsChanged.- If the store's
getPropertyIDis not a valid GA4 property selection (useisValidPropertySelection).
- If the store's
- the store currently
Add to the store
- Import the two new functions into the Analytics-4
basestore, and add them to theModules.createModuleStorecall (as done in the Analytics module).
Test Coverage
- New tests should be added for the new functions and util as described above. Existing tests should pass.
Visual Regression Changes
- None expected.
QA Brief
- Check
validateCanSubmitChangesandsubmitChangesfunctions in theanalytics-4/datastore/settings.jsfile. Make sure those functions satisfy requirements from the AC.
Changelog entry
- Add change validation logic to Google Analytics 4 data store.
@johnPhillips IB mostly looks good, there is just one thing that we need to adjust:
isValidWebDataStreamID utility Should return
undefinedif it isundefined
Undefined is not a valid value for the web data stream ID. We need to return false in this case.
I've made that adjustment, thanks @eugene-manuilov
IB :heavy_check_mark:
@eugene-manuilov, Just doing QA:eng here and I have a small point to clarify.
In the ACs it says:
If any error occurs during the above, it should not be returned by the function - the function should in that case simply return the success response (empty object) early.
I can see that you have returned the error in a few places, e.g.
https://github.com/google/site-kit-wp/blob/d153ac726542fd33c87d5d8c243bf550fb229558/assets/js/modules/analytics-4/datastore/settings.js#L45-L47
Is this an issue? Thanks in advance.
@johnPhillips yes, i think i need to correct this. Thanks for finding it. :+1:
Thanks for clarifying @eugene-manuilov - I'll assign the issue back to you for a follow up.
QA ✅