WooCommerce: Add Conversion Tracking Code to Thank You Page

How do you add a conversion 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

WooCommerce Snippet: Add Conversion Tracking Code to Thank You Page


/**
 * @snippet       Add Conversion Tracking Code to Thank You Page
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19964
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page' );

function bbloomer_conversion_tracking_thank_you_page() {
?>
   <!-- Google Code for Conversion Page -->
   <script type="text/javascript">
   /* <![CDATA[ */
   var google_conversion_id = 000000000000;
   var google_conversion_language = "en";
   var google_conversion_format = "3";
   var google_conversion_color = "ffffff";
   var google_conversion_label = "kjsdhfkjhkfsdfsdf";
   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/000000000000/?label=kjdhfsdjfkjkjshdkkkjsdkk&amp;guid=ON&amp;script=0"/>
   </div>
   </noscript>
<?php
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

57 thoughts on “WooCommerce: Add Conversion Tracking Code to Thank You Page

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

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

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

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

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

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

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

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

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

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

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

  12. it’s work, thank you for code

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

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

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

    1. Awesome 🙂 Auf Wiedersehen!

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

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

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

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

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

  21. 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(); ?>
      
      
  22. Thanks a lot for this tip Rodolfo, it works like a charme!

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

  23. 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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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