How do you add a tracking code to the WooCommerce Thank You Page? Well, with a simple snippet (and no plugin) you can!
No matter whether you’re using Google AdWords, Facebook or other forms of marketing – the Thank You Page is such an important section of an ecommerce store and needs some TLC!
PHP Snippet: Add Tracking Code or Script to WooCommerce Thank You Page
/**
* @snippet Add Script @ WooCommerce Thank You Page
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 5
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page' );
function bbloomer_conversion_tracking_thank_you_page() {
?>
<script type="text/javascript">
Your script here...
</script>
<?php
}
Hi
Does this need to updated for the upcoming GA4 ??
Is there a way to serve total amount, tax, fright?
Best regards
Peter
The official WooCommerce GA plugin is compatible with GA4 now, so no need for custom scripts: https://www.businessbloomer.com/advanced-woocommerce-tracking-analytics-reports-exports-segmentation/
Thanks for this snippet! Does this run every time the thank you page is loaded? I’m concerned that multiple conversions will be triggered if the user reloads the page.
Yes it does, some custom code could be added to make it load only once
Hi thanks for the code,
so did you fix the code to make it only load once?
You’re welcome! Nope, sorry, I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey buddy, can you please guide me in the right direction? I have woocommerce store and want to setup conversion tracking for Google ads. Instead of predefined value, I need a code or solution which can pick order value and bring it into google ads.
And is there any other secondary way of tracking conversions from google ads?
Hey Punit, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Thanks It works for me in Child Theme.
Cool!
Hi,it’s possible to use this gtag trackyng for single product page??
If it’s how should the function looks??
Hi Alex, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I have been asked to place this code on our website and am not sure if this is the same thing as mentioned above?
Please add the below code BEFORE the GTM code on the confirmation page. It is a data layer cost and Ord Script very short.
Yes, give it a go!
Hi Rodolfo,
If I rename my thankyou page in Woo Settings to something not so normal or just translate it to my language will this snippet still work?
I am asking due to an discrepancy between what adwords shows and actually life… (or I get 95% of my orders from SEO 🙂 nice )
Best regards and thanks for your great woo-work.
Peter
To be CLEAR: I see your code: add_action( ‘woocommerce_thankyou’
Is this HOOK “hard coded” so Woo knows what I wrote in my thank you page setting?
I did write something like ‘order-recorded-perfect’ so maybe my add_actioon should be something like…:
add_action( ‘woocommerce_order-recorded-perfect’ ??
Hi Peter, yes, you can keep using “woocommerce_thankyou”
In reference to this comment:
Hello Mohammad, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
I assume you have this code at hand and I’d like a quote.
I want Woocommerce snippet for transaction-specific conversion pixels fired for:
Bing! Ads
Facebook
Pinterest
Thanks for all you do. This blog is great especially since I’m a non-coding marketer.
Cheers,
Jason
Hello Jason, thanks so much for your comment! Yes, this is definitely possible to code – if you’d like to get a quote, feel free to contact me here. Thanks in advance
Hello Rodolfo,
I’m doing this for the first time, just trying to get a google ad going for my site. I’m guessing I have to add my Google ID and Label in their perspective places (like replacing the 0000’s here var google_conversion_id = 000000000000).
Do I also need to change them at the bottom in the SRC code for the image code? (I see them there as well…)
I’m using Square for my checkout with WooCommerce, will that affect this from working at all?
Thank you
Chris
Hey Chris! As the code in the screenshot might be out of date, I recommend you replace the whole content. google will give you the complete tracking code. Square should be fine as long as it automatically redirects people back to the WooCommerce Thank You page
Hi Mr. Melogli,
I am a regular reader of your post but it’s my first comment. I love your posts as got several solutions on your blog for woocommerce and wp.
Today, I’m here for some kinda help as I am trying to create AdWords conversion tracking for woocommerce shopping website with variable/dynamic values. Because every product having the different price that should be fetched within conversion tracking.
But, I stuck in mid as not so much aware about Google tag manager. I request you to please share any step by step post to variable conversion tracking with price value for woocommerce website.
Thanks
Hello Mohammad, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
hi, I have this script n as per instruction told to put within .
gtag(‘event’, ‘conversion’, { ‘send_to’: ‘conversionid/purchasecode’,
‘transaction_id’: ‘order_id’ });
If i put this script in woocommerce_thankyou hook it gets order_id but appears in body tag and if the script is included in header.php then how to pass order_id to it. I just added html form in the action hook and submitted it using javascript in order to receive order_id as post but the form is getting submitted several times.
the script is told to be written under head tag
Hey Ashwini, thanks for your comment! You’d need to use the wp_head hook then, also check if you’re on the thank you page and finally get the order ID from the thank you page URL parameters. A little complex, but doable. Hope this helps 🙂
Hi Rudolfo,
I am trying to insert the Woo order revenue within the Clicky tracking code. I am not a coder. I am just feeling my way around a solution. This is where I am at in the journey.
You have given the order code: get_total(); ?>
But, simply inserting this into the Clicky code does not work. Can you help me?
Thank you for any help.
Hey Tom – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi, is there a way to ensure that the code will be fired just ONCE (if I hit refresh, it will not be fired)? Thanks a in advance
Hey Karolina, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Is there a way to make this only fire on /checkout/order-received/ and not on /checkout/? It works great but seems to show up on every checkout page…
Hey there, thanks so much for your comment! This only fires on the thank you page (“woocommerce_thankyou” is the page-specific hook), so maybe you have some other problem in your code or a plugin conflict. Hope this helps 🙂
Hi Rodolfo,
I’d like to add the snippet to my child theme functions.php file before “?>” , as you recommend it. Unfortunately my functions.php doesn’t have this. So I’m a bit confused where to add the snippet now.
I’m not really familiar with coding but would like to forgo installing another plugin.
Thanks for your help!
Hey Lena, thanks for your comment! If you have no “?>” that’s even better – just paste it at the end 🙂
If someone knows how to code or familiar with codes, it’s certainly the preferred way to add the codes.
But if you aren’t, you could use the WooCommerce Conversion Tracking plugin to track Facebook, Twitter, Google Adwords or even custom snippets.
Thanks Tareq 🙂
Can someone help me with this ?
We need this code inserted on the final confirmation page:
Hey Lean, thanks for your comment! Is that a script? Where did you get that?
@Rodolfo it’s Google’s new GTAG. This is the only kind of conversion tracking action AdWords provides now in the new updated AdWords interface experience.
It affects Google Analytics too.
Its a recent and very big change, you should read about it. https://support.google.com/adwords/answer/7521212
Gotcha 🙂 See, you’re missing the beginning and the end of the code (starts with < script > and ends with same). Once you have that, the whole code can be added to the thank you page in the same exact way this snippet does 🙂
it’s work, thank you for code
Great 🙂
Hi, I need to add facebook pixel here, on the thank page, but I’m really really confused what to do now.
where can I can learn to add snippet on the thank page? thx
Hi, so I’ve come a way with adding the snippet after a night of researching.
I use Plugin name Child Theme Configurator to create a child theme.
Now the facebook pixel already in the page but the problem now is that it said the pixel didn’t load.
Could you please help me with this problem?
Thx u, your help would be very much appreciated 🙂
Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Thanks for this concise tip! I’m wondering how to confirm that the code is actually on the thank you page. Should I be able to see the script when I go to “…/checkout/order-received/” and view source in my browser? Or is it only visible after an actual sale has been made, on the checkout page?
Hey Darryl, thanks for your comment! Yes, the code shows only if an Order is successful, that’s then the hook triggers. So, you will need to place a test order and then “view source” or “inspect element” to verify it’s there 🙂
Hey, thank you. Works great.
Thanks and regards from Germany
Awesome 🙂 Auf Wiedersehen!
Thank you very much!
It works! and you save me a lot of time; great work, great blog =)
Greetings from Argentina.
Genial 🙂 Muchas gracias for your comment Micaela!
Hi,
How can I add the code given by my affiliate platform. They told me to add this line in my success/order page.
I have added this code in my integration section under Tags for purchase. Is that enough or do I need to add more lines of code such as order value or order currency?
Thanks for your comment Ahmed! It depends on your tracking code, but usually this snippet would be enough – just substitute the Google code with your code 🙂
Hey Rodolfo,
Thanks for taking the time to share this snippet with the rest of us.
You saved me hours of searching and am sure did the same for many others as well.
Cheers 🙂
Graham
Excellent, great to hear that Graham 🙂
Hi Rodoflo,
How can include the total order in snippet function – google conversion code? Can u provide a exemple please?
Is possible to use this snippet for cart page and my account?
Thank you so much for help us!
U rocks!
Alex
Hey Alex, thanks so much for your comment! For your first question, you can take a look here: https://docs.woocommerce.com/document/custom-tracking-code-for-the-thanks-page/. For the second, yes, you can – but why would you want to track conversions on the Cart page? Usually this is exclusively done on the Thank you page 🙂
Hi Rodolfo,
1.
—
I try your solution but the order total not work in google tag manager… https://imgur.com/a1aJg5a
1. Missing CDATA comments.
2. Conversion value not set.
3. Code should be placed directly above the closing tag.
Can you provide please a snipped with google recommendations?
I find this article on google support:
2.
—
I used before this plugin https://wordpress.org/plugins/woocommerce-conversion-tracking/screenshots/ , that adds code on the pages cart & my account. Therefore I need to add on those pages google conversion.
Hey Alex, to add other codes to other specific pages take a look at “conditional logic”, where you can echo/execute stuff depending on a given condition: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/
Hope this helps 🙂
Hi again Rodolfo,
Thank you so much for your help, but i can’t fix conversion code for working without error in google tag assistant.
Please take a look here on google exemple for php: https://support.google.com/adwords/answer/6095947?hl=en
If you can please help with this snippet for work with total order on thank you page. 🙂
Alex, thanks for following up! The tutorial on WooCommerce.com should work, so this is strange to me – I’m afraid troubleshooting or custom coding cannot be provided here on the blog for free. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Does this code work with a custom thank you page?
Hey Vuster, with a custom thank you page you can just copy/paste the code inside it without the need of a snippet 🙂
So, just to make sure, does this conversion code mean that Google Analytics will track the purchases to be recorded in the Ecommerce section of GA? Thanks.
Great question! Not really – this conversion code is for Google AdWords. You’ll need a different code for Ecommerce Conversions that you can possibly get from GA’s documentation… this is why I prefer to keep the same thank you page and customize it via hooks instead of redirecting to a custom one, as GA Ecommerce tracking works out of the box with https://wordpress.org/plugins/woocommerce-google-analytics-integration/
Would it be possible to add “Order Value” with a dynamic string?
Alex, thanks so much for your comment! The answer to your question is: absolutely yes! You can get the order total by using the following PHP and then echo it inside the tracking code :
Thanks a lot for this tip Rodolfo, it works like a charme!
Thank you Annie 🙂 Your feedback is much appreciated!
Hi, I’m not sure where to add that snippet? Where is the file?
Hey Synsia, thanks for your comment! You should add this to your child theme’s functions.php file. If you need guidance, check my tutorial at https://businessbloomer.com/woocommerce-customization-hangout. Hope this helps!