APIs & Integrations

amybeans
Participant

Trigger Google Analytics Event with Form Submission

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!

14 Replies 14
codeandtonichq
Contributor

Trigger Google Analytics Event with Form Submission

 

Super easy way to do this in a minute (without Google Tag Manager)

I've written a blog post tutorial for the absolutely easiest way to do this. Has a copy-pasteable snippet and a video which shows it step-by-step.


You should not modify the form itself. It's inside an iframe so it will not communicate with your website analytics for valuable info like where the user came from, which pages they visited etc etc. You'll either use Google Tag Manager or a script like mine to listen for the form being submitted inside it's iframe and then sending the event to Google Analytics.

 

Hope this helps! And feel free to get in touch if you need any 1-on-1 help. 

 

0 Upvotes
JHakkarainen
Participant

Trigger Google Analytics Event with Form Submission

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!

 

jonnoprice1
Participant

Trigger Google Analytics Event with Form Submission

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

Trigger Google Analytics Event with Form Submission

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
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Trigger Google Analytics Event with Form Submission

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

Trigger Google Analytics Event with Form Submission

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>
Not applicable

Trigger Google Analytics Event with Form Submission

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 Alumni
HubSpot Alumni

Trigger Google Analytics Event with Form Submission

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

Trigger Google Analytics Event with Form Submission

link broken

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Trigger Google Analytics Event with Form Submission

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
Nigrinis
Member

Trigger Google Analytics Event with Form Submission

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
amybeans
Participant

Trigger Google Analytics Event with Form Submission

Hi – this worked. thanks for your help!

Nigrinis
Member

Trigger Google Analytics Event with Form Submission

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
Not applicable

Trigger Google Analytics Event with Form Submission

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