JS errors if Tag Manager is disabled via googlesitekit_available_modules
See original GitHub issueBug Description
Similar to #5072, but for covering the case that the Tag Manager module is not available (e.g. via googlesitekit_available_modules
filter): There will be JS errors at least in the Analytics setup as it unconditionally calls modules/tagmanager
selectors.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Any references to
modules/tagmanager
store selectors or actions from a component or store that is part of another module should only occur after verifying that the module is available (available, regardless of whether it is active or not).- In other words, this issue should cover all accesses within
assets/js/modules/*
other thanassets/js/modules/tagmanager
. - A good way to tackle this is to search for any
MODULES_TAGMANAGER
references in those other module directories and adjust/wrap them accordingly. - At a minimum, the check needs to ensure that the respective
MODULES_TAGMANAGER
datastore is available before calling a selector or action on it.
- In other words, this issue should cover all accesses within
Implementation Brief
- Find all instances of
select( MODULES_TAGMANAGER )
/dispatch( MODULES_TAGMANAGER )
and ensure thatisModuleAvailable( MODULES_TAGMANAGER )
istrue
before using those selectors/dispatching those actions. - Find instances of
isModuleActive( MODULES_TAGMANAGER )
and check forisModuleAvailable( MODULES_TAGMANAGER )
.
Note: any code in any module should be checked, not just Tag Manager specific code. Be sure to look for Tag Manager selectors/action dispatches across the whole plugin when making these changes.
Test Coverage
- Add tests to any components with existing tests that disable Tag Manager; ensure they still render without errors/console warnings.
QA Brief
- Add the filter below to your WordPress site, e.g. in your theme’s
functions.php
. - Set up the Analytics module and ensure there are no errors due to Tag Manager not being available.
Filter
add_filter(
'googlesitekit_available_modules',
function( $modules ) {
$index = array_search( 'tagmanager', $modules, true );
if ( false !== $index ) {
unset( $modules[ $index ] );
return array_values( $modules );
}
return $modules;
}
);
Changelog entry
- Fix bug that caused errors to appear in Analytics module when Tag Manager module is not available.
Issue Analytics
- State:
- Created a year ago
- Comments:6
Top Results From Across the Web
JS errors if Analytics is disabled via ...
Bug Description Now that we have the googlesitekit_available_modules filter available (#3993) some administrators may run into various ...
Read more >Google Tag Manager Errors - Tag Assistant Help
Invalid or missing account ID. Data layer fields should be quoted. Consolidate your tags with Google Tag Manager. Code found outside of <body>...
Read more >Google Tag Manager 404 error (gtm.js 404)
A quick solution to Google Tag Manager 404 error (gtm.js 404), also known as 404 not found for GET: GET ...
Read more >Disable google tag manager according to the decision of ...
My understanding is that the <noscript> tag is present for cases where Javascript is disabled. Then the tracking will be done via an...
Read more >Use Tag Manager with a Content Security Policy
Enable the container tag to use CSP; Custom JavaScript Variables ... Then use the nonce-aware version of the inline Tag Manager container code....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@hussain-t I see this was assigned a month ago before the IB/approach in #5071 was finalised. Since I wrote up that IB I’m going to handle this and #5072, because there’s a lot of overlap 🙂
QA Update ✅
https://user-images.githubusercontent.com/94359491/199218497-59bdd92f-7bef-4b13-9c96-0e87a969d4f2.mp4