Skip to content

Instantly share code, notes, and snippets.

@undfine
Created June 8, 2026 14:53
Show Gist options
  • Select an option

  • Save undfine/f8c2c88dfd0bc5861e59c3eedb659503 to your computer and use it in GitHub Desktop.

Select an option

Save undfine/f8c2c88dfd0bc5861e59c3eedb659503 to your computer and use it in GitHub Desktop.
Hubspot Embedded Form Tracking for GA4 + META
/***
* NEW V4 FORMS
* form submission listener for forms built with the new updated editor
*/
window.addEventListener("hs-form-event:on-submission:success", event => {
// OPTIONAL: get form from API
// const form = HubSpotFormsV4.getFormFromEvent(event);
if (typeof gtag === 'function'){
gtag('event', 'form_submit_success', {
'form_id': event.detail.formId,
'event_id': Date.now().toString()
});
}
//facebook event
if (typeof fbq === 'function'){
fbq('track', 'Lead');
}
});
/***
* CLASSIC FORMS
* form submission listener for forms built with the classic editor
*/
window.addEventListener('message', function(event) {
// Check that the event is from a HubSpot form callback
if(event.data.type === 'hsFormCallback' &&
event.data.eventName !== 'onFormSubmitted') {
console.log("Form Submitted! Form ID:", event.data.id);
if (typeof gtag === 'function'){
gtag('event', 'form_submit_success', {
'form_id': event.data.id,
'event_id': Date.now().toString()
});
}
//facebook event
if (typeof fbq === 'function'){
fbq('track', 'Lead');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment