Search code examples
phpwordpresstrackinggoogle-ads-apicontact-form-7

Google AdWords tracking with CF7


I would like to use Google AdWords tracking based on event (when people submit). But I would still like to redirect to a thank you page. I have two codes that do the trick, but they do not work together. Is there a way to have the form redirect to another page but still track on submit? When I add these codes at the same time they say that it is already declared.

<?php

add_action( 'wp_footer', 'mycustom_wp_footer' );
 
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
       location = 'https://revimatch.dk/tak';
}, false );
</script>
<?php
}

add_action( 'wp_footer', 'mycustom_wp_footer' );

function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
    typeof goog_report_conversion === 'function' && goog_report_conversion();
}, false );
</script>
<?php
}


Solution

  • You dont need two functions for this.
    You can hook this in one script to your wp_head

    add_action('wp_head', 'cf7TrackingCode' );
    function cf7TrackingCode(){
    echo "<script>
          document.addEventListener( 'wpcf7mailsent', function( event ) {
            typeof goog_report_conversion === 'function' && goog_report_conversion();
            location = 'https://revimatch.dk/tak';
          }, false );
        </script>";
    }
    

    And very important:
    You cant have two functions with the same name in php