Please note – this method is now deprecated and there is therefore a new preferred way to track WordPress Contact Form 7 via Tag Manager in Google Analytics

Tracking goals through contact forms in WordPress is important for a lot of businesses. It’s been possible now for some time, but with many businesses now moving to Google’s recently out of Beta ‘Universal Analytics‘, many code snippets need updating.

After creating a form within the user interface, click into the edit view and scroll to the bottom. Past the standard response messaging, there is an additional settings box. This area can be used to fire JavaScript calls upon different form states. We’re going to focus on the on_sent_ok hook, as that fires once the form has validated successfully and triggered the email send.

Google (Universal) Analytics provides a good event tracking guide here. In essence, the send event that Google is expecting looks like this;

ga('send', 'event', 'category', 'action', 'label', value); // where value is a number.

For those of you using Yoast’s WordPress SEO plugin, or those with implementations where the ga variable has been renamed, please note that you will need to reflect this new name in your function call. I.e. If you’re using Yoast’s excellent plugin, the line above would actually be this;

__gaTracker('send', 'event', 'category', 'action', 'label', value); // where value is a number.

Of these, the label and value attributes are optional, but sometimes useful if you are tracking goal values – the value you pass in to this event will be used in your reporting.

To translate this into WPCF7 events, we’ll need to wrap this in the ready-provided hook we discussed earlier. The end result looks something like this – though note I have removed the optional values for our tracking.

on_sent_ok: "ga('send', 'event', 'category', 'action');"

Be careful to note the usage of single and double quotes, otherwise you may run into unexpected issues. Once pasted in, you should see something that looks like this, below.

universal analytics wordpress contact form event tracking

Once this is configured, you’ll need to set up your event within Google Analytics. Log into your account, select the profile your goal should apply to, and select Admin from the top menu. You should see the following. Select goals from the third menu;

Impression Google Analytics (Universal)

When presented with a list of your already set up goals, select New from the action buttons, then select ‘Contact us’ from under the Inquiry goal type. Under the next setting, you’ll be asked to set the type of Analytics event that triggers the goal. We’re using events in this example, but you could easily change out the Analytics code above to fire page views instead (for instance you might open a thank you page in a pop up or something similar).

Once ‘event’ has been selected from this step, move onto the next step to set up your event conditions. If you are following this example exactly, you should set up your goal like so;

Universal Analytics Goal Setup

If not, then copy your attributes across from the event firing code you entered into the WordPress Contact Form 7 ‘Additional settings’ box above.

The final step will allow you to verify the goal and finalise the setup. If you are completing this tutorial in one go, then the goals historical conversion rate will be 0%, so don’t be alarmed.

If you’ve found this post useful, please let me know in the comments. Or similarly, if you are interested in any of our services; SEO, PPC or web design, then don’t hesitate to get in touch.

Aaron Dicks

Performance Director

Performance Director of Impression. Search engine optimisation, paid media and web analytics consultant. Also programmer and digital all-rounder. @aarondicks

Aaron has specialist knowledge in SEO, PPC and Analytics Consultancy.

