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
1 2 3 4 |
//track form submissions with this snippet if you use analytics.js jQuery(".yourform .yoursubmitbutton").click(function(){ ga('send', 'event', 'yourcategory', 'contact form submitted', jQuery(location).attr('href')); }); |
If you use ga.js
1 2 3 4 |
//track form submissions with this snippet if you use ga.js jQuery(".yourform .yoursubmitbutton").click(function(){ _gaq.push(['_trackEvent', 'yourcategory', 'contact form submitted',jQuery(location).attr('href'),, 'true']); }); |