This script enables seamless tracking of HubSpot form submissions using the dataLayer. It listens for HubSpot form submission events and pushes relevant data to the dataLayer, making it compatible with analytics platforms such as Google Tag Manager.
- π οΈ Event Listener: Listens for HubSpot form submission events.
- ποΈ Key Data Captured:
- Form ID: Unique identifier for the form.
- Conversion ID: Conversion tracking ID (if applicable).
- Form GUID: Globally unique identifier for the form.
- Submission Inputs: Data submitted through the form.
- π Analytics-Ready: Pushes the data into the
dataLayerfor tracking and integration.
-
Make sure GTM is installed on your hubSpot site.
-
Copy and paste the script below in to a custom HTML tag, fire it on all pages.
-
Create a event trigger in GTM and give it a value of hubspot_form_submit
-
Fire your conversion tracking taging using this event trigger.
<script> // Listen for messages sent to the window object window.addEventListener("message", function(event) { // Check if the event data matches the desired HubSpot form submission callback if (!(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted')) return; // Initialize the dataLayer array if it doesn't already exist window.dataLayer = window.dataLayer || []; // Push the HubSpot form submission data into the dataLayer for analytics tracking dataLayer.push({ event: 'hubspot_form_submit', // Custom event name for form submission formId: event.data.id, // The unique ID of the form conversionId: event.data.data.conversionId, // Conversion ID (if applicable) formGuid: event.data.data.formGuid, // GUID associated with the form inputs: event.data.data.submissionValues // Form submission inputs }); }); </script>