Allow Analytics to be removed as an available module without breaking Site Kit
See original GitHub issueBug Description
If Google Analytics has been disabled via googlesitekit_available_modules (https://github.com/google/site-kit-wp/issues/3993) ensure that the option to setup Analytics isn’t included for other admins who decide to set up the plugin.
Screenshots
Steps to reproduce
- Disable Analytics via the below filter
- Install and setup SK
- The option to add GA remains
Note that GA is never added to the setup steps, so the setup flow does complete successfully if a user proceeds. Google Analytics isn’t connected at any stage, and the option to connect doesn’t appear, nor for the setup CTAs.
add_filter(
'googlesitekit_available_modules',
function ( $modules ) {
return array( 'search-console' );
}
);
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Site Kit should not raise errors due to the Analytics module being programatically removed as an available module
- It is okay not to worry about potentially “weird” looking UI due to not showing certain Analytics information, since this is a special case that we are not going to specifically support through clean UI. In other words, e.g. the Analytics setup CTAs are perfectly fine to be replaced with nothing, i.e. simply white-space. The same applies to the Analytics notice on the splash screen, which should simply not show and therefore behave in a similar way as if Analytics was already active (in which case it already doesn’t show today).
Implementation Brief
As outlined in my comment below, I think the ACs need adjusting because they suggest a particular implementation, but in this case I think we can simplify the approach a fair bit. 🙂
The issue above stems from the fact that we’re using false-y checks for isModuleConnected
instead of explicitly checking for === false
. As @felixartnz mentioned below, rather than “simply” adding a === null
check we should add a new selector: isModuleAvailable( slug )
that allows us to check whether a module is at all available before selecting anything from its store or rendering data related to it.
-
Add a new selector to the
CORE_MODULES
data store:isModuleAvailable( slug )
. It should accept a module slug as the sole argument, and return:undefined
ifgetModule( slug ) === undefined
false
ifgetModule( slug ) === null
true
if neither of the above conditions are met
-
Adjust components to not output Analytics component when the module is not available. Here’s a list of places we’re checking for a false-y
isModuleConnected
where we should also use the newisModuleAvailable( slug )
selector before trying to select/render any module-related data: -
https://github.com/google/site-kit-wp/blob/d9956a60b82eed776a24c78f069ff2ee64c2bfec/assets/js/modules/search-console/components/dashboard/SearchFunnelWidget/index.js#L325-L341 (Update not to show CTAs when
isAnalyticsConnected === null
Everywhere else the sole isModuleConnected(slug)
(“false-y”) checks are accurate and can remain.
- We should also adjust places where we’re checking for
isModuleActive( slug )
and first checkisModuleAvailable( slug )
to ensure the module is available before we check if it’s active. This can be done everywhere we useisModuleActive()
Test Coverage
- Add tests to the setup file (and components that render CTAs) that check to see if the Analytics CTAs are output when Analytics is not available.
- If the datastore approach is taken, we should test to make sure we don’t make fetch requests when Analytics selectors with resolvers are called when Analytics is disabled.
QA Brief
- Add the filter below to your WordPress site, e.g. in your theme’s
functions.php
. - Set up Site Kit and ensure the Analytics CTA doesn’t show on the splash screen.
- Access the various Site Kit areas (main dashboard, entity dashboard, WP dashboard widget, admin bar menu), and ensure there are no errors due to Analytics not being available.
- Check the Tag Manager module still works.
- Note that the Optimize module will also be removed due to its dependence on Analytics.
Filter
add_filter( 'googlesitekit_available_modules', function ( $modules ) {
return array_diff( $modules, array( 'analytics' ) );
} );
Changelog entry
- Decouple core and modules from Analytics module availability.
Issue Analytics
- State:
- Created a year ago
- Comments:9 (3 by maintainers)
@tofumatt IB mostly looks good, a few small notes:
It strikes me a bit odd to rely on
isModuleConnected
here. This is simply about whether the module exists, and while I’m all for reusing code, I think usinggetModule
here instead better represents its purpose.isModuleConnected
also checks something else in addition to whether the module exists, we essentially don’t care about itstrue
vsfalse
state. Hence relying ongetModule
here is sufficient and a bit closer to the purpose.I think we may want to also reference any false-y
isModuleActive
selector calls here, since that can also apply in a few places. Like anywhere that we render some sort of CTA for Analytics when it’s not active, we should also not render such a thing, i.e. add theisModuleAvailable
check to the condition.QA Update ✅
https://user-images.githubusercontent.com/94359491/193585244-0f6cd10f-d8e6-4b1e-b106-c5eabcbd4d8c.mp4