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

Implement submitChanges and validateCanSubmitChanges logic for GA4 store

Open felixarntz opened this issue 5 years ago • 7 comments

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-4 store should receive its own implementation of submitChanges and validateCanSubmitChanges (via the createModuleStore call), with these functions themselves being implemented in assets/js/modules/analytics-4/datastore/settings.js (similar to how it is for the modules/analytics store). Both of the new function implementations should follow the same patterns used in the similar functions in the modules/analytics store.
  • The submitChanges function should do the following:
    1. If the store's getPropertyID is PROPERTY_CREATE, a new GA4 property should be created using the createProperty action. Afterwards, the new property should be set.
    2. If the store's getWebDataStreamID is not valid (a new utility function isValidWebDataStreamID should be added to assets/js/modules/analytics-4/utils/validation.js for this purpose), a new GA4 web data stream should be created using the createWebDataStream action. Afterwards, the new web data stream ID and measurement ID from it should be set.
    3. Then, if settings have changed, the changes should be saved using saveSettings, and on success the analytics-4 cache should be invalidated (no need to include the legacy data API cache here, since it isn't used for any analytics-4 API requests).
    4. 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.
  • The validateCanSubmitChanges function should do the following:
    1. 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.
    2. Fail if the store currently isDoingSubmitChanges.
    3. Fail if for the store ! haveSettingsChanged.
    4. Fail if the store's getPropertyID is not a valid GA4 property selection.

Implementation Brief

Overview

  • Create a new 'settings' file in assets/js/modules/analytics-4/datastore/
  • Use the settings store in the Analytics module as a guide
  • The settings store should export two functions: submitChanges and validateCanSubmitChanges
  • We will also need a new utility, isValidWebDataStreamID, which should be added to assets/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 true if it is
  • Should return false if it is not numeric
  • Should return false if it is undefined.
  • 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 propertyID from the store using the getPropertyID selector
  • If the propertyID is PROPERTY_CREATE, then a new GA4 property should be created using the createProperty action. Afterwards, the new property should be set.
  • Select the webDataStreamID from the store
  • Run it through the isValidWebDataStreamID utility, and if it is not valid, create a new GA4 web data stream using the createWebDataStream action. Then set the new web data stream ID and measurement ID in the store using setWebDataStreamID and setMeasurementID.
  • Determine if the settings have changed using haveSettingsChanged, and if so, the changes should be saved using saveSettings.
  • 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 isAdminAPIWorking and return early if this is false.
  • Return false / fail if any of the following criteria are met (consider using strictSelect for this, an example can be found in the Analytics validateCanSubmitChanges implementation):
    • the store currently isDoingSubmitChanges.
    • ! haveSettingsChanged.
      • If the store's getPropertyID is not a valid GA4 property selection (use isValidPropertySelection).

Add to the store

  • Import the two new functions into the Analytics-4 base store, and add them to the Modules.createModuleStore call (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 validateCanSubmitChanges and submitChanges functions in the analytics-4/datastore/settings.js file. Make sure those functions satisfy requirements from the AC.

Changelog entry

  • Add change validation logic to Google Analytics 4 data store.

felixarntz avatar Apr 28 '21 23:04 felixarntz

@johnPhillips IB mostly looks good, there is just one thing that we need to adjust:

isValidWebDataStreamID utility Should return undefined if it is undefined

Undefined is not a valid value for the web data stream ID. We need to return false in this case.

eugene-manuilov avatar May 04 '21 10:05 eugene-manuilov

I've made that adjustment, thanks @eugene-manuilov

johnPhillips avatar May 04 '21 10:05 johnPhillips

IB :heavy_check_mark:

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

@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 avatar May 14 '21 13:05 johnPhillips

@johnPhillips yes, i think i need to correct this. Thanks for finding it. :+1:

eugene-manuilov avatar May 14 '21 13:05 eugene-manuilov

Thanks for clarifying @eugene-manuilov - I'll assign the issue back to you for a follow up.

johnPhillips avatar May 14 '21 13:05 johnPhillips

QA ✅

ivankruchkoff avatar May 18 '21 20:05 ivankruchkoff