How to insert Google Ads tracking code in WordPress/WooCommerce Thank You page

WebHow to insert Google Ads tracking code in Wordpress/WooCommerce Thank You page

“How do we track sales conversion of our Marketing Campaigns?” This question will always occupy your mind if you are running a WordPress or WooCommerce website. No matter we’re using Google Adwords or Facebook Ads, or any other platform for advertising to boost our sales, it is crucial to track the sales conversions to analyze the end result of the marketing campaign. Tracking the sales conversion is necessary to know what campaigns are working and what aren’t. You can examine whether you’re directing your marketing efforts to the wrong marketing platform that is not getting you any sales conversion. It is also very helpful in taking further decision or to optimize your campaign.

How to insert Google Ads tracking code in Wordpress/WooCommerce Thank You page
Image Source: Elegant Themes

Why Enable Conversion Tracking?

Conversion tracking means to follow up and measure the results of your various marketing campaigns. You should be tracking the actions you want the users to take on your website.

  • For a website offering services, a user submitting a contact form can be a conversion.
  • A successful conversion for the news/blog site will be a sign-up for the newsletter.
  • For an eCommerce website, a successful purchase or an order place will be counted as a conversion.

Once conversion tracking is enabled on WooCommerce/ WordPress, we can use those statistics to optimize the online store and our Google or Facebook ad campaign:

1. Key Performance Indicators (KPIs)

A few KPIs that can be tracked are :

  • Conversion rate
  • Abandonment of Shopping cart
  • Average Order Value
  • Cost per Acquisition

2. Shopping Behavior

It will also give you all the data you need to know about the shopping behavior of customers. For instance, how many users add products to the shopping carts and how many abandon the cart, etc.

3. Checkout Behavior

You can keep an eye on billing and shipment details, payments made and the reviews posted.

How to insert Google Ads tracking code in Wordpress/WooCommerce Thank You page
Image Source: OptinMonster

4. Sales Performance

Setting up WooCommerce conversion tracking also allows you to track all the sales made. It allows you to optimize your advertisement campaign accordingly.

How to Configure Conversion Tracking in WordPress or WooCommerce site?

“How do I configure the sales conversion tracking for my WordPress site?” The answer is simple- You need to insert the tracking code on the thank you or order received page in WooCommerce. However, there lies a challenge for a layman user, there is no real “order-received” page or “thank you” page that you can simply edit like another WordPress page. This is a unique dynamic page that is a part of the WooCommerce My-account endpoint.

So you need to find a way to insert your conversion tracking code snippet into that page. Google Adwords Documentation explains the lengthy conversion tracking setup process. We will achieve this with the help of a simple WooCommerce action hook that can be added directly to the theme’s function.php file or can be dynamically inserted with the help of a code snippet plugin. Here I am going to show you both the ways of achieving this.

Option#1. Insert the code directly to the function.php file.

Go to your WordPress theme’s ‘function.php’ file (Located in ‘wp-content/themes/active theme name/’) and copy the follwoing code, in the end. Though adding the code to the parent theme’s function.php file is not recommended because this will be wiped out when the theme is updated. So it is recommended to add the following code to your child theme’s function.php file or use ‘Code Snippets Plugin‘ to add this code.

Suppose your google adword campaign tracking code is follwoing.

<!– Event snippet for Conversion conversion page –>
<script>
  gtag(‘event’, ‘conversion’, {
      ‘send_to’: ‘AW-XXXXXXXXX/HCfttILBsOICEPPEy5wB’,
      ‘transaction_id’: ”
  });
</script>

Here you also need to pass your transaction or order id with the conversion event. Use the following code snippet which passes all the values when the conversion event is fired.

add_action( 'woocommerce_thankyou', 'hawkdive_google_conversion_tracking' ); function hawkdive_google_conversion_tracking() { echo "<script> gtag('event', 'conversion', {  'send_to': 'AW-XXXXXXXXX/fttILBsOICEPPEy5wB', 'transaction_id': '".$order_id."'   });</script>"; }

Make sure you replace the highlighted tracking code with yours.

Option#2. Insert the code using the “code snippet plugin”.

While a lot of plugins are available to perform this task, you will find that many of them are unwieldy and can slow down your website to a certain extent that affects the performance or they may be overqualified to perform this simple task. So I always use the code snippet; the best minimal coded lightweight plugin.

    1. Login to WordPress dashboard. Navigate to Plugins on the left control menu => select ‘Add Plugins‘.
    2. Search for keywords ‘Code Snippets’ and install the plugins.
      Installing Code Snippets
      Installing Code Snippets
    3. Activate the plugins and Navigate to ‘Snippets‘ option on the left in WordPress dashboard.
    4. Click ‘Add New‘ => type anything in the title to name your code then copy and paste the above filter hook code.
      Add Google Ad Tracking Code in WooCommerce Thank You Order-Received page
      Add Google Ad Tracking Code in WooCommerce Thank You Order-Received page
    5. Click the “Save Changes and Activate” button at the bottom left and you are done.

Getting additional order details

In case you also needed to obtain the order value from the same thank you page and have it sent out with conversion reporting. You can get the order value passed along the conversion event. You can even fetch the customer’s phone and get that along with conversion data. Here is how you can achieve that.

add_action( 'woocommerce_thankyou', 'hawkdive_google_conversion_tracking' ); 

function hawkdive_google_conversion_tracking() {
$order = wc_get_order( $order_id ); $order_data = $order->get_data(); $order_billing_phone = $order_data['billing']['phone']; $order_total = $order_data['total'];
echo "<script> gtag('event', 'conversion', {  'send_to': 'AW-XXXXXXXXX/fttILBsOICEPPEy5wB', 'transaction_id': '".$order_id."',
'amount_paid': '".$order_total."',
'customer_phone': '".$order_billing_phone."'  });</script>"; }

Let me know in case you have any errors or need any other data passed along the conversion, by filling out the comment form below. I will try to include that in my next post.

You may also like to read: How to change the Singapore Dollar currency symbol from $ to SG$ in WooCommerce? How to add YouTube Video in a custom tab on WooCommerce Product Page?

Conclusion

After this, you will see that setting up the WordPress conversion tracking process is not difficult and there are a lot of different ways to implement it. You can set up the tracking process without any bloated plugin as well.

Taiba Hasan
Taiba Hasan
A tech enthusiast with a Post Graduate degree in Computer Applications, trying to make technology less complicated for novice users.
Watch & Subscribe Our YouTube Channel
YouTube Subscribe Button

Latest From Hawkdive

You May like these Related Articles

6 COMMENTS

  1. Hello,
    I have to enter the google tracking code on my Thank you page.
    Does your code work?
    I would like to understand in your code where the tracking code starts, I would like to replace it with mine.
    Also I believe there is an error in the code here “<script & gt;"
    is this correct or should it be "”?
    Thanks

  2. omg, as a code idoit, I finally found this article on how to set up google ads conversion order value = real order value, not $1! Thanks, Nasir, I will have a try and update soon!

  3. This did not work in my case. Throwing fatal error trying to add this code in functions php.
    Instead i found a solution fixing this.

    function hawkdive_google_conversion_tracking() -> becomes ->
    function hawkdive_google_conversion_tracking( $order_id )

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.