{"id":4404,"date":"2020-06-08T08:43:35","date_gmt":"2020-06-08T05:43:35","guid":{"rendered":"https:\/\/trackingchef.com\/?p=4404"},"modified":"2024-11-06T18:29:04","modified_gmt":"2024-11-06T15:29:04","slug":"hubspot-form-tracking-with-google-tag-manager","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-tag-manager\/hubspot-form-tracking-with-google-tag-manager\/","title":{"rendered":"Hubspot form tracking with (and w\/o) Google Tag Manager"},"content":{"rendered":"\n

Hubspot is one of my favorite tools. Working with Hubspot, you usually measure all interactions from inside the platform’s reporting tools, which are pretty decent.<\/p>\n\n\n\n

However, my opinion, contrary to other Hubspot users, is that you should still measure yourself on a proper web analytics tool such as Google Analytics (GA). Even if you don’t use GA, you will want to implement conversion tracking for other advertising platforms, such as Quora, that aren’t supported out of the box in Hubspot.<\/p>\n\n\n\n

This requires being able to track Hubspot form submissions efficiently.<\/p>\n\n\n\n

Like other tools, the best course of action is to use a dedicated Thank You Page (TYP). Most ad platforms (e.g. Google Ads or LinkedIn) support URL-based tracking for TYPs, making this setup a breeze. But in many cases, the form submission will not route the user to a new page, so capturing the form submission is slightly more complex.<\/p>\n\n\n\n

In this post, I’ll cover the different methods to track these forms. I’ll also show how to add these conversion events without<\/strong> Google Tag Manager.<\/p>\n\n\n\n

Generic form tracking<\/h2>\n\n\n\n

The first option is to set up a generic event to indicate form submissions. This option is also the recommended way to track forms on Hubspot pages, e.g. Blog or Landing pages, that have the forms integrated directly.<\/p>\n\n\n\n

In Google Tag Manager, create a new tag of the type Custom HTML. Paste into the tag the following script:<\/p>\n\n\n\n

<script type=\"text\/javascript\">\n  window.addEventListener(\"message\", function(event) {\n    if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {\n      var submissionValues = event.data.data.submissionValues || {};\n      \n      window.dataLayer.push({\n        'event': 'form_submission',\n        'form_id': event.data.id,\n        'submission_data': submissionValues\n      });\n    }\n  });\n<\/script><\/code><\/pre>\n\n\n\n

Trigger the tag on ‘All Pages’.<\/p>\n\n\n\n

This tag will now trigger a Custom Event on each Hubspot form submission, with the Event Category ‘form submission’ and Event Action set to the form’s ID in Hubspot.<\/p>\n\n\n\n

This Custom Event can now be used to trigger additional platforms such as LinkedIn Ads or Quora Ads.<\/p>\n\n\n\n

Optional: Human-friendly form names<\/h3>\n\n\n\n

Since the form’s ID is usually some gibberish string, e.g. 988c94e6-d06b-4181-95d4-3ea925d01d47<\/em>, you might want to translate it to a human-friendly form.<\/p>\n\n\n\n

The simplest way is to slightly alter the code above:<\/p>\n\n\n\n

<script type=\"text\/javascript\">\nvar id_lookupTable = {\n'988c94e6-d06b-4181-95d4-3ea925d01d47': 'contact us',\n'bc3f3208-e01e-4a9f-b1e9-161391c9279f': 'request demo'\n};\n  window.addEventListener(\"message\", function(event) {\n    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {\n      window.dataLayer.push({\n        'event': 'form_submission',\n        'form_id': event.data.id,\n        'form_name': id_lookupTable[event.data.id]\n      });\n    }\n  });\n<\/script><\/code><\/pre>\n\n\n\n

In the snippet, you can add new forms and replace the form ID with a readable name. For example, replace bc3f3208-e01e-4a9f-b1e9-161391c9279f <\/em>with Request Demo<\/em>.<\/p>\n\n\n\n

\n

Pro tip:<\/strong>
You can also send the form’s ID and convert it in GTM to a friendly name using a Lookup Table variable.<\/p>\n<\/blockquote>\n\n\n\n

Tracking form submissions without GTM<\/h3>\n\n\n\n

In the unlikely case that you don’t use GTM, you can still use this method to track form conversions.<\/p>\n\n\n\n

Simply add the following snippet to your site’s code:<\/p>\n\n\n\n

<script type=\"text\/javascript\">\n  window.addEventListener(\"message\", function(event) {\n    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {\n  \/\/ some code you want to trigger\n    }\n  });\n<\/script><\/code><\/pre>\n\n\n\n

For example, if you want to track Facebook Leads and Google Analytics events:<\/p>\n\n\n\n

<script type=\"text\/javascript\">\n  window.addEventListener(\"message\", function(event) {\n    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {\n   fbq('track', 'Lead');\u200e\n   gtag('event', 'form_submission', {'form_id' : event.data.id});\n    }\n  });\n<\/script><\/code><\/pre>\n\n\n\n

Specific form tracking<\/h2>\n\n\n\n

Another alternative to track Hubspot forms is to add a tracking snippet directly to the form’s embed code. This option can be used only when embedding the Hubspot form on a different CMS (e.g. WordPress).<\/p>\n\n\n\n

\n

This method can integrates with the Generic Event Tracking<\/a> method discussed on a separate post, please make sure you follow the instructions detailed there first.<\/p>\n<\/blockquote>\n\n\n\n

First, grab the form’s embed code from Hubspot. It should probably look something like this:<\/p>\n\n\n\n

<!--[if lte IE 8]>\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2-legacy.js\"><\/script>\n<![endif]-->\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2.js\"><\/script>\n<script>\n  hbspt.forms.create({\n\tportalId: \"12345678\",\n\tformId: \"988c94e6-d06b-4181-95d4-3ea925d01d47\"\n});\n<\/script><\/code><\/pre>\n\n\n\n

Under the formID parameter, we will add an event listener that will trigger on this form’s submission. This is the code you will add to embed the form on your site.<\/p>\n\n\n\n

<!--[if lte IE 8]>\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2-legacy.js\"><\/script>\n<![endif]-->\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2.js\"><\/script>\n<script>\n  hbspt.forms.create({\n\tportalId: \"12345678\",\n\tformId: \"988c94e6-d06b-4181-95d4-3ea925d01d47\",\n        onFormSubmit: function($form){\n        window.dataLayer.push({\n          'event': 'form_submission',\n          'form_name': 'request demo'\n      });\n    }\n});\n<\/script><\/code><\/pre>\n\n\n\n

This tag will now trigger a Custom Event on each Hubspot form submission, with the Event Category ‘form submission’ and Event Action set to the form’s name as you set it.<\/p>\n\n\n\n

This Custom Event can now be used to trigger additional platforms such as LinkedIn Ads or Quora Ads.<\/p>\n\n\n\n

Tracking form submissions without GTM<\/h3>\n\n\n\n

In the unlikely case that you don’t use GTM, you can still use this method to track form conversions.<\/p>\n\n\n\n

Simply add the following snippet to your form’s embed code, just under the formID<\/em> parameter:<\/p>\n\n\n\n

<!--[if lte IE 8]>\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2-legacy.js\"><\/script>\n<![endif]-->\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2.js\"><\/script>\n<script>\n  hbspt.forms.create({\n\tportalId: \"12345678\",\n\tformId: \"988c94e6-d06b-4181-95d4-3ea925d01d47\",\n        onFormSubmit: function($form){\n          \/\/ some code you want to trigger\n      });\n    }\n});\n<\/script><\/code><\/pre>\n\n\n\n

For example, if you want to track Facebook Leads and Google Analytics events:<\/p>\n\n\n\n

<!--[if lte IE 8]>\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2-legacy.js\"><\/script>\n<![endif]-->\n<script charset=\"utf-8\" type=\"text\/javascript\" src=\"\/\/js.hsforms.net\/forms\/v2.js\"><\/script>\n<script>\n  hbspt.forms.create({\n\tportalId: \"12345678\",\n\tformId: \"988c94e6-d06b-4181-95d4-3ea925d01d47\",\n        onFormSubmit: function($form){\n         fbq('track', 'Lead');\u200e\n         gtag('event', 'form_submission', {'form_name' : 'request demo'});\n    }\n      });\n    }\n});\n<\/script><\/code><\/pre>\n\n\n\n
\n

Make sure you use your portalID and formID \ud83d\ude42<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

Hubspot is one of my favorite tools. Working with Hubspot, you usually measure all interactions from inside the platform’s reporting tools, which are pretty decent. However, my opinion, contrary to other Hubspot users, is that you should still measure yourself on a proper web analytics tool such as Google Analytics (GA). Even if you don’t […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,42],"tags":[200],"class_list":["post-4404","post","type-post","status-publish","format-standard","hentry","category-google-tag-manager","category-hubspot","tag-forms"],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=4404"}],"version-history":[{"count":14,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4404\/revisions"}],"predecessor-version":[{"id":6108,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4404\/revisions\/6108"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=4404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=4404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=4404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}