You are here: Blog » Code Snippets » Google Analytics Track Form Submission

Google Analytics Track Form Submission

submitbutton

This Google Analytics track form submission snippet can be used to record every time someone submits one of your contact forms – or any form for that matter. The code looks slightly different depending on if you use analytics.js or the older ga.js tracking code.

Note that I always like to use event handles as opposed to inline event tracking as this makes for better code practice and it’s also much more convenient than having to add onEvent javascript code to every instance of a given html element. The problem with using event handlers is that sometimes they are too slow to pick up on the event and push the notification over to Google Analytics before the user leaves the page. For example, if you have your form set to redirect to a thank you page upon submission, the user might end up at the thank you page before google was able to track the event.

The key to making sure that the event notification has enough time to reach google analytics is to use a click() event handler instead of a submit() event handler. The click event should be added to the submit button for your form. In my testing, I found that when you check for the click() button event, the message is received by Google Analytics. However, if you check for the form submit() event, if the user is redirected to a thank you page on form submission, the message was always missed by Google Analytics.

Here is the code:

If you use analytics.js

If you use ga.js

This entry was posted in Code Snippets and tagged , , by .

Author: justin saad

Justin Saad is first and foremost a husband and a father of four. He is also a web developer and designer who has been making websites for over fifteen years. His company is called The Motech Network and his website is ClevelandWebDeveloper.com. In addition to building websites, Justin makes WordPress plugins and likes writing tutorials to help others learn about various aspects of web development.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">