The Problem
We had a recent project where the client wanted to embed HubSpot forms into their regular website. Why? They get your leads fed straight into HubSpot, grabbing all the useful user tracking functionality and conversion optimization tools that has made HubSpot a marketing Goliath. …But it’s never completely milk and honey. Some of the things the client wants to track are easier in Google Analytics, particularly with Google Tag Manager. Some of the key data for conversion optimization just doesn’t dovetail. After a few hundred Google searches, several failed experiments based on search results, and weeks waiting for a response from HubSpot support, we finally stitched together the solution for integrating Google Analytics event tracking in a HubSpot Form. We hope it helps someone else avoid this pain.
The Solution
In the HubSpot developer documentation, they prescribe the function called onFormSubmit. That’s partially the key to the Google Analytics kingdom, the spot where you place the Google event tracking JavaScript…
In the Google Tag Manager documentation, they prescribe the function you need to track the event called dataLayer.push.
If you properly connect your HubSpot form with the Google Tag Manager event, it will look like this…
<!--[if lte IE 8]> <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script> <![endif]--> <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script> <script> hbspt.forms.create({ sfdcCampaignId: 'xxxxxxxxxxxxxxxxx', portalId: 'xxxxxxx', formId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx' <!--BEGIN GOOGLE EVENT TRACKING> onFormSubmit: function($form) { dataLayer.push({'event' : 'formSubmitted', 'formName' : 'newsletter'}); } <!--END GOOGLE EVENT TRACKING> }); </script>
You can customize the parameters in the function to track whatever events you need this way.
We hope this saves you hundreds of Google searches and all the frustration we had…
Special thanks to Rob Loveday at Champion Communications for asking the right questions and helping us chase this solution down.
Leave A Comment