I’m working on a project where I need to include custom data attributes in my HubSpot forms for tracking purposes. Specifically, I want to add custom attributes to the form tag itself that gets generated by HubSpot.
<form class="hs-form" data-tracking="analytics-enabled">
<!-- HubSpot form content -->
</form>
I’m looking for a reliable method to accomplish this, preferably something that doesn’t require dynamic JavaScript since I need it for form analytics tracking. Has anyone found an effective approach to modify HubSpot form attributes? I’ve tried a few different methods but haven’t found a consistent solution yet.
Any suggestions would be really helpful. Thanks in advance!
Here’s what worked for me: use MutationObserver to watch for when HubSpot injects the form into the DOM. Set up the observer on the container where the form appears, then add your custom data attributes once it detects the form element. This catches the form right after HubSpot creates it but before users interact with it. Way more reliable than callbacks since HubSpot’s API doesn’t always fire them consistently. I’ve used this for tracking form impressions and conversion funnel analytics across different HubSpot forms. Just remember to disconnect the observer after you make your changes - otherwise you’ll get performance issues on pages with lots of dynamic updates.
Had this exact problem six months ago when setting up form tracking for our marketing team. The trick that actually works: use HubSpot’s form callbacks with DOM manipulation right after the form loads. I hook into onFormReady and immediately add custom attributes to the form element. Perfect timing - the form’s fully rendered but users haven’t touched it yet. Don’t add attributes too early or HubSpot will strip them when it re-renders during validation. Been running this in production for months with zero issues. Works great for both embedded and popup forms. Just make sure you’re targeting the right form if you’ve got multiple HubSpot forms on one page.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.