Trigger Google Analytics Event with Form Submission

amybeans
Participant

I read this article, which shows you how to add Google Analytics event tracking to a HubSpot form submission:

I applied this to one of my embedded HubSpot forms, but it doesn’t seem to be tracking the event in Google Analytics. I am not using Google Tag Manager.

Here is the code I used:
hbspt.forms.create({
css: ‘’,
portalId: ‘XXXXXXX’,
formId: ‘XXXXXXX-XXX-XXX-XXXXXXX’,
onFormSubmit: function($form) {
"_gaq.push([’_trackEvent’, ‘Contact’, ‘contact_us_form’, ‘Main_Contact_Us’, false]);"
},
submitButtonClass: ‘btn btn-primary btn-hubspot’,
locale: ‘en’,
translations: {
en: {phoneInvalidLengthOrFormat: ‘Please enter a valid phone number.’}
}
});

I realize that this is very similar to the question posed in this forum, but I followed these instructions and am still running into issues.

@Champion You could add the onFormSubmit function to the embed code which could then call your google analytics event.

Any help/guidance you could provide would be helpful.

Thanks!

0 Upvotes
13 Replies 13
Derek_Gervais
HubSpot Employee

Hi @amybenes,

Based on some documentation I’ve read (here), it looks like you might want to consider using something like this:

ga(‘send’, ‘event’, ‘Videos’, ‘play’, ‘Fall Campaign’);

Regardless, you should remove the quotes surrounding the Google Analytics event inside the onFormSubmit function, as this worked in my own testing.

0 Upvotes
amybeans
Participant

Hi – this worked. thanks for your help!

Not applicable

Where you paste that code?
“Here is the code I used:
hbspt.forms.create({
css: ‘’,
portalId: ‘XXXXXXX’,
formId: ‘XXXXXXX-XXX-XXX-XXXXXXX’,
onFormSubmit: function($form) {
”_gaq.push([’_trackEvent’, ‘Contact’, ‘contact_us_form’, ‘Main_Contact_Us’, false]);"
},
submitButtonClass: ‘btn btn-primary btn-hubspot’,
locale: ‘en’,
translations: {
en: {phoneInvalidLengthOrFormat: ‘Please enter a valid phone number.’}
}
});"

I have an hubspot form and i want to do that on click button, but i don’t know where is the place to paste this code p.e. “ga(‘send’, ‘event’, ‘Videos’, ‘play’, ‘Fall Campaign’);”

Thanks for your time.

0 Upvotes
Nigrinis
Member

Hi Can you share your final code working? I can't get to work mine.... it is below:

 

<!--[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({

region: "na1",

portalId: “xxx”,

formId: "dxxxf”,

onFormSubmit: function($form){

  ga ('send', 'event', 'form-submit', 'contact-submit', 'contact-us-page')

    },

 

});

</script>

0 Upvotes
Nigrinis
Member

Hi Derek, I'm trying to replicate this in my form with no sucess... could you please add the onFormSubmit function to the below code? everytime I add it the form doesnt display.... thanks a lot!

 

<!--[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({

region: "na1",

portalId: “xxxx”,

formId: “xxxxx”,

 

});

</script>

 

0 Upvotes
Derek_Gervais
HubSpot Employee

Hi @Daniel_Marques,

You’ll want to put that inside the onFormSubmit callback, where the _gaq.push call is now. You’ll probably also want to check out the ga.js to analytics.js migration docs, if you haven’t already:

https://developers.google.com/analytics/devguides/collection/upgrade/reference/gajs-analyticsjs#even...

0 Upvotes
Jordanwywm
Member

link broken

0 Upvotes
Not applicable

Hi @Derek_Gervais,

Could I use this to add an ga event after submission of a regular HubSpot form (not embedded) ?

Thanks !

0 Upvotes
Derek_Gervais
HubSpot Employee

Hi @Laurent_Maillard,

There isn't currently a way to access the onFormSubmit callback globally or via the UI, so you'd need to either replace it with an embedded form or use a workaround like detecting the hsvalidatedsubmit event (see below).

0 Upvotes
scottfasser
Participant

Hi, I'm trying to trigger GA events from an embedded hubspot form as well, but it's not working after following the instructions. Here is my form code - what have I done incorrectly?

 

<!--[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({
portalId: "4326424",
formId: "106e45cd-6e1d-43b2-af21-78798a4b3e68",
  onFormSubmit: function($form){
  ga ('send', 'event', 'form-submit', 'contact-submit', 'contact-us-page')
    },
});
</script>
jonnoprice1
Participant

Hi all - battling with this same issue. I really think that the better route to follow is to first implement Google Tag Manager and then republish you GA script but through GTM..

"Google Tag Manager is a tag management system that allows you to create and monitor tags on a user interface, without writing new code each time you want to construct a tag. You simply embed the Google Tag Manager code into each page of your website. This eliminates the manual process of creating tags, making your marketing process more efficient and precise." 

Once done - you will be in a much better position to track conversions in Hubspot. With GTM installed here is how you can track HS forms in GTM and GA:

https://analytive.com/blog/the-super-easy-way-to-track-hubspot-forms-in-google-tag-manager-and-analy...

juhafly
Member

I have followed this GTM instrucion as followed but the tag is not firing. In the trigger, I used the option to record every embedded form completion. So I used the "hbspt-form" option in the trigger which is found from the page where the form is embedded. 

 

Screenshot 2020-05-14 at 10.44.35.png

 

Is there something that I'm missing here?

0 Upvotes
JHakkarainen
Member

Hi,

 

I have a similar problem since we should embed a Google Analytics event tracking code in HubSpot form. Conversion is when a customer clicks a send button of the contact form. My question is that where is the right place for GA Event tracking code? And is this type of code the best option in this case: 

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);

 

I tried the above code and replaced Category, Action and Label fields by our campaign names from Google Analytics.

 

Here's the code of the HubSpot form:

<!--[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({
region: "na1",
portalId: "4128102",
formId: "3a46f667-8e71-464a-8b45-d0956e30b917"
});
</script>

Thanks a lot in advance!