site-kit-wp
site-kit-wp copied to clipboard
Refactor Key Metric display logic for selection panel, widget area and view only dashboard
Bug Description
In #7741, we introduced logic for view only users that filters out key metrics which require custom dimensions, but now those custom dimensions are unavailable (since view only users can't do much about this).
We are now directly calling displayInList() in the getKeyMetricSettings selector for view-only users which has side-effects when the displayInList implementation contains calls to check if isKeyMetricActive(), like in the case of Most Popular Products and for widgets that use shouldDisplayWidgetWithConversionEvent(). It causes a infinite recursion as seen with the calls below:
getKeyMetrics()
getUserPickedMetrics()
getKeyMetricsSettings()
displayInList() // for "Most popular products" and widgets where shouldDisplayWidgetWithConversionEvent() is used
isKeyMetricActive()
getKeyMetrics()
However, this logic was introduced in a central getKeyMetricsSettings() selector even though the main purpose here was to filter out only userPickedMetrics, i.e. metrics the user had once saved but they no longer can benefit from since the custom dimensions required have been deleted or modified. So this should be thoughtfully refactored into the getUserPickedMetrics selector or somewhere more suitable.
https://github.com/google/site-kit-wp/blob/8a891c49d336264431477571d21bca63ebf09f9b/assets/js/googlesitekit/datastore/user/key-metrics.js#L467-L501
We do have a clear separation of concerns when it comes to logic:
- that checks if a key metric should be displayed in the metrics selection panel - displayInList()
- that checks if a key metric should be displayed within the widget area - isKeyMetricActive()
But we have created a slight overlap in some places to hack things a certain way which makes things redundant and confusing. This can be re-visited and cleaned up.
Steps to reproduce
- Activate the Woo Commerce plugin on any wordpress site.
- Setup Site Kit, connect Analytics and set up Key Metrics as an Admin.
- Share Analytics using Dashboard Sharing.
- Now login as a view only user and in the Key Metrics widget, select
Pick your own metrics. - Select the
Most popular products by pageviewswidget is in the list. - See the error
Screen recording
https://github.com/user-attachments/assets/ffbaa75e-5216-40c7-b66d-8c590972ccc5
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- View only users should not see any errors when selecting ANY Key Metric widget tile.
- These specifically include the
Most popular products by pageviewswidget tile, widget tiles that require Custom Dimensions and the new ACR metric tiles.
- These specifically include the
- The
displayInList()callback function is not used for purposes other than displaying a key metric widget tile in the Metrics Selection panel.
Implementation Brief
Fixing the bug
-
In
assets/js/components/KeyMetrics/key-metrics-widgets.js:- [ ] Create a new
displayInWidgetAreacallback property for all the key metrics that have arequiredCustomDimension. Set this property to call theshouldDisplayWidgetWithCustomDimensions()function. In the future, this callback can be used by other tiles to add any additional requirements for widgets to be rendered in the widget area which would further override the "user picked" or "answer based" selection.
- [ ] Create a new
-
In
assets/js/googlesitekit/datastore/user/key-metrics.js, :- [ ] Within
getKeyMetricSettings()selector: Remove the wholeisViewOnlycheck and snippet to filter out metrics. This will now be handled within thegetUserPickedMetricsbelow. - [ ] Within
getUserPickedMetrics()selector: Filter thewidgetSlugsresult. Using theKEY_METRICS_WIDGETSdata, check if the widget has adisplayInWidgetAreaproperty and use the boolean function for filtering. Pass theisViewOnlyDashboardvalue.
- [ ] Within
Generic Refactoring
-
[ ] Rename the
displayInListproperty todisplayInSelectionPanelfor more clarity as "list" is too generic. -
[ ] In
getAnswerBasedMetricsselector: Use a new selector for fetching theshowConversionTailoredMetrics: https://github.com/google/site-kit-wp/blob/e1ecae073df4dc7f812bc8fccbe0868ee0ec4599/assets/js/googlesitekit/datastore/user/key-metrics.js#L287-L292 -
[ ] In
getKeyMetricsselector: Remove unnecessary filtering of answer based metrics for ACR feature flag as this is already done within thegetAnswerBasedMetricsselector. https://github.com/google/site-kit-wp/blob/e1ecae073df4dc7f812bc8fccbe0868ee0ec4599/assets/js/googlesitekit/datastore/user/key-metrics.js#L242-L250 -
[ ] In
getKeyMetricsselector: Move the filtering ofuserPickedMetricsfor the ACR feature flag to a callback function that can be added to the newdisplayInWidgetAreaproperty of all the new ACR metrics inkey-metrics-widgets. Remove thekeyMetricsGA4WidgetsNonACRconstant if not used. https://github.com/google/site-kit-wp/blob/e1ecae073df4dc7f812bc8fccbe0868ee0ec4599/assets/js/googlesitekit/datastore/user/key-metrics.js#L227-L231
Test Coverage
No new tests needed.
QA Brief
Changelog entry
ACs here are good, and thanks for the great explanation/context for the issue 👍🏻
Moving to IB 🙂
IB ✅
QA Update ✅
- Tested on dev environment.
- Verified that issue error is not showing on 'View only dashboard' when user selects 'Most popular products by pageviews' KM tile
- Verified KM and user input functionality when 'conversionInfra' feature flag is enabled.
- Verified KM and user input functionality when 'conversionInfra' feature flag is not enabled.
- Verified Top earning pages KM tile related scenarios.
Top earning pages KM tile related scenarios
Top earning pages KM tile when AdSense is disconnected
Top earning pages KM tile when both Analytics and AdSense are disconnected
Top earning pages KM tile when Analytics is disconnected
If AdSense or Analytics module gets disconnect later and 'Top earning pages' KM tile is already selected
https://github.com/user-attachments/assets/7995b22f-2aa0-4118-a1f7-d8bf6fc0b230
Key metric complete flow when 'conversionInfra' feature flag is not enabled
https://github.com/user-attachments/assets/55fd461b-81a9-456e-bedb-0f455289996e
https://github.com/user-attachments/assets/4dd370dc-737a-4f4f-ad05-719e956fdaa4
https://github.com/user-attachments/assets/8d980373-3664-4929-8d16-cc0629674c65
Key metric complete flow when 'conversionInfra' feature flag is enabled
https://github.com/user-attachments/assets/26a8ebfb-061b-457a-8a4e-abb0290d32a5
https://github.com/user-attachments/assets/20a982f1-ed9b-4c21-8dd2-a806361ad2ee
https://github.com/user-attachments/assets/cf0d6a0c-c4dc-4cba-99fc-3995f9dbbdf9
View only dashboard when user selects 'Most popular products by pageviews' KM tile
https://github.com/user-attachments/assets/f1898747-573d-41b0-9ddf-a88f365276e9