Gravity Forms and setting up Google Analytics Goals
December 16, 2012When we deployed our new site at Internetavdelningen last week, one of our goals was to get better at measuring conversions. Naturally, one of the main goals of the site is to convince potential clients to get in contact with us. Therefore we decided to setup a goal in Google Analytics to track how many users submit our contact form, which is powered by the (absolutely awesome) Gravity Forms WordPress-plugin.
The easy way – a separate confirmation page
If that suits your page design and structure, it is easiest to just configure your Gravity form with a confirmation page. See the form settings screenshot below. You just have to create a page, which thanks for the submission or something like that and choose this page in the form settings.
Now it’s only left to add the page as goal to your Google Analytics profile. Click on the “Admin” item in the top menu on the right site and than on your profile in the Profiles tab, in your profile, choose the “Goals” tab and add a goal in one of the sets. Name and activate the goal, choose “URL Destination” and copy the permalink of your confirmation site into the “Goal URL” field. Now, after a few hours (or max one day) your conversion statistics should appear under “Conversions” in the “Standard Reporting” section.
How to do it without a confirmation page
The solution was the somewhat unintuitive goal-type called “Event”. You select it instead of “URL Destination” in the goal settings.
Fortunately there is a great generator to do almost all the work for you. If you follow all the steps, including the described goal settings, the only task left is to add the generated tracking code to the submit button. That’s quite easily done with the Gform-submit-button-filter, provided by Gravity Forms.
Here is the code snippet that adds the tracking code with an onclick-handler to the submit button of the form with id=1. Add it to your themes functions.php.
add_filter("gform_submit_button_1", "add_conversion_tracking_code", 10, 2);
function add_conversion_tracking_code($button, $form) {
$dom = new DOMDocument();
$dom->loadHTML($button);
$input = $dom->getElementsByTagName('input')->item(0);
$input->setAttribute("onClick","_gaq.push(['_trackEvent', 'Contact', 'Information Request', 'Contactform',, false]);");
return $dom->saveHtml();
}
Happy conversion tracking!
Back Tweet me Share me Plus me

Hi and thanks for the tip!
The only problem I’ve got is that I don’t receive any email since I add your code to my functions.php.
Am I doing something wrong?
add_filter("gform_submit_button_2", "add_conversion_tracking_code", 10, 2);
function add_conversion_tracking_code($button, $form) {
$dom = new DOMDocument();
$dom->loadHTML($button);
$input = $dom->getElementsByTagName('input')->item(0);
$input->setAttribute("onClick","_gaq.push(['_trackEvent', 'Contact', 'Enquiry', 'Booking Form',, false]);");
return $dom->saveHtml();
}
The form ID is 2, all the rest is copied from your code.
Since I add the code to the php file the form stop sending me emails but not errors are displayed.
Could yo please help me with it?
Thanks.
Hi Tony,
the form is working if you remove that code? If you submit the form, does it create an entry or is the whole submission process not working?
Thanks for your feedback
Daniel
Hi Daniel,
just to say thanks for this article – it really helped pull some stuff together for me and I now have it all working.
Didn’t really want to use another confirmation page – this is much neater.
Thanks again.
Hi Paul,
thank you so much!
Daniel
Thanks for this great post. I have another possibility: directing the form to an appended URL (e.g. mysite.com/?form=true) and set this URL as the goal. Can you see anything wrong with this? Thanks.
Hi Daniel,
First, thanks for putting this up. It’s been very helpful.
I have a question about the “onClick” event. If a person clicks the Submit button, but the form has errors, would that count towards your analytics numbers as a form submission?
Yes it would. As we are triggering the google event from the client-side, we can never be really sure that the form actually has been send. But you could take further measures to be almost sure, like validating the form with jQuery before you send it and trigger the google event.
I much better and easier solution is just to paste the JavaScript into the “text confirmation” response. Make sure you disable the auto-formatting. This requires no PHP and only executes when VALID submissions are made. Error responses won’t be logged as actions. Here is what I’ve been using
_gaq.push(['_trackEvent', 'Forms', 'Submit', '{form_title}']);oh 2 things, 1 thanks for the tut, it put me on the right track. and 2nd, it removed my “script” tags, don’t forget to add wrap the above in javascript tags
Great article! thanks a lot for sharing this useful info with us.
Thank you so much for this! Wish there was a way to enter the onclick event in the form settings as opposed to adding it via function, but glad to have a solution.