Triggering a Google Analytics event

Occasional Contributor

Hi,

We’re looking to trigger a google analytics event when someone submits a form, which is a HubSpot form embedded on our website.

The form is set up to display a thank you message, so we can’t trigger the event upon load of the thank you page, because there is no thank you page.

Wondering if anyone has any experience with this?

Thanks

Reply
0 Upvotes
4 Replies 4
Occasional Contributor

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

Reply
0 Upvotes
Highlighted
Occasional Contributor

Thanks @pmanca .

I’m not a developer so struggling to get this to work… I have the code that needs to be linked, but not sure how to link it using the onFormSubmit string within the form embed.

Here is the embed form code:

hbspt.forms.create({ sfdcCampaignId: 'XXXXXXXXX', portalId: 'XXXXXXXXX', formId: 'XXXXXXXXX' }); Then the code that has been sent across to be added looks like: dataLayer.push({'event' : 'formSubmitted', 'formName' : 'consultation'}); function onFormSuccess(event) { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event' : 'formSubmitted', 'formName' : 'consultation' }); } { form.onSubmit(function(){ dataLayer.push({ 'event': 'formSubmitted' 'formName': 'consultation' }); }); });
Reply
0 Upvotes
Occasional Contributor

@Champion Read through the documentation I posted above. You need to have the link in your hbspt.forms.create function. so underneath formId you would add it.

Reply
0 Upvotes
Top Contributor

@Champion did you manage to make this work? I’m very interested in your final code

Reply
0 Upvotes