• Live group demo of Marketing Hub + Data Agent

    Standardize reporting, reduce manual work, and introduce AI without cleanup

    Join us on March 12
  • Ready to build your local HubSpot community?

    HUG leaders host events, spark connections, and create spaces where people learn and grow together.

    Become a HUG Leader

HubSpot Form Listener Not Tracking in GTM

LHobbs
Member | Platinum Partner
Member | Platinum Partner

Hi,

 

I have a form embedded on my website and I am trying to send an event when the form loads on my page. 

 

I've added this script to GTM to track when the form has loaded.

 

This is the javascript I am using. Any thoughts on why I wouldn't be seeing when the form has loaded?

 

<script type="text/javascript">
window.addEventListener("message", function(event) {
   if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
    window.dataLayer.push({
      'event': 'hubspot-form-ready',
      'hs-form-guid': event.data.id
     });
   }
});
</script>

 

0 Upvotes
2 Accepted solutions
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

@LHobbs it could be because the script is running too later (after that event has already happened). For instance, if the google tag manager script is in the footer below the HubSpot form embed.

HubSpot Web Design and Development Agency

View solution in original post

miljkovicmisa
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Hello @LHobbs , it seems you are using global events for embedded form. Have you tried binding the event directly to the embedded form like explained in this article?

Basically you would do something like this:

hbspt.forms.create({
  portalId: '',
  formId: '',
  onFormReady: function($form) {
    window.dataLayer.push({
      'event': 'hubspot-form-ready',
      'hs-form-guid': 'the form id' //this is also going to be available somewhere in $form
    });
  } 
}); 



If my answer was helpful please mark it as a solution.

View solution in original post

3 Replies 3
miljkovicmisa
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Hello @LHobbs , it seems you are using global events for embedded form. Have you tried binding the event directly to the embedded form like explained in this article?

Basically you would do something like this:

hbspt.forms.create({
  portalId: '',
  formId: '',
  onFormReady: function($form) {
    window.dataLayer.push({
      'event': 'hubspot-form-ready',
      'hs-form-guid': 'the form id' //this is also going to be available somewhere in $form
    });
  } 
}); 



If my answer was helpful please mark it as a solution.

stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

@LHobbs it could be because the script is running too later (after that event has already happened). For instance, if the google tag manager script is in the footer below the HubSpot form embed.

HubSpot Web Design and Development Agency
Jaycee_Lewis
Thought Leader

Hi, @LHobbs 👋 Thanks for reaching out. Hey, @miljkovicmisa @stefen, do you have any experience or troubleshooting tips you can share with @LHobbs?

 

Thank you! — Jaycee





loop


Loop Marketing is a new four-stage approach that combines AI efficiency and human authenticity to drive growth.

Learn More




0 Upvotes