Tracking Form Submissions with Google Analytics

If you use Google Analytics to analyze web traffic on your site, I imagine you would like to track submissions through your contact forms as well. Contact Form 7 allows you to set up this type of tracking very simply. In this post, I’ll show you how to do it.

_trackPageview()

If you’ve set everything up correctly, Contact Form 7’s contact form behaves as an Ajax application and its form submission is an Ajax event. Google Analytics’ _trackPageview() JavaScript function is able to track an Ajax event without a real page view or redirection.

For example, let’s say that the name of your page tracker object is pageTracker, and you want to track the form submission as a virtual page view of the virtual URL /mysite/thank-you.html. In order to do this, you will need to run the following JavaScript code when the submission event occurs:

pageTracker._trackPageview('/mysite/thank-you.html');

Note that the /mysite/thank-you.html is not necessary for it to be a real URL. It can be any value, which will then be displayed as a page filename in your Analytics reports.

Using on_sent_ok action hook

You don’t need to edit any JavaScript files for running the above _trackPageview() function. Contact Form 7 provides a JavaScript action hook named on_sent_ok, and by utilizing this action, you can specify a JavaScript code that you wish to run after the form is successfully submitted.

It is quite a straightforward process for you to specify the aforementioned _trackPageview() with the on_sent_ok hook. You will find the Additional Settings field at the bottom of the contact form management page; now simply insert the following line into it:

on_sent_ok: "pageTracker._trackPageview('/mysite/thank-you.html');"

Note that you need to quote the codes properly and the code must be in one line.

on_sent_ok in Additional Settings

Now, if you have set everything up correctly, when someone makes a submission via your contact form, Google Analytics will track it as an access to /mysite/thank-you.html.

To verify that this is working correctly, you can check your Top Content report 24-48 hours after a submission. At that point, you should be able to see the assigned page name (/mysite/thank-you.html) in your report.

External resources

9 responses to “Tracking Form Submissions with Google Analytics”

  1. Eve @ Letters to Breathe

    I could kiss you! I have been trying to find a way to do this for two days, LOL I feel silly for not looking here first! Thanks so much- it works perfectly!

  2. Arthur Wetselaar

    Hi, thanks for your great form generator and the usefull info on this page. Would it maybe be better if this could be a general setting for all comments. And that you only have to enable the function and to fill in only the name of the tracker? Icing on the cake would be that I could enable or disable the general tracking per form and use a custom tracker if I wish?

  3. Jessica Davis

    This is great, the only issue I noticed is that the AJAX confirmation message no longer shows up. Did I do something wrong?

    1. Jing Liu

      I had the same problem. Looked at the code, realized that I used Ultimate GA (Google Analytics) so that when I am logged in the tracking code does not show on the page hence the script did not work.

      If you are using Ultimate GA, try logging out and try the form again.

  4. senlin

    Great to learn how to track form submissions.

    Where can I find how to combine this snippet of code with the
    on_sent_ok: “location.replace(‘domain.com/confirmation/’);” snippet I am using already?

  5. Thanks!! I appreciate this plugin and your work

    Man, you have just read my thoughts. Thank you a lot for your help and contribution to us :)

  6. Andreas Ostheimer

    In some cases it might be even better to use the trackEvent function of Google Analytics so that every sent form can be tracked as an Event.
    E.g.:
    on_sent_ok: “pageTracker._trackEvent(‘Formular’,'Absenden’,'Dr. Klein’);”

    See more on this topic here: http://code.google.com/intl/de-DE/apis/analytics/docs/tracking/eventTrackerGuide.html

    Andreas