How to Track HubSpot Forms in GA4 and GTM


Mastering HubSpot form tracking in GA4 ensures you never miss a lead conversion due to iFrame or JavaScript limitations.

The Challenge: Why HubSpot Forms Are "Invisible"

HubSpot forms often load via iFrames or dynamic JavaScript. Standard GTM "Form Submission" triggers can't see these events because they don't bubble up to the main window. To fix this, we need a custom listener that communicates between the HubSpot form and your Data Layer.

Method 1: The GTM JavaScript Listener (Best Practice)

This method captures the onFormSubmitted event directly from HubSpot's postMessage API. It is the most reliable way to track conversions without relying on thank-you page redirects.

1. Create the Custom HTML Tag

In GTM, create a Custom HTML Tag triggered on All Pages:

JavaScript
<script>
    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_id': event.data.id
            });
        }
    });
</script>

2. Configure the Trigger and GA4 Tag

  • Trigger: Create a Custom Event trigger named hubspot_form_success.
  • GA4 Tag: Create a GA4 Event tag with the event name generate_lead and fire it using the trigger above.

Method 2: Native HubSpot Embed Callback

If you have direct access to the website code and don't want to use GTM for the listener, you can add the tracking event directly into the HubSpot embed script:

JavaScript
hbspt.forms.create({
    region: "na1",
    portalId: "0000000",
    formId: "your-form-guid",
    onFormSubmitted: function($form) {
        // Direct GA4 Event
        gtag('event', 'generate_lead', {
            'form_id': 'contact_form'
        });
    }
});

Advanced: Tracking Validation Errors

Tracking when users fail to submit a form is crucial for CRO. Add this snippet to your GTM listener to capture validation errors:

JavaScript
if(event.data.eventName === 'onFormFailedValidation') {
    window.dataLayer.push({
        'event': 'hubspot_form_error',
        'hs_form_id': event.data.id
    });
}

Summary

Using a GTM listener is the cleanest way to track HubSpot forms in GA4. It prevents duplicate data, works with iFrames, and provides granular insights into user behavior. Always test your setup using the GTM Preview mode and GA4 DebugView before going live.

Report an issue

Frequently Asked Questions

Why can't GTM form submission triggers detect HubSpot form submissions?
HubSpot forms often load via iFrames or dynamic JavaScript. Standard GTM Form Submission triggers only detect native form events that bubble up to the main window — events from within an iFrame are sandboxed and invisible to these triggers. A custom JavaScript listener using the postMessage API is required to capture HubSpot form events reliably.
How do you track HubSpot form submissions in GA4 using GTM?
Create a Custom HTML Tag in GTM that fires on All Pages. The tag adds a window message event listener that listens for HubSpot's hsFormCallback postMessage with the eventName onFormSubmitted, then pushes a custom event (hubspot_form_success) to the dataLayer. Then create a GA4 Event tag triggered by that custom event, using generate_lead as the event name.
How do you track HubSpot form validation errors in GA4?
Inside the same GTM message listener, add a condition that checks for eventName === 'onFormFailedValidation' and pushes a separate dataLayer event such as hubspot_form_error. This lets you measure how often users fail to complete forms, which is essential data for conversion rate optimization.
Can you track HubSpot forms in GA4 without GTM?
Yes. If you have direct access to the page code, use the onFormSubmitted callback in the hbspt.forms.create() embed script and call gtag('event', 'generate_lead', {...}) directly inside it. This approach is simpler but less flexible — GTM is preferred when you need to manage tracking across multiple forms without touching the codebase.

Stay in touch

Notes on Web Development

New articles on web development, accessibility, and technical SEO.

Occasional deep dives into platform-specific topics like HubSpot CMS, based on real-world problems and solutions.