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

Implement dropdown component showing all UA and GA4 properties

Open felixarntz opened this issue 5 years ago • 10 comments

A new Analytics component PropertySelectIncludingGA4 should be implemented in the analytics module which lists all Analytics properties the user has in the current account, of both UA and GA4 type. This component will be used in the "ga4-transitional" flow (see #3170) to list all of the account's UA and GA4 properties.


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

Acceptance criteria

  • A new component PropertySelectIncludingGA4 should be implemented within assets/js/modules/analytics/components/common.
  • It should render a dropdown with similar appearance and high-level behavior as the existing PropertySelect, with the difference that it includes all UA properties and all GA4 properties in the current account (relying on the getPropertiesIncludingGA4 selector from #3167). For example:
    • If no valid account ID is specified yet, the component should return null (also see related #3243).
    • If properties are still loading, the component should return a loading bar.
    • Every property should show up as its name with its ID in parentheses.
  • Since getPropertiesIncludingGA4 returns objects of different shape, this has to be catered for. Each object is either a UA property or a GA4 property, and they have their respective names and IDs in different fields (check the Analytics PropertySelect and the GA4 PropertySelect components for reference).
  • The currently selected property in the dropdown is determined as follows:
    • If the getPrimaryPropertyType selector returns "ga4", the selected property has to come from the modules/analytics-4 store's getPropertyID selector.
    • Otherwise or if the getPrimaryPropertyType selector returns "ua", the selected property has to come from the modules/analytics store's getPropertyID selector.
  • When selecting a property from the dropdown, the following should happen:
    • If the selected property is a GA4 property, the setPrimaryPropertyType action should be called to indicate "ga4". Then, the modules/analytics-4 store's selectProperty action should be used to set the GA4 property, while the modules/analytics store's selectProperty action should be used to empty/reset the UA property (since that then needs to later be chosen in another dropdown based on the GA4 property).
    • Otherwise or if the selected property is a UA property, the setPrimaryPropertyType action should be called to indicate "ua". Then, the modules/analytics store's selectProperty action should be used to set the UA property, while the modules/analytics-4 store's selectProperty action should be used to empty/reset the GA4 property (since that then needs to later be chosen in another dropdown based on the GA4 property).
  • Storybook coverage for the new component should be added, with data including both UA and GA4 properties.

Implementation Brief

This will involve taking code from the GA and GA4 PropertySelects (currently in progress on https://github.com/google/site-kit-wp/issues/3166), and also the changes for rending null in https://github.com/google/site-kit-wp/issues/3243

As mentioned in the AC, the changes will be using the selector getPropertiesIncludingGA4, and using the code for rendering the <Option> from both the UA and GA4 PropertySelect's (switching based on the property type - check for keys _id vs id e.g. https://github.com/google/site-kit-wp/blob/develop/assets/js/modules/analytics/datastore/properties.js#L484)

Pay attention to the rules in the AC about which property to show selected, and how to set that property (the AC is very descriptive)

Create new storybook stories for this, copying the existing UA and GA4 PropertySelect stories

Test Coverage

Using the existing tests for assets/js/modules/analytics-4/components/common/PropertySelect.test.js and assets/js/modules/analytics/components/common/PropertySelect.test.js, the new tests will have all these tests (selecting an option, displaying an option, rendering a list of options) with both UA and GA4 fixtures.

Select a UA property, select a GA4 property, etc

Visual Regression Changes

The PR for Implement GA4 PropertySelect component in JS https://github.com/google/site-kit-wp/issues/3166 has no extra VRTs, so none are needed for this.

QA Brief

  • Observe story https://google.github.io/site-kit-wp/storybook/pull/3383/?path=/story/analytics-module--property-select-including-ga4-properties "Property Select including GA4 properties" in "Analytics Module". It shows GA4 and UA properties (and both can be selected)

Changelog entry

  • Add Analytics property select component for showing all UA and GA4 properties.

felixarntz avatar Apr 29 '21 00:04 felixarntz

@danielgent I will approve this IB but next time could you please elaborate a little bit more on details? Try to give more clear details of what needs to be done and what to use.

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

Also, I am going to bump the estimate to 11 since 7 seems to be low for me (knowing that this ticket involves js tests and storybook changes).

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

IB :heavy_check_mark:

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

@eugene-manuilov I was struggling to explain this further without just doing the PR!! :smile: As it involves copying, pasting, and slightly modifying existing code (even deciding whether a property is GA4 or UA can be taken from my already-existing code)

Honestly I was thinking even less than 7 as it involves copying and pasting from existing files :see_no_evil: (happy to be proved wrong though!!! I've estimated way way under before)

danielgent avatar May 04 '21 12:05 danielgent

Just a head's up that these two requirements involve changing the code in ways that are out the scope of this ticket @felixarntz @eugene-manuilov

  • the modules/analytics store's selectProperty action should be used to empty/reset the UA property
  • the modules/analytics-4 store's selectProperty action should be used to empty/reset the GA4 property

For both stores, selectProperty requires a valid ID e.g.

selectProperty( propertyID, internalPropertyID = '' ) {
     invariant( isValidPropertySelection( propertyID ), 'A valid propertyID selection is required.' );

So I can't empty/reset them!

I tried removing these invariants but then other things started to explode: see my commit here (which fails tests and linting) https://github.com/google/site-kit-wp/commit/3b207af7229daf04ce54d48612e90373fb6a0363

Currently it fails when setting the GA4 propertyID to null. I'm not going to investigate further as this is an error with the @material library, and I already know of one existing bug that we have with these :smile:

  ● PropertySelectIncludingGA4 › should update propertyID in the UA store when a new UA item is selected

    TypeError: Cannot read property 'length' of null

      193 | 
      194 |             // "length null error" is something inside @material component
    > 195 |             fireEvent.click( getAllByRole( 'menuitem', { hidden: true } )[ 7 ] );
          |                       ^
      196 | 
      197 |             expect( registry.select( MODULES_ANALYTICS ).getPrimaryPropertyType() ).toBe( 'ua' );
      198 | 

      at e.foundation [as handleChange] (node_modules/@material/react-select/dist/webpack:/packages/select/node_modules/@material/select/foundation.js:140:36)
      at e.componentDidUpdate (node_modules/@material/react-select/dist/webpack:/packages/select/index.tsx:152:18)
      at commitLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:22117:22)
      ```

danielgent avatar May 18 '21 15:05 danielgent

@danielgent let's use the following instead of changing selectProperty selectors:

// for the analytics module:
registry.dispatch( MODLUES_ANALYTICS ).setPropertyID( '' );
registry.dispatch( MODLUES_ANALYTICS ).setInternalWebPropertyID( '' );
registry.dispatch( MODLUES_ANALYTICS ).setProfileID( '' );

// for the analytics-4 module:
registry.dispatch( MODLUES_ANALYTICS_4 ).setPropertyID( '' );
registry.dispatch( MODLUES_ANALYTICS_4 ).setWebDataStreamID( '' );
registry.dispatch( MODLUES_ANALYTICS_4 ).setMeasurementID( '' );

eugene-manuilov avatar May 18 '21 17:05 eugene-manuilov

Thanks for the suggestion @eugene-manuilov

I did try this though (copying the empty string passed to setWebDataStreamID) but sadly it doesn't work

the setPropertyID have invariants that call isValidPropertySelection which calls isValidPropertyID

export function isValidPropertyID( propertyID ) {
	return typeof propertyID === 'string' && /^UA-\d+-\d+$/.test( propertyID );
}

Which does not match empty strings :smile:

egex

danielgent avatar May 18 '21 18:05 danielgent

@danielgent how come it doesn't work :thinking:? Those actions are autogenerated and the only thing they validate is that the incoming value doesn't equal undefined: https://github.com/google/site-kit-wp/blob/1181d49b512d3fb79ebd6ff858a96db32197fd12/assets/js/googlesitekit/data/create-settings-store.js#L302-L309

Can you try again or show me an example of what you are doing?

eugene-manuilov avatar May 18 '21 18:05 eugene-manuilov

Apologies @eugene-manuilov

I'd misread :facepalm:

What you wrote works perfectly! Thanks :+1:

danielgent avatar May 18 '21 19:05 danielgent

QA ✅

Double checked the code and verified that the story works as described in the QAB.

johnPhillips avatar May 26 '21 11:05 johnPhillips