Main

How to Send Events to Google Analytics with a Javascript Event Listener on Form Submission

A tutorial on how to send events to Google Analytics 4 (GA4) and Universal Analytics with a Javascript Event Listener triggered on form submission.

Nefer Lopez

1 year ago

Hi, my name is Nef and I help vacation rental owners get more direct bookings on their website. There are many different ways to send form submission events to Google Analytics. I've showed you how to do it with Typebot, which is a chatbot that has a code block where you're able to send data to the Data Layer and use Google Tag Manager to capture that data and send it over to Google Analytics. I've also showed you how to use the pre-built triggers inside of Google Tag Manager. This way you don't
have to use the Data Layer, but just target the specific form that you want to capture on Lead Submission, and then from there, capture the data and send it over to Google Analytics using Google Tag Manager. Now, in this video, I'm going to show you how to use an addEventListener using JavaScript to listen for every time a form is submitted on your website. From there, we'll be capturing that data, pushing it to the Data Layer, and then using Google Tag Manager to capture that data and then sen
d it over to both Google Analytics 4 and Universal Analytics. So, here we are in my Swipe Pages account for my Cabin Promos website. Going down to tracking codes, what you want to do is first install Google Tag Manager, and then before this closing tag, you want to embed the script. Let's quickly review what the script is doing. First, we're targeting a form with the ID of lead-magnet-form, and based on the submit event on that form, we want to push this data to the Data Layer. So let's head ov
er to the form and make sure that we have this ID set to lead- magnet-form so we can target it properly. Here we are in the form element. I've selected it and then also placed the lead-magnet-form as the ID. Next, let's use Google Tag Manager to preview these changes. Here we are in Google Tag Manager. We're going to go to Preview. A tab opened up with my website, and next we're going to go to the form and submit it and then go back to Tag Assistant to make sure that the generate_lead event fire
d up correctly. Going back to the Tag Assistant, we can see that the generate_lead event got fired correctly. If we click into the generate_lead event, we can see the tags that were fired and which ones were not fired. So here I created two tags to be fired once that generate_lead trigger was executed. I created a few videos on how to set this up. But if we jump over to analytics, this is actually Universal Analytics, we can see that the event was fired correctly. And if we hop over to Google An
alytics 4, we can also see that a generate_lead event was passed in and also converted as a conversion event, which is right here: generate_lead. So this is one way to capture events for form submissions to then send to Google Analytics. I also created another video on how to use the Form Submit built-in trigger within Google Tag Manager to do the same thing. See you in the next video.

Comments