Help Needed: DataLayer Not Capturing Email for Enhanced Conversions

I’m trying to implement Google Ads Enhanced Conversion Tracking for a HubSpot form submission. Still, I’m facing an issue where the dataLayer fails to pass the user’s email address after a successful form submission. Here’s what we did:
On the thank-you page, we placed this code snippet to push the enhanced conversion data:
<script>
dataLayer.push({
‘event’: ‘ec_formsubmit’,
‘enhanced_conversion_data’: {
“email”: ‘email’
}
})
</script>
When we test the form and inspect the dataLayer in the browser console on the thank-you page, the “email” value appears as a literal string ‘email’ instead of the actual user email that was entered in the form. Attached below is a screenshot of the console showing the dataLayer output after submission — as you can see, it’s not capturing the user’s email.

Note: Our website is on the Drupal platform, and we are using the HubSpot form on Contact us and Request a demo CTAs. We have already created a conversion action on Google Ads and triggered it with the help of GTM.
Website link: https://www.beroeinc.com/contact
Any guidance or code examples from the HubSpot developer community would be really appreciated. Also, please do let me know after making changes to the code what changes we have to do on the GTM, too, if needed. Thanks in advance!
Saket Tripathi
(Assistant Digital Marketing Manager at Beroe Inc.)

Hey, @SaketTripathi25 :waving_hand: Thanks for posting and including your details. Tracking down issues like this involving externally hosted pages, HubSpot tracking, and GTM gets complex fast. Hey @AGale001 @CrystalHopper @SanjayKumar do you have any troubleshooting steps @SaketTripathi25 might take as a next step? Thank you! – Jaycee

The email address isn’t available on the thank you page.
You need to hook into the javascript API on the form page and get the email address from the form after the user clicks submit, but before the form navigates to the thank you page.

I think you are using legacy forms and the embed script, like this:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "1234567",
formId: "YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY",
region: "na1"
});
</script>

If the label on your email field is ‘email’, then add this code:

<script>
window.dataLayer = window.dataLayer || [];

window.addEventListener("message", function (event) {
 if (event.data && event.data.type === 'hsFormCallback') {
 if (event.data.eventName === 'onFormSubmit') {

 // Optional: simple de-duplication per form
 window.__hsSubmitted = window.__hsSubmitted || {};
 var formGuid = (event && event.data && event.data.id) || 'unknown';
 if (window.__hsSubmitted[formGuid]) return;
 window.__hsSubmitted[formGuid] = true;

 var fields = Array.isArray(event.data.data) ? event.data.data : [];
 var emailField = fields.find(function (f) { return f.name === 'email'; });
 var email = (emailField && emailField.value) ? emailField.value.trim().toLowerCase() : '';

 if (email) {
 dataLayer.push({
 'event': 'ec_formsubmit',
 'enhanced_conversion_data': {
 'email': email
 }
 });
 }
 }
 }
});
</script>

If you are using the new HubSpot forms, then the javascript API is different. More info here:

Hi OJ_ONT,
Thanks for the reply. We will implement this solution suggested by you and let you know if it is working.