57 thoughts on “Event tracking in WordPress Contact Form 7 (Universal Analytics)

  1. Optimix says:

    Thank you for usefull article 🙂 Just right I was searching for.

  2. Joanne G says:

    Agh this isn’t working for me – do you think me adding the demographics snippet to the UA code would have affected anything? Racking my brains..

    Thanks 🙂

    1. Aaron Dicks says:

      Hi Joanne
      Can you paste in the code that’s rendering in the page’s source code? Are you seeing any errors in console? Also specifically, which bit isn’t working – the event tracking?

      1. Joanne G says:

        Thanks for your reply Aaron,

        UA code is..

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

        ga(‘create’, ‘UA-xxxxxxx-x’, ‘auto’);
        ga(‘require’, ‘displayfeatures’);
        ga(‘send’, ‘pageview’);

        and in contact form 7 additional settings I’ve tried..

        on_sent_ok: “ga(‘send’, ‘pageview’, ‘/your/url’);” and on_sent_ok: “ga(‘send’, ‘event’, ‘contact’, ‘enquiry’, ‘ContactForm’);”

        tried to set these as goals (destination and event) and it’s just not tracking.

        Thanks 🙂

        1. Aaron Dicks says:

          That looks ok to me… Do you want to send me the URL privately?
          Also, where is your code placed in the template?
          Can you see any errors in console?
          If you type ga into the console dialog, is the GA object returned?


          1. LOCA says:

            I followed the above instructions exactly but don’t see anything in event in GA.. any ideas why?

  3. Brian Swanick says:

    This worked really well, thanks for the info!

  4. Robert Essien says:

    Great detailed article! Unfortunately like Joanne G, it’s not tracking for me. Used exact code and GA setting from this article. Any ideas?

    1. aarondicks says:

      Hi Robert. Where in your HTML is your GA code? Are you getting any errors in the browser console when this is firing? Aaron

      1. Robert Essien says:

        Thanks for your reply Aaron! I’m using Super Simple Google Analytics WP plugin – checking the source code it’s just before the closing head tag. Yes in the Browser Console I’m getting an error when submitting forms “ReferenceError: ga is not defined”

        1. aarondicks says:

          Hi Robert. What that means is you’re firing your event call before the GA code is running. Are you definitely using Universal analytics?

          If you are, then it’s to do with the priority in which you’re loading the scripts on to your website.

          Hope this helps?

          1. Robert Essien says:

            Thanks Aaron! Yes that link helped me realise that the plugin I’m using was inserting the old analytics code and not universal anayltics. I’ve changed plugin, which should hopefully solve the issue now. Thanks again for your help – much appreciated!

  5. Hemilton Tuco says:

    Thanks!!! Worked perfectly!

  6. Chad Warner says:

    Thanks! I used this for the contact form on a landing page.

  7. John-Pierre Cornelissen says:


    This original code stopped working, it produced the error “ga is not defined” again. Ihad to replace the first part to get it working again.

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

    New one
    on_sent_ok: “__gaTracker(‘send’, ‘event’, ‘category’, ‘action’);”

    Not sure when this happened, maybe after an update of the yoast analytics plugin.

    1. Tiziano D'Angelo says:

      Thanks a lot, I have the same issue, but the error keeps going: __gaTracker is not defined

      1. John-Pierre Cornelissen says:

        Hi Tiziano, after writing the above I found out that it indeed was caused by an update of the Yoast Google Analyitics plugin. Recent versions (I use the most recent v5.2.8 version) use __gaTracker which you can see in the code they insert in the source code of the page.

        1. Tiziano D'Angelo says:

          Sorry John-Pierre, you are indeed right. My CF7 form had already __gaTracker while I thought it was ga.
          I checked the script code and it has indeed __gaTracker while on Analytics admin the script code shows ga.

  8. businessa2z says:

    This is an absolute pain to setup with a catalog of issues it begs belief that google makes something so fundamental to their product so difficult to setup. Do you have any examples of tracking success with this particular goal setting using WP and CF7 ?

  9. Phillip Lind says:


    Thanks for a great article!

    I just did these things, but cf7 dosen’t show me the “message sent” anymore – and the data in the form dosen’t reset.

    And furthermore i can’t see if it shows the goal in GA.

    The site is:

    I made attached some screens.

    Hope you can help me out here 🙂

  10. Thibaud CLEMENT says:

    We encountered the same problem as many people, i.e. the “Thank you” message was not appearing any more as soon as we were pairing the contact form with Google Analytics.

    Here is how we solved it:

    1. We made sure we used a double quote between on_sent_ok and __gaTracker (thanks @aarondicks:disqus for your help on this)

    2. We replaced ‘sent’ by ‘submit’ in our code.

    Here is the code we ended up using:

    on_sent_ok: “__gaTracker(‘send’, ‘event’, ‘Contact’, ‘Submit’);”

    Hope that may help someone.

    1. zgall1 says:

      I used your suggestion but my goals are still not registering. I have tweeted @aarondicks:disqus in the hopes that he might be able to take a look.

    2. Aaron Dicks says:

      Thanks, Thibaud Clement, I’ve only just seen this comment. Thanks for explaining your solution here for everyone to see.

    3. nm says:

      is Submit the name of your action?

      1. Aaron Dicks says:

        In this example, yes. Here’s a full resource:

        I hope the below helps;
        ga(‘send’, ‘event’, ‘CATEGORY’, ‘ACTION’, ‘LABEL’, VALUE);


  11. Rémi Bonnaud says:


    I am kind of lost after testing 10 differents codes and goals as I have no conversion data in my Google Analytics.

    I am going to try exactly what you said in this article and hope it works.

    1. Aaron Dicks says:

      Hi Rémi, sorry I didn’t reply sooner. Are you using any plugins that might mean you are renaming your GA object. For instance, if you’re using Yoast’s Analytics Plugin, the ga object is renamed to prevent it clashing with manual code insertions. Perhaps try using this new name for ga() -? __gaTracker()

  12. Keesjan Deelstra says:

    Hi, do you guys also know of a way to collect the GA Aquisition sources to the emails send? I found but that one is not for universal analytics. I did found a tutorial a few days ago but cannot find it anymore ;-( Something with hidden fields.

    1. Aaron Dicks says:

      Hi Keesjan,

      i have achieved this before using this PHP class, but not with WPCF7 I’m afraid. It’s something I want to look into again in the future.

      I’m not sure whether it works with Universal Analytics, though.


  13. John-Pierre Cornelissen says:

    Here is some trouble shooting information with issues I ran into myself when configuring this:

    1) In Google Analytics, make sure you have an ‘unfiltered’ view. I have for example a view that filters traffic from my own IP address, so whenever I test a contact form goal completion from my own computer, the goal completion won’t be registered in that view. Therefore, to test goal completion I also have an unfiltered view where the goal completion will be registered. Needles to say that when you look at your report you’ll need to look at the data from the unfiltered view!

    2) Verify that that the words you use for ‘category’ and ‘action’ in the contact from 7 settings are the same as the once you have entered in the Google Analytics Event conditions.

    3) If you use the WordPress Plugin Analytics by Yoast with the Univeral Analytics setting enabled, you’ll need to enter a code in contact from 7 settings that is different then the one mentioned in this article.
    This article says:
    » on_sent_ok: “ga(‘send’, ‘event’, ‘category’, ‘action’);”
    With Yoast Universal Analytics you need to use
    » on_sent_ok: “__gaTracker(‘send’, ‘event’, ‘category’, ‘action’);”

    If you’re unsure, have a look at the page source and search for the part where it says “This site uses the Google Analytics by Yoast”. If you see something like the following you’ll know you need to use the __gaTracker code.
    » (window,document,’script’,’//’,’__gaTracker’)

    4) Again, if you use the WordPress Plugin Analytics by Yoast, there is an ‘ignore users’ setting on the general tab where you enter the WordPress roles that should be ignored. If you have entered your role there, e.g. administrator, and you submit the contact form while you are logged in to that WordPress website, then Yoast has stripped the Google Analytics code from the page. CF7 will trigger the on_sent_ok but since the analytics code doesn’t exist on the page it results in an error (you only see that in the console of your browser) so the goal is not registered. The only thing you see when that happens – or actually not see – is that you don’t see the CF7 confirmation message.

    I hope this helps solving most issues with CF7 and GA.


  14. acg2891 says:


    Thanks for your article. I am experiencing some technical problem with it though. If I add:

    on_sent_ok: “ga(‘send’, ‘event’, ‘Contact Form’, ‘sent’);”

    when contact form is sent, there’s no “Thank you message” like it used to be, therefore users think they didn’t submit properly. Not sure what to do and I am also wondering if when the contact form is submitted the url should change to something like

    This is the second time I try to add this code. First time I did the entire process described but nothing was tracked on GA either.

    Can anyone help, please? Really struggling here.

    Thanks very much, Alexandra

    1. John-Pierre Cornelissen says:

      Hi Alexandra, did you check all of these tips?

      With your on_sent_ok code the url should not change. It is possible if you want that though, check here:


  15. Guest says:

    I just want to say, that i also had the problem like the rest of you.. The Thank you message would not appear after i added the code.

    I ended up trying all kind of stuff. The thing that got it working for me, was to move the double quote one to the left, so it would not be placed before the closing tag ;

    my code ended up being:

    or for people using Yoast

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

    1. IP3 Design says:

      This worked to get the Thank you message to show. My code looks like this and it tracks:

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

  16. Jannik Boldsen says:

    I just want to say, that i also had the problem like the rest of you.. The Thank you message would not appear after i added the code.

    I ended up trying all kind of stuff. The thing that got it working for me, was to move the double quote one to the left, so it would be placed before the closing tag and not after.

    my code ended up being:

    or for people using Yoast

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

  17. Davide De Maestri says:

    Thank you!

  18. Marcos Marcon says:

    Any update? I am not being able to make it work…

  19. Alek says:

    Great article,

    My question is what if I don’t want to track on_sent_ok but quite the opposite. I want to track any submit button click. Meaning if someone clicks on the submit button and the form doesn’t send, can I track that somehow?

    Thanks in advance!

    1. aarondicks says:

      Hi Alek,
      No problem – that’s possible. Can you edit the onclick event of the element? If so, just use that in a similar way to how we’re passing in the ga event call.
      If you can’t edit the element, then perhaps in jQuery you can implement a small code snippet that listens to button clicks, and then triggers the event that way.
      Why is it that you’re trying to track all button clicks, and not just valid ones?

      1. Alek says:

        hi Aaron,

        Haven’t had a chance to try this out yet, but thanks for the quick reply.

        As to your question, its more of a QA for me to make sure that when people come to the page and fill out the form when they click on submit i get the submitted email. it’s for an appointment request page so its critical that I have every submit click work. If i know that they are clicking and i’m not getting an email that means something went screwy and they didn’t just leave that particular page. Then i go in and try to pinpoint what OS/device/browser combination they were using to make sure none of my custom code is messing anything up. I’ll post as soon as I try out your suggestion!

  20. Alex says:

    For those who uses Google Tag Manager on his WordPress website and has implemented the Google Analytics tracking code with the help of it (GTM), the instructions in the article above won’t work. Only after implementing event triggering via data layers in Google Tag Manager my Contact Form 7 submissions are being tracked again!

    I followed this instructions (direct implementation without plugin):

    1. David Coghlan says:

      Thanks for sharing Alex, this method is awesome, worked like a dream.

  21. Olaf Poneta says:

    thank you.

  22. Ivan Kozlovskiy says:

    You can using with popup massage:
    on_sent_ok: “changePopup(‘#message’); ga(‘send’, ‘event’, ‘Contact Form’, ‘sent’);”

  23. Mark says:

    Great article,

    Events are the soul of our websites!

    Matt, what about using an innovative solution to track your event s?

    We just built please, check it out


  24. Mark says:


    Matt, what about using an innovative solution to track your events?

    Events are the soul of our websites!

    We just built please, check it out


  25. jwwietsma says:

    Thank you so much! Your article finally showed me why my custom eventtracking didn’t worked.

  26. AgnesLP says:

    Hi, When I use this code, the form works well, but CF7 stops displayign confirmation message. Any workaround?

    1. aarondicks says:

      Hi Agnes, if you’re inspecting your browser’s console, can you see any errors? If the messages aren’t sending, it’s likely you have a JavaScript error. Key elements you’d want to check is your placement of punctuation in your sent hook

      1. AgnesLP says:

        Hi, afrter another copy paste it now does work. Thanks 🙂
        on_sent_ok: “ga(‘send’, ‘event’, ‘category’, ‘action’);”

      2. AgnesLP says:

        PS: I understand why it sometimes did work and sometimes not, the cosole indeeds telles me “ga not defined”
        That is because, on a test environment, I had desactivated GA tracking. So the script could not run well.

  27. SmallBizGeek says:

    on_sent_ok: is going to be discontinued at the end of 2017 sop I suggest you look for an alternative solution.

  28. Lee Ginbey says:

    As mentioned by SmallBizGeek, on_sent_ok is now discontinued – I have a simple work around that uses GTM here:

  29. aarondicks says:

    If you’ve come this far, you may find this new, blog to be of much more use since DOM events have been supported in WPCF7 (plus we use Google Tag Manager, too)


  30. Malav Adit says:

    I want to track and bifurcate the form submission according to the Traffic type like Adword, Organic and Direct.

    1. aarondicks says:

      Hi Malav

      You will need to look at other JS plugins to get this working.

      Consider reading my more recent blog here:

      And then combining that with a first party utm cookie tracker setup such as this:

      Hope this helps!