Enhanced Conversion Tracking with Google Tag Manager

Hi there,
does anyone know how to grab the email address from a form submit of HubSpot out of a thank you page? It should help me to boost my Google ads campaigns if Google can match the email address within his platform.
We use forms from HubSpot on our Drupal pages and I do not know how I can do the implementation within Google Tag Manager. I did some research within other HubSpot community topics and found this one here: https://community.hubspot.com/t5/HubSpot-Ideas/Google-Enhanced-Conversions-through-HubSpot/idc-p/645053/highlight/true#M114486
In there is a script code, which I have to implement. I have experiences with Google Tag Manager, but it would helpful to give me some instructions on how to do this. I would much appreciate it if someone could support me…

<script type="text/javascript">
 window.addEventListener("message", function(event) {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
 window.dataLayer.push({
 'event': 'hubspot-form-success',
 'hs-form-guid': event.data.id,
 'email': Object.keys(hubspot._emailValidationAndResubCache)[0]
 });
 }
 });
</script>

Thank you very much!

Hi @BenGoe and welcome, it’s great to see you here!
Great question, thanks for asking the HubSpot Community and thanks for sharing a workaround that you found in the Ideas Forum! :heart:
Let’s see what our Experts and Community Members have to share: Hi @ALunaJust, @JBhathena, @derekcavaliero, @Ben_M and @Aakar do you have any tips to share with @BenGoe, please?
Have a lovely day and thanks so much in advance for your help!
Bérangère

Thank you so much @BérangèreL!
I would really appreciate it to get some support from the experts @ALunaJust, @JBhathena, @derekcavaliero, @Ben_M and @Aakar
Best,
Benett

Hi @BenGoe
Yes, you can track enhanced conversions using HubSpot forms with Google Tag Manager (GTM), even when forms are embedded on Drupal pages.
Here’s how you can set it up:

Steps to Implement Enhanced Conversion Tracking via GTM
1. Add the Script to Your Drupal Thank You Page

2. Place the following script on the Thank You page (replace the link into script):

<script type="text/javascript">
window.addEventListener(“message”, function(event) {
if(event.data.type === ‘hsFormCallback’ && event.data.eventName === ‘onFormSubmitted’) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
‘event’: ‘hubspot-form-success’,
‘hs-form-guid’: event.data.id,
‘email’: Object.keys(hubspot._emailValidationAndResubCache)[0] || ‘’
});
}
});
</script>

3. In GTM: Create a Trigger

  • Trigger Type: Custom Event
  • Event name: hubspot-form-success

4. In GTM: Create a Variable

  • Go to Variables > New > Data Layer Variable
  • Name it: DLV - Email
  • Data Layer Variable Name: email

5. In GTM: Create a Tag (for Enhanced Conversions)

  • Use your Google Ads conversion tag or Google Tag for enhanced conversions.
  • Under “Enhanced conversions,” choose Manual and map the email to the variable {{DLV - Email}}.

6. Test in Preview Mode

  • Submit the form, verify the email is passed in the Data Layer, and the tag fires.

Important: This will work when your HubSpot forms is embed and you have permission to access _emailValidationAndResubCache object. Always verify in Chrome DevTools and GTM Preview mode before implementing

Perfect! Thank you very much. I really appreciate it. I’ll implement and test it soon.