This is the third article in few days about the Thank you page. I’m feeling a lot thankful.

Today you will learn how to add the Google Analytics tracking code used to track conversions. Since the first page you see after completing an order is the Thank you page, you need to add it there.

Are you ready to get your hands dirty?

Well, then open, as always, the file functions.php located in wp-content/themes/your-theme-name/ and add this code at the end of it:

Add your Google Analytics tracking code where you read <!– PUT YOUR GA CODE HERE –> and test the conversions in Google Analytics.

Happy tracking!

32 replies
  1. Chris O
    Chris O says:

    Is the Google Analytics tracking code the same as the tracking ID or account number UA-…?

    Do I paste the entire script with tracking code in the area for or just the UA-number?

    Also, this works with current version of WooCommerce correct?

    Thanks

    Reply
  2. Ben Niehues IV
    Ben Niehues IV says:

    Hi Nicola,

    I’ve tried using this code for my Facebook conversion pixel. It seems to be very close to working but the code at the beginning that declares the order variables is being printed at the top of the Thank You page. Any help would be greatly appreciated, thanks Nicola!

    /**
    * @return void
    */
    function fb_conversion_tracking( $order_id ) {
    if ( is_order_received_page() ) {
    ?>
    $order = new WC_Order( $order_id );
    $order_total = $order->get_order_total();

    (function() {
    var _fbq = window._fbq || (window._fbq = []);
    if (!_fbq.loaded) {
    var fbds = document.createElement('script');
    fbds.async = true;
    fbds.src = '//connect.facebook.net/en_US/fbds.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fbds, s);
    _fbq.loaded = true;
    }
    })();
    window._fbq = window._fbq || [];
    window._fbq.push(['track', 'XXXXXXXXXX', {'value':'','currency':'USD'}]);

    <img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=XXXXXXXXXX&cd[value]=&cd[currency]=USD&noscript=1" />
    <?php
    }
    }
    add_action( 'wp_head', 'fb_conversion_tracking' );

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Louisa,
      i don’t really know how your code has been added to your site. There are hundreds of different methods.

      Try to check if you have any plugin installed that could add that code. If not, check if you have some code in your functions.php in your theme folder.

      Reply
  3. Mohammad
    Mohammad says:

    Hello,

    We need a solution to have conversion tracking of orders, line items, price & name.

    If you’re able to help please contact me & we can discuss cost

    Reply
  4. Evan
    Evan says:

    Nicola,

    Thanks for the article. It seemed like it would be an easy add but I’ve been encountering some problems. The last few lines of my functions.php file look like this:

    //echo ‘4=

    ‘;print_r($_SESSION[‘custom_shoe’]);echo ‘

    ‘;
    //unset($_SESSION[‘custom_shoe’]);
    /*———————————————————————————–*/
    /* Don’t add any code below here or the sky will fall down */
    /*———————————————————————————–*/
    ?>

    I didn’t read that warning at first so I pasted your code underneath it and the site disappeared so I had to login to my FTP to undo it. Then I pasted the code after “Custom_shoe’});” and I had the same result. I tried a few different locations and every time the site shut down. I read elsewhere that sometimes it’s better to edit that file on the FTP site and not on WordPress. Should I try that?

    Thanks,
    Evan

    Reply
  5. Chris
    Chris says:

    Looks great 🙂

    Two questions:
    1. Can I also use this code for the Google Adwords Conversion Tag?
    2. If yes… can I also include 2 Google Adwords Conversion tags for two different products?

    Reply
  6. Lindy
    Lindy says:

    Hi
    I’d like to use this for adwords tracking as well but the adwords tracking needs to go in the body tag not the head tag.
    How would I modify to acheive this, and to keep the above code for my facebook conversion tracking?

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Lindy,
      use wp_footer in the last line of the snippet instead of wp_head.

      I’m not sure where exactly in the body it should be, wp_footer will put it at the end of the page.

      If you want it at the top, you will probably need to edit your theme.

      Reply
  7. Julian
    Julian says:

    Is this code still valid? Other tutorials I’ve seen have used return to run javascript code within an add_action event. An example for the checkout completing would be:

    function checkout_complete( $order_id ) {
        $order = new WC_Order( $order_id );
        $total = $order->get_order_total();
        return '
      adroll_conversion_value = '.$total.';
      adroll_currency = "USD";
    ';
    }
    add_action( 'woocommerce_thankyou', 'checkout_complete' );
    
    Reply
    • Nicola Mustone
      Nicola Mustone says:

      It is still valid Julian,
      The code used depends on the action used. Your example uses a different one.

      Also just returning the code won’t work since it won’t be printed in the page.

      Reply
  8. wajid
    wajid says:

    Hello,

    I am stuck adding Google Adwords ‘transaction-specific conversion values’

    https://support.google.com/adwords/answer/6095947?hl=en

    I have done steps 1 + 2 but I am stuck on step 3 (Part 3: Add or modify the tag in your website’s code)

    The code Genrated looks pretty much like this:

    /* <![CDATA[ /
    var google_conversion_id = 1234567890;
    var google_conversion_language = "en_US";
    var google_conversion_format = "1";
    var google_conversion_color = "666666";
    var google_conversion_label = "xxx-XXx1xXXX123X1xX";
    var google_conversion_value = 10;
    if () {
    var google_conversion_value =
    var google_conversion_currency =
    }
    var google_conversion_currency = "USD";
    /
    ]]> */

    <img height=1 width=1 border=0
    src="//www.googleadservices.com/pagead/
    conversion/1234567890/?value=
    &conversion_currency=
    &label=Purchase&script=0">

    I am using the Google Woocommerce plugin – what do I need to change to track the value of each order?

    I belive the PHP expression in woocommerce for order value is $total = $order->get_order_total();

    Anychane you can help me modifying and insterting this code into my website..
    I am happy to pay you a small fee for your support

    Thanks!

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi,
      The code you need is $order->get_total().

      You only need to change the if part with this:

      if (<?php echo $order->get_total(); ?>) {
        var google_conversion_value = <?php echo $order->get_total(); ?>
        var google_conversion_currency = "<?php echo get_woocommerce_currency(); ?>"
      }
      

      Then below, change the img src to this:

      //www.googleadservices.com/pagead/conversion/1234567890/?value=<?php echo $order->get_total(); ?>&conversion_currency=<?php echo get_woocommerce_currency(); ?>&label=Purchase&script=0
      
      Reply
  9. Kevin
    Kevin says:

    We’re using google analytics with woo commerce and I notice some page views are missing for our “order complete” pages, which is how we’re tracking conversions of successful purchases. Each “order complete” page is displayed only once and has a unique order# that I’ve checked against our web orders received, but Google Analytics isn’t registering 10-50% of the “order complete” page views. I don’t think it’s a sampling issue because we’re a low-traffic website (500-1000 sessions/wk). It also makes me wonder what other page views are missing – there must be a way to check…

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

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