Support » Plugin: WooCommerce Conversion Tracking » Product specific code only loads on product page itself, not checkout (with FIX)

  • Resolved aapollo

    (@aapollo)


    In this plugin, the product specific code loads on the product page, not on the checkout or thank you page. This defeats the purpose of the entire plugin, and causes huge errors in the numbers of conversions, which cost my client several thousand dollars in the first week until we determined the problem. Do not use this plugin.

    If you need to load in a product specific conversion code, try this:

    <?php
    // Load Tracking Pixel by Harlan T Wood & Adam Apollo
    // Facebook Tracking Pixel Code on Thank You Page for Specific Product
    // User adds -tp to product slug to load the conversion code
    
    function order_items_include_tracking_pixel($order, $order_id) {
      foreach ( $order->get_items() as $item ) {
        $product = $order->get_product_from_item( $item );
        if ( ! $product ) { continue; }
        $sku = $product->get_sku();
        if (preg_match('/-tp$/', $sku) === 1) {
          return true;
        }
      }
      return false;
    }
    
    function my_custom_tracking( $order_id ) {
      $order = new WC_Order( $order_id );
    
      if (order_items_include_tracking_pixel($order, $order_id)) {
        $total = $order->get_order_total();
    
        // INSERT TRACKING CODE or
        // DO YOUR CUSTOM BEHAVIOR HERE
      }
    } 
    
    add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
    
    ?>

    It can be inserted in Functions.php — just add “-tp” to the end of any product slug to load the tracking code or custom behavior on the thank you page. You can also change the specific text to be added to the slug on this line by changing the “-tp” to something else:

    if (preg_match('/-tp$/', $sku) === 1) {

    You do not need this plugin for this code to work.

    https://wordpress.org/plugins/woocommerce-conversion-tracking/

Viewing 12 replies - 1 through 12 (of 12 total)
  • @aapollo I see that you are posting this piece of code everywhere but does it work? I couldn’t get it working.

    Thread Starter aapollo

    (@aapollo)

    @thi_ – If you have a recent version of woocommerce installed, and you are adding -tp to the slug of a product, this code will work. Of course, you need to be using appropriate tracking pixel code as well. We used the default code supplied by Facebook for our site.

    Hi,
    Combining this code and code from: https://support.woothemes.com/hc/en-us/articles/203526455-Custom-Redirect-Based-on-Item-Purchased

    Shouldn’t this be the code to have the specific facebook pixel per event within woocommerce/wootickets? Where PRODUCTID and OTHER PRODUCTID are the unique productids for the woocommerce products?

    <?php
    // Load Tracking Pixel by Harlan T Wood & Adam Apollo
    // Facebook Tracking Pixel Code on Thank You Page for Specific Product
    // User adds -tp to product slug to load the conversion code
    function order_items_include_tracking_pixel($order, $order_id) {
      foreach ( $order->get_items() as $item ) {
        $product = $order->get_product_from_item( $item );
        if ( ! $product ) { continue; }
        $sku = $product->get_sku();
        if (preg_match('/-tp$/', $sku) === 1) {
          return true;
        }
      }
      return false;
    }
    
    function my_custom_tracking( $order_id ) {
      $order = new WC_Order( $order_id );
    
      if (order_items_include_tracking_pixel($order, $order_id)) {
        $total = $order->get_order_total();
        $order = wc_get_order( $order_id );
    	foreach( $order->get_items() as $item ) {
    		$_product = wc_get_product( $item['product_id'] );
    		// Add whatever product id you want below here
    		if ( $item['product_id'] == PRODUCTID ) {
     		   // INSERT PRODUCT SPECIFIC TRACKING CODE or
    		    // DO YOUR CUSTOM BEHAVIOR HERE
    		    // change below to the URL if you want to have a specific thankyou page that you want to send your 		    // customer to
    		    //  wp_redirect('http://www.yoururl.com/your-thank-you-page');
    		  }
    		if ( $item['product_id'] == OTHER PRODUCTID ) {
     		   // INSERT PRODUCT SPECIFIC TRACKING CODE or
    		    // DO YOUR CUSTOM BEHAVIOR HERE
    		  }
    	}
    add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
    
    ?>

    Greetings, Martijn

    Hi,

    I think the following is the correct code..Can anyone may be make this more performance based? Now There are two loops and may be it is possible with one loop?
    Thanks!

    // FB Pixel Tracking Code
    add_action( 'woocommerce_thankyou', 'fb_pixeltracking' );
    
    // Load Tracking Pixel by Harlan T Wood & Adam Apollo
    // Facebook Tracking Pixel Code on Thank You Page for Specific Product
    // User adds -tp to product slug to load the conversion code
    
    function order_items_include_tracking_pixel($order, $order_id) {
      foreach ( $order->get_items() as $item ) {
        $product = $order->get_product_from_item( $item );
        if ( ! $product ) { continue; }
        $sku = $product->get_sku();
        if (preg_match('/-tp$/', $sku) === 1) {
          return true;
        }
      }
      return false;
    }
    
    function fb_pixeltracking( $order_id ) {
       $order = new WC_Order( $order_id );
       $order_total = $order->get_total();
       if (order_items_include_tracking_pixel($order, $order_id)) {
       		$total = $order->get_order_total();
    
    	    $order = wc_get_order( $order_id );
    		foreach( $order->get_items() as $item ) {
    			$_product = wc_get_product( $item['product_id'] );
    			// Add whatever product id you want below here
    			if ( $item['product_id'] == 9892 ) {
    
     			?>
    			<!-- Start FB Tracking - Replace XXXXXXXXXXXXXX with your tracking ID to track orders with values-->
    
    	    	<!-- Facebook Conversion Code for Sales from Facebook Ads -->
        		<script>(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', 'XXXXXXXXXXXXXX', {'value':'<?php echo $order_total ?>','currency':'EUR'}]);
        		</script>
        		<noscript>
        			<img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/trev=XXXXXXXXXXXXXX&cd[value]=<?php echo $order_total ?>&cd[currency]=EUR&noscript=1" />
        		</noscript>
         		<!-- END FB Tracking -->
    
    			<?php
    			}
      		}
      	}
    }
    Plugin Author Tareq Hasan

    (@tareq1988)

    Sorry guys, didn’t think about that. Will update the plugin accordingly.

    Update: Fixed in v0.3

    Hi,
    Is the plugin now updated with:
    – On productpage possibility to add tracking code [Yes I think this is working]!
    and
    – When a product is sold, that on the thankyou page the (say FB pixel) triggers, instead on product selection page?

    Thanks for your reply in advance, greetings, Martijn

    Colin

    (@colinsafranek)

    @tareqhasan
    +1

    Could you please confirm the updated functionality in v0.3 ?

    In the plugin’s change log you said this:
    [fix] Product specific code only loads on product page itself, not checkout

    But could you specify how you fixed that issue, and how we should now utilize the product-specific tracking pixel field?

    My assumption is that, now, with the new update, any code/scripts that are input into the “Conversion Tracking Code” textarea field on a WC product edit page (in wp-admin), will be served or triggered/fired on the WC checkout endpoint ONLY if that particular product was purchased. Is this correct?

    Also, does this mean that if a customer purchases several products in one checkout, 3 of which have product-specific tracking scripts, that all 3 of those tracking scripts will fire on the WC checkout endpoint?

    Thank you for the clarification.

    Plugin Author Tareq Hasan

    (@tareq1988)

    Yeah I think you got it right.

    You insert tracking pixel for a product “Apple” in the product edit page in WooCommerce. That tracking code will be shown in the “Apple” page in the front-end. When someone purchase “Apple”, that tracking code will also be triggered at the WC Checkout page.

    Colin

    (@colinsafranek)

    hmm… that sounds like the tracking code will fire twice. Am I understanding that correctly? I just want to be sure, because if it fires on the product page, as well as on the checkout page, then that would create 2 “conversions” instead of one.

    Colin

    (@colinsafranek)

    @tareqhasan
    Can you confirm whether the tracking pixel will fire once or twice when applied to a specific product? Re: Above.

    Thanks.

    Plugin Author Tareq Hasan

    (@tareq1988)

    Yeah, right…twice. Seems thats not how it should work.

    Hi Tareq,

    I’m confused. Does the current plugin work correctly? In other words, does it fire only when a customer purchases a product? Or does it fire when a customer goes to the product page?

    I tried aapollos code in my child functions.php and it crashed my site with parse errors. Ouch!

    Anyhow, I really want to use your plugin, but am confused whether it gives the correct result. Also does it work with the current wordpress?

    Thank you very much!
    ~g

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Product specific code only loads on product page itself, not checkout (with FIX)’ is closed to new replies.