APIs & Integrations

ChampionComms
Member

Triggering a Google Analytics event

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

0 Upvotes
4 Replies 4
troelsfeodor
Top Contributor

Triggering a Google Analytics event

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

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Triggering a Google Analytics event

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

0 Upvotes
ChampionComms
Member

Triggering a Google Analytics event

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' }); }); });
0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Triggering a Google Analytics event

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

0 Upvotes