menu
Contact CreareQuick Online Quote

Goal Tracking with Contact Form 7 in Universal/Classic Analytics

One of my favourite plugins for WordPress is Contact Form 7, a free and powerful plugin that allows developers to manage multiple contact forms on their WordPress site. However as the forms are processed within an AJAX application it’s not immediately obvious if and how you can track form submissions as goals in Google Analytics.

The great news is that you can and it’s very easy to setup. In this quick tutorial i’ll walk you through setting up a new goal and the code you’ll need for both ‘Classic’ and the shiny new ‘Universal’ Google Analytics.

Setting up the Goal in Google Analytics

So let’s start by setting up the goal in our Google Analytics account. Login to GA and head over to the Admin panel of your select domain profile. Select Goals from within the View (Profile) menu:

cf7-goal-step1

Next click on ‘Create a Goal’ to begin setting up the new goal that you’d like to track for Contact Form 7. In Step 1 click on ‘custom’ rather than ‘template’ from the initial set-up option. Now within Step 2 simply choose a name for your goal like ‘Contact Page’ and choose ‘Destination’ from the ‘Type’ options:

cf7-goal-step2

Finally in step 3 choose ‘Equals to’ for the destination and set a ‘fake’ URL that we can use to track the goal. I’ll explain later on why this Goal Destination is ‘fake’ but for now, simply pick a URL that doesn’t exist (and would therefore 404) that you can assign to the goal.

cf7-goal-step3

Complete the goal as necessary to finish the set-up of your new goal. Remember your ‘fake’ Destination URL as we’re going to ‘ping’ this imaginary path in the Contact Form 7 plugin next.

Pinging the GA Goal from Contact Form 7

As I mentioned earlier, CF7 forms work as AJAX applications – which means the data is submitted and processed within an AJAX event. When users successfully complete a CF7 form, they’re not taken to a new ‘thank you page’ URL by default, which can make tracking the event tricky.

However, both the Classic and the new Universal Google Analytics provide functions that allow you to trigger or ‘ping’ page-views whether you’re actually on that specific page or not. To take advantage of these functions, Contact Form 7 includes a really useful JavaScript Action Hook called ‘on_sent_ok’ that can trigger custom JavaScript events when the form has sent successfully. Depending on whether you’re using Classic (ga.js) or the new Universal (analytics.js) Google Analytics code, you’ll need either one of the following lines of code:

Universal Analytics Code

on_sent_ok: "ga('send', 'pageview', '/your/url');"

Classic Analytics Code

on_sent_ok: "_gaq.push(['_trackPageview', '/your/url']);"

Remember that Goal URL that we created earlier in Google Analytics? Swap /your/url in the code above for the path that you set up. If you now navigate to your individual Contact Form 7 form settings, scroll down to the bottom to to the ‘Additional Settings’ field:

cf7-insert-code

Paste in the line of code like in the example above. Now, once our form is successfully completed, the on_sent_ok Action Hook will trigger our ga() or _gaq.push() functions that will ping our goal within Google Analytics.

