Is it possible to add tracking to Hubspot form submit button


#1

Hello All,

Does anyone know if tracking code can be added to the submit button in aHubspot form? I've tried adding the tracking code using the onFormSubmit attribute but I'm not getting any tracking results for the submit button clicks. Maybe I have the code incorrectly formatted.? I've included my code below for reference. I've removed the portal and form ids for security reasons.

<!--[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({ 
css: '',
cssClass:'.hs-form fieldset {max-width: 100%;}',
portalId: '',
formId: '',
inlineMessage: '<iframe style="width:100%; height:800px;" scrolling="auto" src="URL goes here"></iframe>',
onFormSubmit: 'mmConversionTag(704048, this, "_self"); ga("send","event","button","click","product distributors")'
});
</script>

Thanks!


#2

In your case, the error is in the formatting indeed. onFormSubmit has to be a function if I recall right. Therefore this should do the trick:

hbspt.forms.create({ 
  css: '',
  cssClass:'.hs-form fieldset {max-width: 100%;}',
  portalId: '',
  formId: '',
  inlineMessage: '<iframe style="width:100%; height:800px;" scrolling="auto" src="URL goes here"></iframe>',
  onFormSubmit: function() {
    mmConversionTag(704048, this, "_self");
    ga("send","event","button","click","product distributors");
  }
});

We're using GTM for tracking everything. I don't know exactly how ga() works but with GTM our tracking works like this:

At the top of the page, a small script will make sure that dataLayer array is available: window.dataLayer = window.dataLayer || [];
Then anywhere in the code (in this case onFormSubmit event), we just do following:

window.dataLayer.push({
  event: '<EVENT_NAME>',
  formId: '<FORM_ID>'
});

And it works just fine.


#3

Hi,

I have used following script in the GTM but it's not worked for me, could anyone help me to get resolve this issue?

hubspot

Roshan


#4

Hi @Roshan,

Can you give some more details on your code/setup?


#5

Hi @Derek_Gervais Sorry the code was not displayed on the above thread i don't know why, I have modified the above thread and added the screenshot of the code.

Thanks!


#6

Hi @Roshan,

I believe this is the same issue we're discussing in your other topic: