My data layer push script is not working. I am applying this to a page built on Hubspot CMS using a regular Hubspot form. The issue that I am running into is that console continues to return “HubSpot forms library not found.”
This happens before the form is submitted. I’ve checked any other related errors and nothing seems to correlate. I checked the network and it seems to send a request to hsforms.net
Any idea what might be wrong here?
<script>
// Ensure the data layer exists
window.dataLayer = window.dataLayer || [];
// Function to execute after form submission
function onFormSubmitted(event) {
// Replace selectors with the correct ones for your form
var email = document.querySelector(‘[id^=“email-”]’).value;
var phone = document.querySelector(‘[id^=“mobilephone-”]’).value;
dataLayer.push({
‘event’: ‘hubspotFormSubmit’,
‘email’: email,
‘phoneNumber’: phone
});
}
// Check if the HubSpot form library is available
if (window.hbspt && window.hbspt.forms) {
window.hbspt.forms.on(‘submitSuccess’, onFormSubmitted);
} else {
// Consider other ways to detect form submission if necessary
console.log(‘HubSpot forms library not found.’);
}
</script>