Implement default Snippet Toggle behavior when there is an existing GTM property
See original GitHub issueFeature Description
We should implement logic (either in useExistingTagEffect or somewhere else) that turns on/off Snippet Toggle when there’s an existing GTM property. It should behave similarly to #4913.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
Some background information: The ACs here mostly update the Tag Manager enforcement behavior to be GA property-specific, rather than apply regardless of which GA property is used in Tag Manager. For the time being, we will keep using this approach, i.e. we will not incorporate the Tag Manager tag into a useExistingTagEffect-like handler. The useSnippet setting of GA should for now remain untouched in favor of sticking with the canUseSnippet filter control. This ensures that, if the Tag Manager module is deactivated again later, the Analytics snippet is placed again.
- The
googlesitekit_analytics_can_use_snippetfilter should be enhanced to receive the current UA property ID as second parameter.- In the case of the option default being used, it should simply pass an empty string since in that case no property ID is set.
- The Tag Manager usage of that filter should be adjusted to only set the value to
falseif the GA property in Tag Manager is set and is the same one as the GA property in the Analytics module (i.e. the one passed to the filter, see above). - Since this is now a bit more dynamic based on taking the current UA property ID into account, that needs to be reflected on the client-side as well. Therefore, the
analyticsstore’sgetCanUseSnippetselector should be re-implemented as an actual selector, rather than just using the generated selector that directly returns the value of thecanUseSnippetsetting.- It should first call
getSettingsto get the Analytics settings. - It should then check if the
tagmanagermodule is active and whether themodules/tagmanagerstore is loaded. - If it both conditions are true, it should call that store’s
getGAPropertyIDselector to get a potential GA property ID within Tag Manager, and if there is one, compare it to the current UApropertyIDin Analytics. If they match, it should returnfalse, otherwise it should returntrue. - If one of the conditions is false, it should just return the value from the
canUseSnippet.
- It should first call
- The Analytics
SetupUseSnippetSwitchcomponent should include the same check present in the module’sSettingsUseSnippetSwitchcomponent: IfcanUseSnippetisfalse, the same text should be displayed (The code is controlled by the Tag Manager module.). - Both the
SetupUseSnippetSwitchand theSettingsUseSnippetSwitchshould be updated to also display the toggle as off whencanUseSnippetisfalse, without modifying theuseSnippetsetting though. In other words,canUseSnippetoverridesuseSnippet, while the latter setting should remain unmodified. - The result from the changes above should be that the snippet toggle is always displayed as visually disabled, with the value as off, and accompanied by a corresponding text when there is a Tag Manager GA property that is the same as the selected UA property.
Implementation Brief
in Google\Site_Kit\Modules\Tag_Manager class:
- in the
can_analytics_use_snippetmethod:- Take
ua_propertyas the second param which is empty string by default. - Return
falseif thegaPropertyID === $ua_property_id - Return
original_valueotherwise.
- Take
- in the
registermethod:- Update the
add_actionhook call ofgooglesitekit_analytics_can_use_snippetto receive 2 arguments.- change
add_actiontoadd_filteras it’s a actually a filter. add_filter( 'googlesitekit_analytics_can_use_snippet', $this->get_method_proxy( 'can_analytics_use_snippet' ), 10, 2 );
- change
- Update the
In Google\Site_Kit\Modules\Analytics\Settings class:
- In the
registermethod:- Inside the callback of
default_option_*filter, pass empty string as the third parameter of googlesitekit_analytics_can_use_snippetfilter. ie.apply_filters( ‘googlesitekit_analytics_can_use_snippet’, true, ‘’ );` - Inside the callback of
option_*filter, passproperty_idas the third parameter of googlesitekit_analytics_can_use_snippetfilter. ie.apply_filters( ‘googlesitekit_analytics_can_use_snippet’, true, $property_id );`
- Inside the callback of
In assets/js/modules/analytics/datastore/base.js:
- Re implement
getCanUseSnippetas a REAL selector.- See
assets/js/modules/tagmanager/datastore/base.jsfor an example on how to override generated store selector. - Using
createRegistrySelectorfunction, redefine the getCanUseSnippet selector. - Check if tagmanager store is available by checking the value of
isModuleConnectedselector ofCORE_MODULESstore. - Check if the Tag Manager Snippet is enabled using the
useSnippetselector. - If Tag Manager is connected and it’s snippet is enabled, get the potential UA property within GTM using
getGAPropertyIDselector.- If it’s available and
isValidPropertyID, compare it with thepropertyIDof analytics module.- return false if both property IDs match.
- return true otherwise.
- If it’s available and
- Return the value of
canUseSnippetotherwise.
- See
In assets/js/modules/analytics/components/common/SetupUseSnippetSwitch.js:
- Get
canUseSnippetusing the newly reimplimented selector. - Return
nullfrom the component when there’s noexistingTagor the value ofcanUseSnippetisundefinedortrue - Render the
UseSnippetSwitchwith the same copy as used inSettingsUseSnippetof the same module whencanUseSnippet === false. - Update all usage of the component to be rendered unconditionally.
In assets/js/modules/analytics/components/common/UseSnippetSwitch.js:
- Update the
checkedprop of the switch component to befalsewhencanUseSnippet === false. ie.checked={ canUseSnippet === false ? false : useSnippet } - Update the
disabledprop to bedisabled={ canUseSnippet === false }
Test Coverage
- Update all the failing PHP and JS tests.
QA Brief
- Add an UA tag inside the Tag Manager Container.
- Make sure both Analytics and Tag Manager are Completely disconnected. (Incomplete setup will pose problems, ie. neither module should be in Connected Modules. Reset site kit if needed.)
- Attempt to setup Tag Manager with same container.
- See that There’s a
Continue to Analytics SetupBlue button and the following copy on top of the page.- Copy:
Looks like you’re already using Google Analytics within your Google Tag Manager configuration. Activate the Google Analytics module in Site Kit to see relevant insights in your dashboard.
- Copy:
- Click on the Blue CTA to continue to Analytics Setup.
- See that the AC is met there.
Changelog entry
- Update Analytics snippet toggle behavior to be disabled when the same property is set in the Tag Manager container.
Issue Analytics
- State:
- Created a year ago
- Comments:13 (5 by maintainers)

Top Related StackOverflow Question
@felixarntz I think if
canUseSnippetisfalse, the toggle should bedisabledand thevalueshould be off (indicating that the Analytics snippet will not be output). The important thing I think you’re getting at is that theuseSnippetsetting value in the store does not change as a result.That is, if
canUseSnippetisfalse, ifuseSnippetistrue, the toggle would still show as both disabled and toggled off, butuseSnippetin the store would be unchanged. Does that make sense?E.g.
@aaemnnosttv
Correct, that should still apply, good point. The GTM
useSnippetsetting has to betruefor Tag Manager to override the Analytics snippet. If it’sfalse, AnalyticsgetCanUseSnippetshould return the regulartrueno matter what.