Trigger Google Analytics Event with Form Submission


#1

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.

Any help/guidance you could provide would be helpful.

Thanks!


Adding onClick button event code
Post submission callback on an embedded form
How can i put value with form embed code
#2

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.


Adding onClick button event code
#3

Hi – this worked. thanks for your help!


#4

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.


#5

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#events


#6

Hi @Derek_Gervais,

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

Thanks !


#7

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).