Integrating Google AdWords Conversion Tracking into WooCommerce

Google Conversion Tracking is a free tool integrated into Google Adwords which helps you measure which advertising clicks convert into real sales. This is very useful to determine which of your ads are meaningful and which are not. As a result you are able to optimize online marketing efforts precisely.

Now, how can we get implement this into our WooCommerce store to track our conversion in an intelligent way?

First we need to get the proper tracking code from our Google Adwords account. This will look something like this:


Since we want to track a conversion when a sale is really finished, we need to target the WooCommerce «Thank You»-page which is shown after a customer successfully passed the checkout including the desired payment option.

There is a template file called thankyou.php that we could modify, but best practice is to hook into it with a custom function added to your theme’s functions.php:

function google_conversion_tracking() {
	if ( is_order_received_page() ) :
	
	// we will do stuff in here
	
	endif;
}
add_action( 'wp_footer', 'google_conversion_tracking' );

This assures that our code will only run on that page, since we do not need it elsewhere. Probably you noticed that the Google tracking code comes with a variety of variables you can provide. Among these we can send the currency, language and a monetary value (the final order amount).

To get this information we have to access WooCommerce’s data for the current order which is stored in the global $wp variable. In our case we want the (rounded) «subtotal» of the order which does not include taxes and shipping costs. We also check if an order exists and if it has the status «failed»:

function google_conversion_tracking() {
	
	if ( is_order_received_page() ) :
		global $wp;
		$order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
		$order = new WC_Order( $order_id );

		if ( $order && ! $order->has_status( 'failed' ) ) :
			$order_total = round($order->get_subtotal(),2);
			?>

			/* Google tracking code goes here */
			
			

		
		<?php endif; endif; } add_action( 'wp_footer', 'google_conversion_tracking' );

If you copy and paste this function be sure to replace all the «XXX» values (also the ones in line 28) with the ones found in your generated tracking code.

Now you should be all set. Happy conversions! 🙂

(Featured image from Alexas_Fotos, Pixabay)

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert