AdWords Conversion Tracking

, , , ,

So I ran into an issue where I needed to be able to track conversions in Google AdWords for forms that were using Gravity Forms.  While this example talks about how to do this you could really use this with any form submission to thank you page or confirmation message.  As long as the confirmation message has the ability to output from a shortcode.  All of these go into your theme's functions.php file which should be a child theme if it isn't a custom developed theme.

This first bit of code is the global site tag assignment.

add_filter('wp_head','adwords_conversion_head');
function adwords_conversion_head() {
  ?>
  <!-- Global site tag (gtag.js) - Google AdWords: xxxxxxxxx -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-xxxxxxxxx"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'AW-xxxxxxxxx');
</script>

<?php

}

This second piece of code is what generates the shortcode to which you can put either in the confirmation message of the gravity form or within the content of the thank you page.

add_shortcode(‘adwordsconversion’, ‘adwords_conversion_code’);
/* [adwordsconversion] – shortcode example */
function adwords_conversion_code() {
return “
gtag(‘event’, ‘conversion’, {‘send_to’: ‘XX-xxxxxxxxxx/xxxxxxxxxxxxxxx-xxxxxxx’});
” . “\n”;
}

If you didn't want to go through and edit all the thank you pages you may have you could build a function like this to output the code on specified page ID's.

add_filter("wp_footer", "adwords_other_conversions");
function adwords_other_conversions() {

  $thank_you_pages = array( 
      '5659', //contact us thank you page id
      '5657', //rfq thank you page id
    );

  if (is_page( $thank_you_pages )) {	
    echo adwords_conversion_code();
  }
}

In reality this could be used for Google Analytics event firing or any other time you need to output code to a thank you page or confirmation message.  Maybe I will also think of a way to utilize the gform_confirmation filter but for now this way will work as you would need to write the code in such a way as to check what confirmation was being used.  This could be set to redirect or be just the message that will be getting displayed and then the redirect could be to a local page or could be a page on another site.

Skills

Posted on

January 26, 2018

Submit a Comment

Your email address will not be published. Required fields are marked *