From 194d1b8680dca941d7fc6cdfd56b3c455408121a Mon Sep 17 00:00:00 2001 From: Abdelmalek El Mellouki Date: Tue, 9 Sep 2025 19:19:04 +0100 Subject: [PATCH] Add mailchimp integration for ECEE. --- assets/js/event-providers/mailchimp.js | 55 +++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/assets/js/event-providers/mailchimp.js b/assets/js/event-providers/mailchimp.js index c07c46adb13..ff68ba16e95 100644 --- a/assets/js/event-providers/mailchimp.js +++ b/assets/js/event-providers/mailchimp.js @@ -14,14 +14,67 @@ * limitations under the License. */ +import { classifyPII, getUserData } from './utils'; + ( ( mc4wp ) => { if ( ! mc4wp ) { return; } - mc4wp.forms.on( 'subscribed', () => { + mc4wp.forms.on( 'subscribed', ( mc4wpForm, data ) => { + const gtagUserDataEnabled = global._googlesitekit?.gtagUserData; + + const userData = gtagUserDataEnabled + ? getUserDataFromForm( mc4wpForm.element, data ) + : null; + global._googlesitekit?.gtagEvent?.( 'submit_lead_form', { event_category: 'mailchimp', + ...( userData ? { user_data: userData } : {} ), } ); } ); } )( global.mc4wp ); + +/** + * Extracts and classifies user data from a Mailchimp form submission. + * + * @since n.e.x.t + * + * @param {HTMLFormElement} form The submitted form element. + * @param {Object} data The submitted form's data. + * @return {Object|undefined} A user_data object containing detected PII (address, email, phone_number), or undefined if no PII found. + */ +function getUserDataFromForm( form, data ) { + // eslint-disable-next-line sitekit/acronym-case + if ( ! form || ! ( form instanceof HTMLFormElement ) ) { + return undefined; + } + + const detectedFields = Object.entries( data ) + .map( ( [ name, value ] ) => { + // Mailchimp joins the individual name fields into a single field "NAME", + // but still provides the individual values in the data object, so we skip them. + if ( 'NAME' in data && ( name === 'FNAME' || name === 'LNAME' ) ) { + return null; + } + + const input = form.querySelector( `[name='${ name }']` ); + + const type = input?.type; + + const label = input?.id + ? form.querySelector( `label[for='${ input?.id }']` ) + ?.textContent + : undefined; + + return classifyPII( { + type, + label, + name, + value, + } ); + } ) + .filter( Boolean ); + + return getUserData( detectedFields ); +}