Tracking Form Submissions with Google Analytics

If you use Google Analytics to analyze web traffic on your site, you may also want to track contact form submissions. In this article, I’ll introduce a simple way to track Contact Form 7 form submissions with Google Analytics.

Note that the JavaScript code used in this article is based on the latest version of the Google Analytics library at the time of this post (analytics.js). If you use the legacy library (ga.js), you need to change the JavaScript in the following code examples according to Google’s guide.

First, take a look at the HTML source of your site and confirm that you have a code snippet like the following:

<!-- Google Analytics -->
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
<!-- End Google Analytics -->

If not, you haven’t finished installing the basic page tracking code for Google Analytics yet, so install that first.

Event Tracking

Contact Form 7’s AJAX form submission can be tracked as an event. You can send a contact form submission event to Google Analytics with the following JavaScript function call:

ga('send', 'event', 'Contact Form', 'submit');

You don’t need to edit any JavaScript files to run this code because you can utilize on_sent_ok action hook, explained in the next section.

Using on_sent_ok

Contact Form 7 provides a JavaScript action hook called “on_sent_ok” to specify a JavaScript function that you’d like to run after a form submission successfully completes.

To set the event tracking code mentioned above to the hook, open the Additional Settings tab in the contact form editor screen, and insert the following line into the field:

on_sent_ok: "ga('send', 'event', 'Contact Form', 'submit');"

Don’t forget to quote the code properly, and make sure all of the code is on one line.

That’s it. Now, if you have set up everything correctly, Google Analytics will track successful form submissions through the contact form as an event with “Contact Form” as the event category, and “submit” as the event action.

To verify this is working correctly, you can check the Behavior > Events > Overview report page on Google Analytics 24-48 hours after a submission. At that point, you should be able to find tracked events there.

Just another contact form plugin for WordPress. Simple but flexible.