HubSpot Form Tracking in GTM: Lead Tracking Simplified for Service-Based Websites

HubSpot forms are a popular choice for capturing leads, but tracking their performance effectively in analytics tools like Google Analytics 4 (GA4) or ad platforms requires a reliable integration. By setting up a GTM listener for HubSpot forms, you can track form submissions seamlessly and push relevant data to your analytics platforms.
In this newsletter, I’ll guide you through the process of setting up HubSpot form tracking using the custom GTM listener code provided.
Why Track HubSpot Forms?
Tracking HubSpot forms ensures you:
- Measure lead generation performance effectively.
- Attribute form submissions to specific campaigns, channels, or ad sets.
- Use the data for remarketing, conversion tracking, or advanced analytics.
Step-by-Step Guide to Tracking HubSpot Forms in GTM
1️⃣ Add the Listener Code
The following script listens for HubSpot form submissions and pushes data to the GTM Data Layer:
<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',
'event-data': event.data // Pass form data for analytics
});
}
});
</script>
What it does:
- Listens for the hsFormCallback message from HubSpot.
- Checks if the eventName is onFormSubmitted (triggered when the form is successfully submitted).
- Pushes a custom event (hubspot-form-success) and form data to the GTM Data Layer.
Where to add this code:
- Add it to your website’s <head> section or through a Custom HTML Tag in GTM.
2️⃣ Create a GTM Trigger
- Go to Triggers > New.
- Choose Custom Event as the trigger type.
- Set the event name to hubspot-form-success.
- Add any optional conditions (e.g., page path or form-specific identifiers).
3️⃣ Create a Tag in GTM
Depending on where you want to send the form submission data, you’ll need to create an appropriate tag.
For Google Analytics 4 (GA4):
- Go to Tags > New.
- Select Google Analytics: GA4 Event.
- Configure the tag:Event Name: form_submission.Event Parameters:Key: form_idValue: {{event-data.portalId}} (or any relevant property from the event-data object).Add additional parameters if needed.
- Assign the Custom Event Trigger (hubspot-form-success) to this tag.
For Other Platforms (e.g., Meta Pixel):
- Use the standard Lead event or a custom conversion event in your tag configuration.
4️⃣ Test Your Setup
- Enable Preview Mode in GTM.
- Submit a HubSpot form on your website.
- In the GTM debug panel, verify that:The custom event hubspot-form-success is triggered.The form data is pushed to the Data Layer.
- Check if your tag (e.g., GA4 or Meta Pixel) fires successfully.
Advanced Configuration
- Track Form Fields: Extract specific fields from the event-data object, such as user email or form name. Use Data Layer Variables in GTM to capture these values dynamically.
- Separate Tracking by Form: Use unique form IDs or names from the event-data to differentiate between forms.
Use Cases for HubSpot Form Tracking
- Google Ads Conversion Tracking: Attribute form submissions as conversions.
- Facebook and LinkedIn Retargeting: Build audiences based on lead data.
- Custom Dashboards: Create visualizations in tools like Google Data Studio.
Conclusion
With this setup, you’ll have a robust tracking solution for HubSpot forms, enabling you to measure performance and optimize marketing efforts effectively.
💡 If you’re tracking HubSpot forms already, what challenges have you faced? Share your experiences and let’s solve them together!
📰 Subscribe for More!
Enjoyed this guide? Subscribe to my LinkedIn newsletter for more tips on GTM, analytics, and advanced tracking strategies!
Need this implemented, audited, or white-labeled?
Whether you are an eCommerce brand owner fixing signal loss or a digital marketing agency needing a senior analytics engineer under strict NDA, I deliver 24–48h SLA turnaround with 8.5+ EMQ match rates.


