Add Google Conversion OnClick Function To Gravity Forms Submit Button

Add Google Conversion OnClick Function To Gravity Forms Submit Button

This post was originally created on our sister site WP Cover.
View Original Article
April 18, 2018

Today we have another quick code snippet that you can copy/paste and quickly implement into your WordPress website that features Gravity Forms.

Everybody wants to know conversion data, it’s how marketing decisions are made.  Fortunately, it’s pretty easy to do.  Here’s a quick example on how you could add an onclick event to a Gravity Form submit button.  Before adding the onclick event, Google will provide you with additional site-specific tracking code that needs to be placed within your header file of the active theme.  Take care of those instructions first.  Once it comes time to add the onclick event, open up the functions.php file from your active theme and add the following:

 

add_filter( 'gform_submit_button_1', 'add_ga_onclick', 10, 2 );
function add_ga_onclick( $button, $form ) {
    $dom = new DOMDocument();
    $dom->loadHTML( $button );
    $input = $dom->getElementsByTagName( 'input' )->item(0);
    $onclick = $input->getAttribute( 'onclick' );
    $permalink = get_permalink();
    $onclick .= " return gtag_report_conversion(" . $permalink . ");";
    $input->setAttribute( 'onclick', $onclick );
    return $dom->saveHtml( $input );
}

The only modification you will want to make to the above code is on the first line, ‘gform_submit_button_1’. You will want to change the ‘_1’ with the number ID of your Gravity Form.

Other than that, implement and enjoy conversion data!

The post Add Google Conversion OnClick Function To Gravity Forms Submit Button appeared first on WP Cover.

Share Your Thoughts

FREE: 10 Step Guide To Choosing A Web Design Company

FREE: 10 Step Guide To Choosing A Web Design Company

Download your free e-book. Learn the questions you should be asking prior to partnering with a web design company. Enter your email address and we'll send it to you right away!

We've sent the e-book to your email address!