Tracking Contact Form 7 events with Yoast and Google Analytics

David Nash wordpress 1 Comment

I discovered this while setting up Google Analytics when I recently re-launched my site.

I’m using the very popular WordPress plugin Contact Form 7 for a nice AJAX contact form. I’m also using the equally popular Google Analytics by Yoast (GAbY) WordPress plugin.

Google Analytics shows which pages users are viewing. I want to drive users to contact me, and I’d like to get information on how many people are doing that. But because it’s an AJAX form (ie, submits the data via Javascript), Google Analytics doesn’t register an additional page view. You have to “tell” it to do that.

At first I tried the instructions from Contact Form 7.

What it boils down to is editing your form in CF7, clicking the “Additional Settings” tab, and entering this:

_gaq.push(['_trackEvent', 'Contact Form', 'Submit']);

When I tested it, I didn’t get the usual confirmation message. I noticed this in the Chrome Web Developer console:

Uncaught ReferenceError: _gaq is not defined

So why is it undefined? Initially I thought it might be that CF7 is loading before GAbY, but that’s not the case.

The solution: 

By default, Google Analytics by Yoast doesn’t track Administrator and Editor user types. This is why _gaq is not defined. To test it as a user, I use Chrome’s “Incognito mode”. It’s an easy way to open a window where I’m not logged in.

Once I did this the Contact Form 7 form worked correctly.

I’m expecting the event to show up in Google Analytics under Behaviour -> Events. It’s been less than the 24-48 hours so the events are yet to show up in my Analytics account, but I’m pretty confident it’s working correctly now. If not, I’ll update this post.

Comments 1

  1. i was confused why the CF7 works occasionally. Normally i can received emails but sometime not. The GA shows that was an event but no email received. Can you help me out on this? thank you.

Leave a Reply

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