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 ( ); }; const renderForm = () => { return (
<> { translate( 'Google Analytics' ) }

{ translate( 'A free analytics tool that offers additional insights into your site.' ) }{ ' ' } { translate( 'Learn more' ) }

{ displayForm && (
{ translate( 'Google Analytics Measurement ID', { context: 'site setting' } ) } { ! isCodeValid && ( ) } { translate( 'Where can I find my Measurement ID?' ) } { translate( 'Enabling this option is mandatory in certain countries due to national ' + 'privacy laws.' ) } { translate( 'Learn more' ) } { wooCommerceActive && ( ) }

{ translate( 'Google Analytics is a free service that complements our {{a}}built-in stats{{/a}} ' + 'with different insights into your traffic. Jetpack Stats and Google Analytics ' + 'use different methods to identify and track activity on your site, so they will ' + 'normally show slightly different totals for your visits, views, etc.', { components: { a: , }, } ) }

{ translate( 'Learn more about using {{a}}Google Analytics with WordPress.com{{/a}}.', { components: { a: ( ), }, } ) }

) } { showUpgradeNudge && site && site.plan ? ( ) : ( <>
{ isJetpackModuleAvailable ? ( ) : ( renderSettingsToggle() ) }
) } ); }; // we need to check that site has loaded first... a placeholder would be better, // but returning null is better than a fatal error for now if ( ! site ) { return null; } return { renderForm() }; }; export default GoogleAnalyticsJetpackForm;