How to Track Successful Form Submissions Without a Plugin

Introduction

Would you like to track your successful form submissions without the need for another plugin? Using one of the many WPForms hooks and a little PHP code snippet you can easily track the form submissions inside Google. In this tutorial, we’ll give you the snippet needed and walk you through setting up these tracking events.

Adding the snippet

For the purpose of this documentation, we will presume you already have your Google Analytics account set up and running. If you need any assistance with setting up your Google Analytics, please review this tutorial.

Once you’ve set up your Google Analytics and your form, just add the snippet to your site. If you need any assistance in how to add a snippet to your site, please review this documentation.

/**
 * Use Google Analytics to track submissions by form without a plugin
 *
 * @link https://wpforms.com/developers/how-to-track-successful-form-submissions-without-a-plugin/
 *
*/

function ga_track_event_form_submission( $fields, $entry, $form_data, $entry_id ) {

  $endpoint = 'https://ssl.google-analytics.com/collect?';

  $payload_data = array(
    'v' => 1,
    'tid' => 'UA-XXXXXXXXX-1',
    'cid' => 123,
    't' => 'event',
    'ec' => 'Forms',
    'ea' => 'Successful Submission',
    'el' => 'Contact Us'
    );

    $parameters = http_build_query( $payload_data );

    wp_remote_post( $endpoint . $parameters );

}

add_action( 'wpforms_process_complete_25', 'ga_track_event_form_submission', 10, 4 );

In the snippet you’ll need to provide the UA number from your Google Analytics account. To find this number just go to the Admin section in your Google Analytics Dashboard and click on the Users tab.Copy your UA number from Google

For a more detailed explaination on the ‘ec’, ‘ea’, and ‘el’ can be found from Google Analytics Event Measurement.

The wpforms_process_complete_25 action that we’re using is because we only want to capture the analytics for the form ID 25. You’ll need to update this to match your own form ID. For help locating your form ID, please review this tutorial.

Once that code snippet is added, you’ll start seeing your data being recorded in your dashboard.

You can now track successful form submissions without the need of another plugin

And that’s it! You’re now able to track successful form submissions without the need for another plugin. Would you like to be able to limit the years that displays in your Date form field? Take a look at our article on How to Limit the Years Inside Your Date Picker.

Action Reference: wpforms_process_complete

FAQ

Q: Can I use this same snippet for multiple forms?

A: You can reuse this function for multiple forms but you’ll need to change the function name so that it’s unique. So instead of using ga_track_event_form_submission you’d need to change this function name each time you want to use this function on another form.

You’ll also need to update the ‘cid’ number as well to keep this unique event