Track conversions with the Google Analytics Tracking Code in WooCommerce

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!


More Posts That You Might Like…


32 responses to “Track conversions with the Google Analytics Tracking Code in WooCommerce”

  1. I assume that this would be the same for the code to track Facebook conversion pixel? Thanks!

    1. Hi Mike, yes, of course!

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

    1. Hi Chris,
      you need to add the entire script, not only the UA-number.

      Write all the code, that one which starts with <script>.
      Yes it works with the latest version of WooCommerce.

  3. G’day, how would this work with including the order total?

    1. Hi Mike, sorry i missed your comment.

      To make that work with order totals you would need more PHP code and the ecommerce functionalities enabled in your GA.

      In this case i suggest to use this plugin instead: https://wordpress.org/plugins/woocommerce-google-analytics-integration/

  4. Ben Niehues IV Avatar
    Ben Niehues IV

    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' );

    1. Hi Ben,
      just move the variables declaration before the ?> and after if ( is_order_received_page() ) {.

  5. help please! I can see the analytics code I want to remove in the page source code, but cant find it in wordpress – can you help me ?

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

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

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

    1. Hi,
      remove this code

      //echo ‘4=
      
      ‘;print_r($_SESSION[‘custom_shoe’]);echo ‘
      
      ‘;
      //unset($_SESSION[‘custom_shoe’]);
      

      And add the snippet there. I suggest to do it via FTP so if something breaks you can remove it quickly and get your website online again.

      1. Nicola,

        Thanks for the reply. So the code you’re telling me to remove has no purpose? Removing it won’t do anything?

        1. Exactly. That code does not make any sense and should break your website.

  8. Hi there,

    thanks for the article. How would you do this if you are using Google tag manager?

    Thank you

    1. Hi Scott,
      i’m sorry but i never used Google Tag Manager before nor i have a clue about what it is/does.

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

    1. Hi,

      1) Yes
      2) I’m not sure honestly. I never used AdWords so I don’t really know how it works with tags, etc.

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

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

  11. Hi, should the replaced line now look something like this?…

    1. Hi Derek,
      with this what?

      1. Sorry, my bad, I posted a code snippet not thinking it wouldn’t show correctly!

        Hopefully it will show this time…

      2. Sorry, I am obviously completely incompetent and can’t work out how to post code! (How do you do this for future reference?)

        I will simply ask the question instead… is it simply the written text PUT YOUR GA CODE HERE you replace or do you replace any of the characters too?

        1. You replace the whole line, so <!-- PUT YOUR GA CODE HERE -->

  12. 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' );
    
    1. 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.

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

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

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: