Tracking events in Google Analytics with WordPress plugin Contact Form 7

If you’re looking for an easy way to create forms on your WordPress blog, I recommend the Contact Form 7 plugin. Once you’ve set up a form using this plugin, you’ll probably want to track its usage in Analytics. The Contact Form 7 plugin support site has published a quick tutorial on how to configure Analytics tracking of forms. However, it doesn’t work (at least, not anymore). When I tried to track my form by adding the code line of the tutorial to the Additional Settings, two problems arose. First, the form sent confirmation message disappeared, leaving visitors in confusion and overflowing my email inbox with duplicate mails of those confused visitors harassing the submit button. Secondly, event tracking still did not work. The problems are caused by an error that you can find via Firebug or console in Chrome’s Element Inspection:

“Uncaught ReferenceError: _gaq is not defined”

Fortunately, there is an easy way to solve this. Instead of using this line (as is said in the plugin site’s tutorial):

on_sent_ok: “_gaq.push([‘_trackEvent’, ‘Contact Form’, ‘Submit’]);”

Add the following line to the ‘Additional Settings’ field at the bottom of the contact form edit page:

on_sent_ok: “ga(‘send’, ‘event’, ‘category’, ‘action’);”

You’ll see that the confirmation message returns and events are being tracked in Google Analytics.