Is there definitive javascript code to use when tracking hubspot forms in Google Analytics 4?

There are numerous posts on this board and others referencing this topic but none of these that we have trialled thus far work correctly.

Does anyone - ideally from hubspot - have the JS snippet that needs to be ingested by Google Tag Manager in order to correctly capture leads from hubspot’s forms in google analytics 4?

Understand that there are other moving parts to this as well. at this point we simply want to identify the correct version of that javascript snippet.

There is nothing from Hubspot on this as you are coding for a third party, not them. This initially has to do with how your events are setup as this is specific how you are tracking form submissions. Once that is done, you just attach GTM events to look for the Hubspot form submission. Have you gotten to the point of at least defining your GA4 events/conversions?

Hi @SLovejoy ,

I created a little video on how you can track several HubSpot events and send the data to the data layer. It also includes the ‘form submitted’ event that is used to send an event to the data layer whenever a form is being submitted.
Here is the code that you could probably use in Tagmanager;

<script type="text/javascript">
window.dataLayer = window.dataLayer || [];

if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
 window.dataLayer.push({
 event: 'hubspotFormSubmitted',
 hsFormGuid: event.data.id,
 currentUrl: window.location.href,
 });
}
</script>

We have implemented and tested this on several HubSpot websites, and so far, had no issues with this code.

Many thanks for your helpful reply here, Teun. There was further work that had to be undertaken with Google’s technical team themselves in order to get conversions working for this hubspot form in analytics 4. Fortunately they offered two sessions for Google Ad migration purposes which covered this functionality.