Studio Matrix

Getting Contact Form 7 to work with google adwords conversion tracking.

Getting Contact Form 7 to work with google adwords conversion tracking.

Its not easy, and most documentation is out of date. Not to mention Google has recently updated their javascript code they give you when you create a conversion event. So all the forum posts online are using a slightly different code which will not work. Contact Form 7 has also deprecated support for the old way it used to handle this – with the advanced settings tab and:

on_submit: "goog_report_conversion(window.location.href);"

 

To get the contact form to submit a conversion event to AdWords, here are the steps you need to take.

 

1. Create your AdWords conversion tracking codes

This is pretty simple, I imagine you’ve already done this if you’re reading this page. However, it will look something like this:

<!-- Event snippet for <your conversion name> 
In your html page, add the snippet and call gtag_report_conversion when someone clicks on the chosen link or button. -->
<script>
function gtag_report_conversion(url) {
 var callback = function () {
 if (typeof(url) != 'undefined') {
 window.location = url;
 }
 };
 gtag('event', 'conversion', {
 'send_to': '<your adwords code>',
 'event_callback': callback
 });
 return false;
}
</script>

This will need to be pasted at the bottom of your contact form 7 form.

2. Edit your functions.php file

Next, you need to add this to your functions.php file at the bottom:

 

add_action( 'wp_footer', 'mycustom_wp_footer' );

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

And that’s it! your conversions should now be tracking and there is no need to do anything in the advanced section of your form. The function created in functions.php will only submit the form loaded in the DOM when the submit button is clicked. A word of warning however, if there is more than one form loaded on the page (with a different ID) you will need to modify your functions.php function.

Hope this helps.

image by Kevin from UnSplash

Stay up to date on the latest and greatest from the digital world.
Join our digital community here!

Thank you for subscribing to our community!

Enter your name

Enter your phone number

Enter your email

Type of enquiry?

Enter your message