'Form submit tracking

I'm trying to get Google Ads to track a conversion every time the user submits a form on a wordpress site. The problem is that even though everything is placed correctly I can't track the click even. The global Google tag is placed on the website, as well as the page specific tag and the button tag [Originally: return gtag_report_conversion('https://theurl.com/sub/')] which is where I think the issue lies. I'm utilizing the following code to modify the form's submit button in order to add the onclick event:

add_filter( 'gform_submit_button_3', 'form_submit_button', 10, 2 );
function form_submit_button( $button, $form ) {
    return '<input type="submit" id="gform_submit_button_3" class="gform_button button" value="Enviar" onclick="return gtag_report_conversion(https://theurl.com/sub/); if(window[&quot;gf_submitting_3&quot;]){return false;}  window[&quot;gf_submitting_3&quot;]=true; " onkeypress="if( event.keyCode == 13 ){ if(window[&quot;gf_submitting_3&quot;]){return false;} window[&quot;gf_submitting_3&quot;]=true;  jQuery(&quot;#gform_3&quot;).trigger(&quot;submit&quot;,[true]); }">';
}

As you can see I had to modify the code slightly to remove the apostrophes so it didn't screw up with the function and I think that may have something to do with the issue I'm having.

I also tried to escape part of the html code like this, but that completely disables the function of the button (the page reloads and nothing gets send):

add_filter( 'gform_submit_button_3', 'form_submit_button', 10, 2 );
function form_submit_button( $button, $form ) {
    $theurl = "return gtag_report_conversion('https://theurl.com/join/')";
    return '<input type="submit" id="gform_submit_button_3" class="gform_button button" value="Enviar" onclick="if(window[&quot;gf_submitting_3&quot;]){return false;}  window[&quot;gf_submitting_3&quot;]=true; '. esc_html($theurl) .';" onkeypress="if( event.keyCode == 13 ){ if(window[&quot;gf_submitting_3&quot;]){return false;} window[&quot;gf_submitting_3&quot;]=true;  jQuery(&quot;#gform_3&quot;).trigger(&quot;submit&quot;,[true]); }">';
}

Any clue what I could be doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source