That’s it!

  • caledonianmercury

    Hi James, I am using Google Tag Manager and have successfully set up a Goal (event rather than destination) for tracking Contact 7 form submissions, but having done so realised that I cannot set up funnels for events in Google Analytics.

    I was pleased to find your post as I am now looking to find how I could track Contact 7 form submission in the way you describe above i.e. using a destination page. Clearly using a destination page allows setting up of the funnel path and this is useful when you are looking to optimise pages for conversion. Unfortunately the approach you described above didn’t trigger/ping page view.

    • http://www.creare.co.uk/ James Bavington

      Sorry to hear that it didn’t work. For your Tag Type are you using the ‘Google Analytics’ or ‘Universal Analytics (beta)’ Tag type? In order for CF7’s Action Hook to ping GA, you need to make sure you’ve added the corresponding code from the two options above.

      • Stefan Jacobsen

        Hi James. I am using Universal analytics and Tag manager, but I cant figure out how to track my leads from Contact7 form. Could you please explain how to do this? What codeline should I paste at contact7 form and what tag-type and firing-rules should I add at GTM? Thank you!

        • http://www.creare.co.uk/ James Bavington

          Hi Stefen, are you just using the GTM at the moment to bring out your Analytics code? Could you DM me a link to your site that it’s installed on?

          • Stefan Jacobsen

            Hi. what is your email address?

          • Stefan Jacobsen

            Yes I am using GTM. Please see link attached.

          • http://www.creare.co.uk/ James Bavington

            Hi Stefan, I’ve tried inspecting your GTM code, but the URL of the iFrame seems to 404 which may suggest that it’s not installed correctly? Therefore I don’t think any tracking data is being captured? Are you receiving any data in analytics?

          • stefan

            Hi James. Yes I am receiving other data in Analytics. I debugged GTM and all rules were fired correctly at different pages.

    • Xavier

      traditional GA code doesn’t work if you use Google Tag Manager. You’d need to find out the tracker object firs.

      • Stefan Jacobsen

        Can you please explain this: “You’d need to find out the tracker object firs.”?

        • http://www.creare.co.uk/ James Bavington

          Hi Stefan, is this an error message you’re receiving whilst using the Tag Manager like Xavier?

  • Pepe

    Hi James! Does the results appear in Analytics in real time? I’m trying to do the goal tracking through a plugin extension for CF7, dinamically changing the fake URL depending on a variable. It doesn’t seem to work, but maybe I have to wait some hours, perhaps days to see it in Google Analytics admin page?

    Thank you very much!

    • http://www.creare.co.uk/ James Bavington

      Hi Pepe, if you set the reporting range to include the current day – you can start to see results coming through after a few hours. Best thing to do is to leave it 24 hours and review the day’s testing the following day.

  • Tessa

    Hi James,
    I’ve added the UA code above as per your instructions and the contact form 7 form has stopped displaying the ‘Your message was sent successfully’ message and doesn’t clear the contact form after hitting Send. I’ve removed the code and the successful completion message etc works fine. Any thoughts as to what in the UA code would be stopping these two things from working and how I could get both to work at the same time?

    • http://www.creare.co.uk/ James Bavington

      Hi Tessa, is the form posting and sending the message and pinging the goal? If you’re using Chrome or Firebug, are you able to open the console and try submitting the form to see if there are any JavaScript errors shown? Thank you Tessa.

      • Siobhan

        Hi James,

        I’m getting this same issue with the ‘Your message was sent successfully’ message no longer appearing after the set up of the tracking code (I also have no events showing up in Analytics yet but it’s been less than an hour since the test so may be too soon to tell).

        Using Chrome I’m getting the following error message in my console when a form is submitted. The error message appearing is “Uncaught ReferenceError: ga is not defined”. Do you know what might be causing this?

        Thanks

        • http://www.creare.co.uk/ James Bavington

          Hi Siobhan, it sounds like you might be using the wrong code snippet from above. Your error message suggests that you’re using Classic Analytics, rather than the new Universal Analytics tracking code. Try using the Classic code snipped from above (_gaq.push), and replace the one you have in place (_ga). That should hopefully remove the console error, bring back the ‘Your message was sent successfully’ message and also ping the goal in GA. Let me know if that works.

  • http://www.ragewebdesign.co.uk/ Nick Baldwin

    Added it… waiting to see if google records it now!

    • Catherine

      Hey Nick,

      Did this end up working for you and if so how long did it take to show that it recorded?

      • http://www.ragewebdesign.co.uk/ Nick Baldwin

        It did work and it took at least a day and a half to show up! I was on Classic GA! Very useful.

        • Mark McKay

          So to confirm, it takes a couple of days for the info to show up? I am using universal analytics. Also, can you tell me, why do we do use the phantom URL method instead of tracking this as an event? Thanks.

          • http://www.creare.co.uk/ James Bavington

            It can take about 12 hours yes before it shows up. As per my reply to your other comment, please make sure you use the ga() code snippet rather than _gaq.push() if you’re using the Universal tracking code.

  • James J

    Hi James. This works. Thanks a lot. It’s now tracking form submissions that come through organic searches which is great. Do i need to add anything else in order to track form submissions that come through and Adwords campaign? I would like to to be able to see which form submissions come from organic searches and which come from PPC. At the moment I just have the on_sent_ok: “_gaq.push(['_trackPageview', 'my/url']);” in the Additional Settings section of the CF7 panel. Would i need to add anything else here or would Analytics automatically detect which users had come through Adwords?

    Thanks

    • http://www.creare.co.uk/ James Bavington

      Hi James, unless you’re tracking any specific custom goals, then Google Analytics can automatically break this data up for you. Simply navigate to the Goal Overview then apply the Paid Search Segment.

      • James J

        Hi James. Thanks for your reply. We’re tracking ok but what we need to do is to display all our Analytics data on the submitted form that gets sent through so that the sales team can load the leads into their CRM. We are using a plugin by Found for this. Unfortunately we can’t seem to display the correct information when the lead comes from Adwords. So, we would like to see the Campaign name and campaign term which are correctly recorded in Analytics. It comes through saying the enquiry was from PPC but the rest of the info is missing. It seems that when you connect Adwords to Analytics the cookies get changed and a new parameter called ‘utmgclid’ appears – Google stores the AdWords campaign data (encrypted) here and it is only read by Analytics. There seem to have been numerous attempts to decode ‘utmgclid’, but nobody has succeeded?

        At the moment, there doesn’t seem to be a solution that would allow us to get the data sent on the form form and link it to Analytics as well. You can of course unlink AdWords form Analytics, but then there will be no advert statistics in the Analytics panel. Any ideas would be much appreciated. Is driving us mad. Thanks

  • Nombre

    Comentario

  • http://www.concept-pl.us Konstantinos Christodoulakis

    Hello James,
    I created a new destination goal as you described on GA Universal and I added on_sent_ok: “ga(‘send’, ‘pageview’, ‘/goals/subscription-page’);” on Contact7 additional settings.
    It’s 24h after this and no data are loaded on GA for Goal Completion :(
    Any ideas?

    • http://www.creare.co.uk/ James Bavington

      @konstantinoschristodoulakis:disqus, please could you tweet me your URL offline and I can take a quick look to see if I can see anything @jamesbavington

      • Stephen Lessey

        Hi James, I am having the same problem. I followed these instructions and for some reason, i’m not getting any data. Can you help?

        • http://www.creare.co.uk/ James Bavington

          Hi Stephen, if you can tweet me your URL I can happily take a look for you and try and help.

  • Farhan Mahmood

    Hi James,
    Thank you for this great tutorial, it works.
    I have a question, I have two different CF7 forms on my homepage and I’m using their shortcodes in homepage template, although goal tracking works (I can see green successful submission alert) but all submissions are not registered, e.g,. I submitted forms 4 times (two times both forms), checked after 24 hours and goal conversion was 3, then I submitted 4 times again, after 24 hours conversion was 2.
    May be two different form (side by side) in same template is causing the problem? Your suggestion?

    Thanks,

    • Farhan Mahmood

      Sorry for my bad english. The second time I submitted forms goal conversion was increased by 2, not that it was reduced to 2 from 3! :P
      So when I submitted forms first time, goal conversion was 3, after second time, goal conversion is 5.

      Thanks,

  • http://notyouraveragedad.com Mark McKay

    Does this line of code also need to be added to the Google Analytics code in the footer of the page? on_sent_ok: “_gaq.push(['_trackPageview', '/your/url']);”

    • http://www.creare.co.uk/ James Bavington

      Hey Mark, no it only needs to be added into the additional settings in the plugin configuration. Just make sure though that if you’re using the new Universal analytics tracking code you use the ga() not _gaq.push() version of the code, otherwise it won’t track the conversions. Hope that helps.

      • http://notyouraveragedad.com Mark McKay

        Thanks James. My form is for people to RSVP to an event. Once the form is completed it sends an email to a generic email account. When I look at the analytics, it shows me that there were 60 goals completed, but I only received 8 emails confirming the RSVP. Any idea on why the discrepancy between completion and the actual number of emails received? Thanks.

        • http://www.creare.co.uk/ James Bavington

          Hi Mark – if you could email me or Tweet me your site’s URL etc, I can happily take a look for you to see if I can work out why.

  • Alek

    Hi James, great article, works as advertised. Is there a hook to track if the submit button was clicked but the email was not sent? Basically a way for me to track if there were any issues with the submit form.

    • http://www.creare.co.uk/ James Bavington

      Morning Alek, I don’t believe there’s a hook within CF7, however you could simply track clicks of the submit button itself as a custom event. My other tutorial here shows how to set-up specific event tracking on things like links and buttons, you just need to ensure the submit button has an ID or class you can identify: http://www.creare.co.uk/track-custom-events-universal-ga

  • Dave

    when I add this code the “thank you” messages disappears

    • http://foreverbarcelona.com Marta Laurent Veciana

      Same problem here… Thank you message disappears…

    • http://www.creare.co.uk/ James Bavington

      This is usually indicative of a JavaScript error. First thing i’d check is that you’re using the right tracking code above (i.e Universal vs Classic) with the right supporting code. Happy to take a look if you’re able to post or DM me your URL.

      • sc456a

        Thanks, James. That was my issue. I had to switch my Analytics plugin to Universal.

        • http://www.creare.co.uk/ James Bavington

          Cool, thanks for letting me know, glad that’s working now.

  • http://www.creare.co.uk/ James Bavington

    Hi, the issue you’re experiencing is a result of a JavaScript error. At the moment, you’re page is set up with the Classic Analytics tracking code, but you’re using the Universal Analytics code snippet (_ga). Do you get the same problem when you do change the code to the _gaq.push() method from the post? If you could switch this over to me, I can take a look – or you could leave the code in as-is now, and change your GA plugin from Yoast to something like WP Headmaster (which allows the Universal Tracking Code) or by manually adding it to your header.php file.

    • http://www.kanesimms.com/ @kanesimms

      Thanks for taking a look at this for me James, it’s much appreciated.

      I tried both the ga() and the _gaq.push() on the form and they both don’t show the ‘thank you’ pop up after the form has been submitted.

      Weirdly, my GA account has given me the UA code, but the Yoast plugin doesn’t support UA just yet. Is there any way I can override this (even temporarily)? If I was to remove the plug in and just manually slip the code into my theme’s header.php, would that work?

      I’ve changed the for back to gaq.push() now if you wouldn’t mind taking a look?

      • http://www.creare.co.uk/ James Bavington

        Hi Kane, I’ve just done a test message, and the form seemed to work OK for me? No JavaScript errors and I got the success message and the form fields cleared. Let me know if you received my test message, and hopefully that will look as a goal in a few hours?

        If you’re looking to add the UA code from Google (which is Universal Tracking Code), yes I’d recommend disabling Yoast’s plugin and manually adding the code into your header.php file, just before the closing tag.

        • http://www.kanesimms.com/ @kanesimms

          Hi James,

          Thanks again. Yep, that’s what I did, I manually added the tracking code, just waiting to see whether it tracks the goals in GA now.

          Thanks so much for your help and support (and the post!), it’s hugely appreciated.

  • http://www.creare.co.uk/ James Bavington

    Hi, I’m not seeing Google Analytics installed on the page, is the above the right link?

  • http://www.sparkgrowth.com Darren Carter

    Hi, James. Thanks for the great post!

    I’m not able to get Contact Form 7 3.8.1 to work with GTM & UA for event tracking. I’ve added the following to the additional settings this form:

    on_sent_ok: “ga(‘send’, ‘event’, ‘Form Tracking CF’, ‘Contact Us’);”

    on this page:
    http://www.warm-transfer.com/contact-us

    No events are generated after successful form submission. This goes for two other forms, too. BTW, can I troubleshoot this using real-time event tracking in Analytics to your knowledge? I was thinking I should be able to.

    Any thoughts would be greatly appreciated!

    I am able to submit events using GTM tags. However the AJAX processing works, I’m not able to use GTM form validation so I have to count every click on submit buttons. Since this will over count actual successful submissions, I’d love to get event tracking CF7 working.

    Cheers!

    • http://www.creare.co.uk/ James Bavington

      Hi Darren, I don’t use the GTM myself, so I’ve not experienced the potential incompatibility I’m afraid. Have you tried adding the UA tracking code inline temporarily to ensure the events track normally, to point point the issue?

  • http://www.albinomedia.co.uk/ JJ

    Hi James, excellent post, I have done similar but using events instead of virtual pageviews. The issue I’m having is that I can’t get the on_sent_ok hook to do two things at once. i.e. I want it to track an event/pageview, then redirect to a thank you page after form submission. Any ideas?

    • http://www.creare.co.uk/ James Bavington

      Hi JJ, what code are you adding into CF7? If you could paste into here, I can take a look to see if I can help.

  • Joanne G

    Hi there,

    I’m having trouble with this and feel like I’ve tried everything.

    As soon as I updated to universal analytics (added demographics code too), tracking in contact form 7 stopped working.

    I updated the code in additional settings – UA to on_sent_ok: “ga(‘send’, ‘pageview’, ‘/your/url’);”

    and even tried it as an event – on_sent_ok: “ga(‘send’, ‘event’, ‘contact’, ‘enquiry’, ‘ContactForm’);” …but no joy.

    Even tried to use the code that directs people to an actual thank you page but that won’t work either.

    If anyone has any ideas it would be much appreciated!

    Thanks,
    Joanne

    • http://www.creare.co.uk/ James Bavington

      Hi Joanne, if you could tweet, email or direct message me your URL I can take a look for you to see if I can help.

      • Joanne G

        Thanks James! Just followed on Twitter, or if you could let me know your email :)

  • http://www.patricealbertus.net/ Patrice Albertus

    Hi James,
    I’m also having trouble to make it work and I think this issue is still open ?

    • http://www.creare.co.uk/ James Bavington

      Hi Patrice, sorry to hear that. I’ve managed to work with most of the people who’ve commented, offline and resolved the issues they’d been having. What problems are you having with your integration?

      • http://www.patricealbertus.net/ Patrice Albertus

        I’m still having the same issue, Contact Form 7 don’t fire the event on my website. :(
        I added the but form get sent properly, but no event pushed. URL sent via Hangout

  • Daniel

    Same issue as Patrice. The Universal Analytics Code is populated correctly (I think). The CF7 Additional Settings fied has been populated with the respective code as well (on_sent_ok: “ga(‘send’, ‘event’, ‘Enquiry’, ‘Submit’);”). But no joy. No events. Nothing in real time. Nothing in non-real time. Been trying all day, and nothing.

    http://www.trifecta.com.sg/contact-us-novena/

  • soma

    I’m so happy that this has been worked for me. I just used classic code in universal analytics.. thanks