import {
FEATURE_GOOGLE_ANALYTICS,
PLAN_JETPACK_SECURITY_DAILY,
} from '@automattic/calypso-products';
import {
FormInputValidation as FormTextValidation,
FormLabel,
Button,
ExternalLink,
} from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import { ToggleControl } from '@wordpress/components';
import { find } from 'lodash';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import googleIllustration from 'calypso/assets/images/illustrations/google-analytics-logo.svg';
import UpsellNudge from 'calypso/blocks/upsell-nudge';
import QueryJetpackModules from 'calypso/components/data/query-jetpack-modules';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import FormTextInput from 'calypso/components/forms/form-text-input';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { PanelCard, PanelCardHeading } from 'calypso/components/panel';
import SupportInfo from 'calypso/components/support-info';
import { PRODUCT_UPSELLS_BY_FEATURE } from 'calypso/my-sites/plans/jetpack-plans/constants';
import JetpackModuleToggle from 'calypso/my-sites/site-settings/jetpack-module-toggle';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { requestSiteSettings } from 'calypso/state/site-settings/actions';
import { getSiteAdminUrl } from 'calypso/state/sites/selectors';
import FormAnalyticsStores from '../form-analytics-stores';
import './style.scss';
const GoogleAnalyticsJetpackForm = ( {
displayForm,
enableForm,
fields,
handleCodeChange,
handleFieldChange,
handleFieldFocus,
handleFieldKeypress,
handleSubmitForm,
isCodeValid,
isRequestingSettings,
isSavingSettings,
isSubmitButtonDisabled,
jetpackModuleActive,
path,
placeholderText,
recordSupportLinkClick,
setDisplayForm,
showUpgradeNudge,
site,
siteId,
sitePlugins,
translate,
isAtomic,
isJetpackModuleAvailable,
} ) => {
const upsellHref = `/checkout/${ site.slug }/${ PRODUCT_UPSELLS_BY_FEATURE[ FEATURE_GOOGLE_ANALYTICS ] }`;
const analyticsSupportUrl = isAtomic
? localizeUrl( 'https://wordpress.com/support/google-analytics/' )
: 'https://jetpack.com/support/google-analytics/';
const nudgeTitle = translate( 'Connect your site to Google Analytics' );
// TODO: it would be better to get wooCommercePlugin directly in form-google-analytics using getAllPluginsIndexedByPluginSlug
const wooCommercePlugin = find( sitePlugins, { slug: 'woocommerce' } );
const wooCommerceActive = wooCommercePlugin ? wooCommercePlugin.sites[ siteId ].active : false;
const dispatch = useDispatch();
const trackTracksEvent = ( name, props ) => dispatch( recordTracksEvent( name, props ) );
const statsUrl = useSelector( ( state ) =>
site.options?.is_wpcom_atomic
? getSiteAdminUrl( state, siteId, 'admin.php?page=stats' )
: '/stats/' + site.domain
);
useEffect( () => {
// Show the form if GA module is active, or it's been removed but GA is activated via the Legacy Plugin.
if ( jetpackModuleActive || ( ! isJetpackModuleAvailable && fields?.wga?.is_active ) ) {
setDisplayForm( true );
} else {
setDisplayForm( false );
}
}, [ jetpackModuleActive, setDisplayForm, isJetpackModuleAvailable, fields?.wga?.is_active ] );
useEffect( () => {
if ( jetpackModuleActive && ! fields.hasOwnProperty( 'wga' ) ) {
dispatch( requestSiteSettings( siteId ) );
}
}, [ jetpackModuleActive, siteId ] );
const handleToggleChange = ( key ) => {
const value = fields.wga ? ! fields.wga[ key ] : false;
trackTracksEvent( 'calypso_google_analytics_setting_changed', { key, path } );
handleFieldChange( key, value );
};
const handleAnonymizeChange = () => {
handleToggleChange( 'anonymize_ip' );
};
const handleSubmitFormCustom = () => {
if ( isJetpackModuleAvailable ) {
handleFieldChange( 'is_active', !! jetpackModuleActive, handleSubmitForm );
return;
}
handleSubmitForm();
};
const trackActiveToggle = () => {
trackTracksEvent( 'calypso_google_analytics_setting_changed', { key: 'is_active', path } );
};
const handleSettingsToggleChange = ( value ) => {
trackActiveToggle();
handleFieldChange( 'is_active', value, handleSubmitForm );
};
const renderSettingsToggle = () => {
return (