WooCommerce: Add Tracking Code / Script @ Thank You Page

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!

WooCommerce: Add Conversion Tracking to the Thank You Page
WooCommerce: Add Conversion Tracking to the Thank You Page

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
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

70 thoughts on “WooCommerce: Add Tracking Code / Script @ Thank You Page

  1. Hi
    Does this need to updated for the upcoming GA4 ??
    Is there a way to serve total amount, tax, fright?

    Best regards

    Peter

    1. 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/

  2. 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.

    1. Yes it does, some custom code could be added to make it load only once

      1. Hi thanks for the code,
        so did you fix the code to make it only load once?

        1. 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!

  3. 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?

    1. 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!

  4. Thanks It works for me in Child Theme.

    1. Cool!

  5. Hi,it’s possible to use this gtag trackyng for single product page??
    If it’s how should the function looks??

    1. 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!

  6. 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.

    1. Yes, give it a go!

  7. 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’ ??

    1. Hi Peter, yes, you can keep using “woocommerce_thankyou”

  8. 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

    1. 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

  9. 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

    1. 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

  10. 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

    1. 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

  11. 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.

    1. the script is told to be written under head tag

      1. 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 🙂

  12. 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.

    1. 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

  13. 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

    1. 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

  14. 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…

    1. 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 🙂

  15. 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!

    1. Hey Lena, thanks for your comment! If you have no “?>” that’s even better – just paste it at the end 🙂

  16. 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.

  17. Can someone help me with this ?

    We need this code inserted on the final confirmation page:

    
    gtag(‘event’, ‘conversion’, {
    ‘send_to’: ‘AW-1036778088/mx5jCNrl5wEQ6PSv7gM’,
    ‘value’: 1.0,
    ‘currency’: ‘USD’,
    ‘transaction_id’: ”
    });
    
    
    1. Hey Lean, thanks for your comment! Is that a script? Where did you get that?

    2. @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

      1. 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 🙂

  18. it’s work, thank you for code

  19. 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

    1. 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 🙂

      1. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  20. 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?

    1. 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 🙂

  21. Hey, thank you. Works great.
    Thanks and regards from Germany

    1. Awesome 🙂 Auf Wiedersehen!

  22. Thank you very much!
    It works! and you save me a lot of time; great work, great blog =)
    Greetings from Argentina.

    1. Genial 🙂 Muchas gracias for your comment Micaela!

  23. 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?

    1. 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 🙂

  24. 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

    1. Excellent, great to hear that Graham 🙂

  25. 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

    1. 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 🙂

      1. 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:

        <!-- Google Code for Purchase Conversion Page -->
        	<script type="text/javascript">
        	/* <![CDATA[ */
        	var google_conversion_id = 1234567890;
        	var google_conversion_language = "en";
        	var google_conversion_format = "1";
        	var google_conversion_color = "666666";
        	var google_conversion_label = "xxxxXXx1xXXX123X1xX";
        	var google_conversion_value = 1.00;
        	if (<? echo $totalValue ?>) {
        		var google_conversion_value = <? echo $totalValue ?>;;
        		var google_conversion_currency = <? echo $currency ?>;
        	}
        	var google_conversion_currency = "USD";
                var google_remarketing_only = false;
        	/* ]]> */ 
        	</script>
        	<script type="text/javascript"
        	src="//www.googleadservices.com/pagead/conversion.js">
        	</script>
        	<noscript>
                <div style="display:inline;">
        	<img height="1" width="1" style="border-style:none;" alt=""
        	src="//www.googleadservices.com/pagead/
        	conversion/1234567890/?value=
        	<? echo $totalValue ?>&amp;currency_code=<? echo $currency ?>
        	&amp;label=xxxxXXx1xXXX123X1xX&amp;guid=ON&amp;script=0">
                </div>
        	</noscript>
        	</body>
        

        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.

        1. 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 🙂

          1. 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. 🙂

            1. 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

  26. Does this code work with a custom thank you page?

    1. Hey Vuster, with a custom thank you page you can just copy/paste the code inside it without the need of a snippet 🙂

      1. 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.

        1. 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/

  27. Would it be possible to add “Order Value” with a dynamic string?

    1. 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 :

      
      <?php echo $order->get_total(); ?>
      
      
  28. Thanks a lot for this tip Rodolfo, it works like a charme!

    1. Thank you Annie 🙂 Your feedback is much appreciated!

  29. Hi, I’m not sure where to add that snippet? Where is the file?

    1. 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!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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