I get asked this question quite a bit, and that is probably because this is something that every eCommerce store needs. The question usually goes like this:
How can I add my tracking code to the WooCommerce thank-you / order-recieved page?
There are two ways to tackle this. The easiest being to use a plugin. While there are quite a few plugins out there to do this, I recommended this plugin as it is created by the developers of WooCommerce: WooCommerce Google Analytics Integration
However, there is always the DIY crowd (me included) that like to do this on their own, or perhaps you are using a custom analytics system that requires you to give it a little special attention. In either case, you need to be able to put some tracking code on the page users go to after checking out so you can track the orders. And I’m sure you’ve noticed by now that WooCommerce does not have a specific page for this. Rather it is just the checkout page with a custom endpoint. So to add something specifically to this page, we will need to hook onto a hook found on just the thank-you page.
For this, we will be using the woocommerce_thankyou
hook found in the checkout/thankyou.php template.
tl;dr – the completed snippet will look like this:
About this snippet
First, we are hooking into the woocommerce_thankyou
hook found in the checkout/thankyou.php template. That means our code will only be fired off when somebody reaches the thank you page.
Then, if the customer is on the order received page, we are adding the tracking code to the bottom of the page right where that hook is located in the template. Now you can add in your custom tracking code between the two commented lines, and you’ll be all set! I included some variables that you might need to use in your tracking code, such as the order ID and order Total. If you need some order information that is not there, take a peek at this file in WooCommerce and see if there is a function available that will give you the data you need from the order. Remember, that if you want to use one of those variables, you need to enter into PHP mode again like this: <?php echo $total ?>
This snippet should go in your child theme’s functions.php file. If you are using a premium child theme (meaning your child theme will get updates from the theme authors), then you should use the Theme Customizations plugin.
Does that sound right? The customer must view the final Order Received page for it to be tracked in Analytics?
Yes, the page will need to be viewed / loaded. But customers are automatically directed there after checkout is complete.
Any chance you could shed some light on how to add a little more text to that page, instead of or in addition to the boilerplate “Your order has been received.” Would love to add a few more words and maybe a coupon code for a future visit, etc. Thanks!
You can either override the “checkout/thankyou.php” template and change / add text. Or you can use the woocommerce_thankyou_order_received_text filter: https://github.com/woocommerce/woocommerce/blob/2.4.7/templates/checkout/thankyou.php#L36
[…] Must Read Article: Add analytics tracking to the WooCommerce thank-you page […]
That’s super nice and helpful but we came across a problem with Paypal tracking.
Sometimes a purchase is tracked and sometimes it’s not. We assume that after Paypal payment you can go back to the shop but you don’t HAVE to. So some people just leave after payment without even seeing the Thank you-page.
There is this hook we would like to try out: “woocommerce_payment_complete”. Not sure if it works though
Hi! Is this applicable using other tracking pixels too? I use markethero , and I want to put pixels on my thank you page.
Yes, the same method can be applied to any sort of tracking scripts. Just need to make sure you are passing in the correct variables.
Thank you so much .the first article that brought me so close to solving this.I say close becos I’m not so techy savvy I hope I get it right. Thank you
[…] Must Read Article: Add analytics tracking to the WooCommerce thank-you page […]