/**
 * Analytics Use Snippet Switch component.
 *
 * Site Kit by Google, Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import PropTypes from 'prop-types';

/**
 * WordPress dependencies
 */
import { useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies
 */
import Data from 'googlesitekit-data';
import { MODULES_ANALYTICS } from '../../datastore/constants';
import Switch from '../../../../components/Switch';
import { trackEvent } from '../../../../util';
import useViewContext from '../../../../hooks/useViewContext';
const { useSelect, useDispatch } = Data;

export default function UseSnippetSwitch( { description } ) {
	const viewContext = useViewContext();
	const useSnippet = useSelect( ( select ) =>
		select( MODULES_ANALYTICS ).getUseSnippet()
	);
	const canUseSnippet = useSelect( ( select ) =>
		select( MODULES_ANALYTICS ).getCanUseSnippet()
	);
	const hasExistingTag = useSelect( ( select ) =>
		select( MODULES_ANALYTICS ).hasExistingTag()
	);

	const { setUseSnippet } = useDispatch( MODULES_ANALYTICS );
	const onChange = useCallback( () => {
		const newUseSnippet = ! useSnippet;
		setUseSnippet( newUseSnippet );
		trackEvent(
			`${ viewContext }_analytics`,
			newUseSnippet ? 'enable_tag' : 'disable_tag',
			'ua'
		);
	}, [ useSnippet, setUseSnippet, viewContext ] );

	if ( useSnippet === undefined || hasExistingTag === undefined ) {
		return null;
	}

	return (
		<div className="googlesitekit-analytics-usesnippet">
			<Switch
				label={ __(
					'Place Universal Analytics code',
					'google-site-kit'
				) }
				checked={ useSnippet }
				onClick={ onChange }
				hideLabel={ false }
				disabled={ ! canUseSnippet }
			/>
			{ description }
		</div>
	);
}

UseSnippetSwitch.propTypes = {
	description: PropTypes.node,
};

@@ -2179,16 +2179,50 @@
 hecked=%7B
+ canUseSnippet === false ? false :
  useSnip
@@ -2290,18 +2290,16 @@
 sabled=%7B
- !
  canUseS
@@ -2301,24 +2301,34 @@
 nUseSnippet 
+=== false 
 %7D%0A%09%09%09/%3E%0A%09%09%09%7B

Update UseSnippetSwitch component of analytics module.