All / Advanced / Conversions
HubSpot forms V4 trigger a conversion on submit
AB Split Test supports two methods for tracking HubSpot form submissions as conversions. The native integration (added in v2.5.2) is the easiest and works for most setups. The manual JavaScript method is still available for advanced cases or older HubSpot embed versions.
Method 1: Native integration (v2.5.2 and later)
Since v2.5.2, AB Split Test includes built-in HubSpot form conversion tracking. No custom code required.
Step 1. Go to AB Split Test > Settings > Data Management and enable HubSpot Form Conversions. The setting is off by default.
Step 2. Create or edit a test and select Form Submission as your conversion goal type.
Step 3. In the HubSpot form field, enter one of the following:
- A specific form GUID to track only that form
- Multiple GUIDs separated by commas to track several specific forms
- Leave it blank to track any HubSpot form submission on the page as a conversion
Method 2: Manual JavaScript (V3/V4)
The native integration fully replaces this method for most setups. Use the manual method only if you need to fire a conversion based on custom logic, are using a heavily customised HubSpot embed, or are on a version of AB Split Test before v2.5.2.
HubSpot embedded forms cannot be selected with a button click selector because they are embedded in an iframe and browser security rules prevent AB Split Test from watching click events inside it. The workaround is to listen for the HubSpot postMessage event and fire abstConvert() manually.
Add this script to your page. It must run independently of how the form is embedded:
<script>
// MUST run before/independent of how the form is embedded
window.addEventListener('message', function (event) {
// HubSpot posts structured messages from its iframe
if (!event || !event.data || event.data.type !== 'hsFormCallback') return;
// v3 & v4 both emit onFormSubmitted on successful persist
if (event.data.eventName === 'onFormSubmitted') {
try {
abstConvert(1234); // CHANGE TO YOUR TEST ID
} catch (e) { console.error('abstConvert failed', e); }
// Optional: inspect event.data for formGuid, portalId, fields, etc.
console.log('HubSpot form submitted:', event.data);
}
});
</script>
Important: Replace 1234 with your actual test ID. Find your test ID in the test settings screen in AB Split Test.
Important: Make sure the test ID matches your currently active test. If the test has been completed and you created a new one, the ID will be different. This is the most common cause of HubSpot conversions not recording.
Method 3: Manual JavaScript (V2)
If you are using the older HubSpot forms V2 embed, modify your existing embed code to add an onFormSubmitted callback.
Note: This is a community snippet and has not been verified by the team. We recommend upgrading to HubSpot forms V4 and using the native integration in Method 1.
<!-- 1) Load HubSpot forms v2 -->
<script src="https://js.hsforms.net/forms/embed/v2.js" async></script>
<!-- 2) Container to render the form -->
<div id="hs-form-container"></div>
<script>
//find your existing form embed and make changes as per below
window.addEventListener('load', function () {
if (!window.hbspt || !hbspt.forms) return;
hbspt.forms.create({
region: "na1", // adjust if your portal uses a different region
portalId: "YOUR_PORTAL_ID",
formId: "YOUR_FORM_GUID",
target: "#hs-form-container",
// add this to your create object change 1234 to your test ID
onFormSubmitted: function ($form) {
try { abstConvert(1234); } catch (e) { console.error(e); }
},
// Optional: fires right before data is sent
onFormSubmit: function ($form) {
// e.g., disable UI; do not fire conversions here
}
});
});
</script>
Troubleshooting
Conversions not recording?
- Check the test ID in
abstConvert()matches the ID of your currently active test. Completed tests do not record conversions. - If using the native integration, confirm HubSpot Form Conversions is enabled in Settings > Data Management.
- Add
?abstdebug=1to your page URL and open the browser console to see what AB Split Test is doing. - Check cookie consent is not blocking tracking. Go to Settings > Data Management > Wait for Consent Approval.