/** * Pushes Contact Form 7 submission data to the Piwik PRO data layer. * Fires on the native `wpcf7mailsent` event, which CF7 dispatches only * after a form has been validated and successfully sent (no AJAX * race condition, since CF7 submits without a page reload). * * Replaces the previously auto-generated `stg.formSubmit` event. */ document.addEventListener('wpcf7mailsent', (event) => { const form = event.target; const inputs = event.detail?.inputs ?? []; // Build a simple { fieldName: value } lookup from CF7's inputs array. const fields = inputs.reduce((acc, { name, value }) => { acc[name] = value; return acc; }, {}); window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event': 'form_submitted_successfully', 'formId': event.detail?.contactFormId ?? null, 'your-case-bu': fields['your-case-bu'] ?? null, 'your-interest': fields['your-interest'] ?? null, 'elementId': form?.id ?? '', 'elementClasses': form?.className ?? '', 'elementUrl': window.location.href, 'elementName': form?.getAttribute('name') ?? null, }); });
Passa al contenuto principale