diff --git a/assets/js/mailchimp.js b/assets/js/mailchimp.js index b9a6d6ec..0dc85a95 100644 --- a/assets/js/mailchimp.js +++ b/assets/js/mailchimp.js @@ -121,12 +121,18 @@ for (let i = 0; i < forms.length; i++) { const listId = forms[i].getAttribute('data-list-id'); - if (listId && !tracked[listId]) { - tracked[listId] = true; + const formId = forms[i].getAttribute('data-form-id') || ''; + const key = `${listId}|${formId}`; + + if (listId && !tracked[key]) { + tracked[key] = true; const formData = new FormData(); formData.append('action', 'mailchimp_sf_track_form_view'); formData.append('list_id', listId); + if (formId) { + formData.append('form_id', formId); + } formData.append('mailchimp_sf_nonce', window.mailchimpSF.analytics_nonce); fetch(window.mailchimpSF.analytics_ajax_url, { diff --git a/includes/blocks/mailchimp/block.json b/includes/blocks/mailchimp/block.json index c5049bed..dbb07679 100644 --- a/includes/blocks/mailchimp/block.json +++ b/includes/blocks/mailchimp/block.json @@ -6,6 +6,14 @@ "category": "widgets", "description": "Mailchimp List Subscribe Form", "attributes": { + "formId": { + "type": "string", + "default": "" + }, + "formTitle": { + "type": "string", + "default": "" + }, "list_id": { "type": "string" }, diff --git a/includes/blocks/mailchimp/edit.js b/includes/blocks/mailchimp/edit.js index aaa97b87..f95c4ba1 100644 --- a/includes/blocks/mailchimp/edit.js +++ b/includes/blocks/mailchimp/edit.js @@ -10,6 +10,7 @@ import { PanelBody, ToggleControl, SelectControl, + TextControl, Spinner, Placeholder, } from '@wordpress/components'; @@ -61,8 +62,17 @@ export const BlockEdit = (props) => { show_required_indicator = true, required_indicator_text, template = 'default', + formTitle = '', } = attributes; + // Give every block instance a stable analytics ID. Generated once when + // empty and persisted with the post, so it survives edits and reordering. + useEffect(() => { + if (!attributes.formId && typeof window.crypto?.randomUUID === 'function') { + setAttributes({ formId: window.crypto.randomUUID() }); + } + }, []); // eslint-disable-line react-hooks/exhaustive-deps -- Only run on mount. + const [listData, setListData] = useState({}); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(''); @@ -417,6 +427,20 @@ export const BlockEdit = (props) => { __nextHasNoMarginBottom /> + + setAttributes({ formTitle: value })} + help={__( + 'Used to identify this form in the Analytics dashboard. Defaults to the form header if left blank.', + 'mailchimp', + )} + __nextHasNoMarginBottom + /> + get_lists(); - $list_ids = array_map( + $list_id = $attributes['list_id'] ?? ''; + $form_id = wp_unique_id( $list_id . '_' ); + + // Stable per-form analytics id from the block attribute. Empty for legacy + // blocks not yet re-saved + $mc_analytics_form_id = Mailchimp_Forms_Registry::sanitize_form_id( $attributes['formId'] ?? '' ); + $lists = ( new Mailchimp_List_Subscribe_Form_Blocks() )->get_lists(); + $list_ids = array_map( function ( $single_list ) { return $single_list['id']; }, @@ -105,8 +109,11 @@ function ( $single_list ) { } ?>
-