drsxobip501258 2018-12-21 18:58
浏览 447
已采纳

跟踪添加到购物车并提交Facebook Pixel In Woocommerce的订单

I am desperately trying for days to get unique tracking for AddToCart and Purchase Tracking in Facebook Pixel. Having a default setting of 0.00 gives wrong conversion results.

I am desperately trying for days to get unique tracking for AddToCart and Purchase Tracking in Facebook Pixel. Having a default setting of 0.00 gives wrong conversion results.

We are using WooCommerce and I am implementing FB Pixel Through Tag Manager.

All events are tracking but I want to get a AddToCart value for AddToCart and a total Cart amount on purchasing. WooCommerce is using a data layer and I tried to use the Data Layer of ecomm_total and price but I get the error

Invalid AddToCart Value Parameter

Example of our code is below

Is there any value I can replace 0.00 with to get the correct values for each product and order?

<script>
fbq('track', 'AddToCart', {
value: 0.00,
currency: 'SEK',
});
</script>
  • 写回答

1条回答 默认 最新

  • douwa1304 2018-12-22 02:05
    关注

    Based on Conditional Logic and custom FB Pixels Integration into WooCommerce answer code, the following will set main events and price values from "AddToCart" and "Purchase" events:

    // Function that gets the Ajax data
    add_action( 'wp_ajax_product_added_to_cart', 'wc_product_added_to_cart' );
    add_action( 'wp_ajax_nopriv_product_added_to_cart', 'wc_product_added_to_cart' );
    function wc_product_added_to_cart() {
        if ( isset($_POST['pid']) ){
            // Get an instance of the WCW_Product Object
            $product = wc_get_product( $_POST['pid'] );
    
            // Return the product price including taxes
            echo number_format( wc_get_price_including_tax( $product ), 2 );
    
            // OR =>the product price EXCLUDING taxes
            // echo number_format( wc_get_price_excluding_tax( $product ), 2 );
        }
        die(); // Alway at the end (to avoid server error 500)
    }
    
    // FB PiXELS HEAD Script (Initializing)
    add_action( 'wp_head', 'fbpixels_head_script' );
    function fbpixels_head_script() {
        ?>
        <script>
        !function(f,b,e,v,n,t,s)
        {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
        n.callMethod.apply(n,arguments):n.queue.push(arguments)};
        if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
        n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];
        s.parentNode.insertBefore(t,s)}(window, document,'script',
        'https://connect.facebook.net/en_US/fbevents.js');
        </script>
        <?php
    }
    
    // FB PiXELS Footer script Events
    add_action( 'wp_footer', 'fbpixels_add_to_cart_script' );
    function fbpixels_add_to_cart_script(){
        // HERE set your init reference
        $init_id = '1552143158136112';
    
        // HERE set the product currency code
        $currency = 'SEK';
    
        ## 0. Common script -  On ALL Pages
        ?>
        <script>
        fbq('init', <?php echo $init_id; ?>);
        fbq('track', 'PageView');
        <?php
    
        ## 1. On Checkout page
        if ( ! is_wc_endpoint_url( 'order-received' ) && is_checkout() ) {
        ?>
        fbq('track', 'InitiateCheckout');
        <?php
    
        ## 2. On Order received (thankyou)
        } elseif ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;
    
        // Get the Order ID from Query vars
        $order_id  = absint( $wp->query_vars['order-received'] );
    
        if ( $order_id > 0 ){
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
        ?>
        fbq('track', 'Purchase', {
            value:    <?php echo $order->get_total(); ?>,
            currency: '<?php echo $order->get_order_currency(); ?>',
        });
        <?php
        }
        ## 3. Other pages - (EXCEPT Order received and Checkout)
        } else {
    
        ?>
        jQuery(function($){
            if (typeof woocommerce_params === 'undefined')
                return false;
    
            var price;
            $('body').on( 'adding_to_cart', function(a,b,d){
                var sku = d.product_sku, // product Sku
                    pid = d.product_id,  // product ID
                    qty = d.quantity;    // Quantity
    
                $.ajax({
                    url: woocommerce_params.ajax_url,
                    type: 'POST',
                    data: {
                        'action' : 'product_added_to_cart',
                        'sku'    : sku,
                        'pid'    : pid,
                        'qty'    : qty
                    },
                    success: function(result) {
                        // The FB Pixels script for AddToCart
                        if( result > 0 ){
                            fbq('track', 'AddToCart', {
                                value:    result,
                                currency: '<?php echo $currency; ?>',
                            });
                            console.log(result);
                        }
                    }
                });
            });
        });
        <?php
        }
        ## For single product pages - Normal add to cart
        if( is_product() && isset($_POST['add-to-cart']) ){
            global $product;
    
            // variable product
            if( $product->is_type('variable') && isset($_POST['variation_id']) ) {
                $product_id = $_POST['variation_id'];
                $price = number_format( wc_get_price_including_tax( wc_get_product($_POST['variation_id']) ), 2 );
            }
            // Simple product
            elseif ( $product->is_type('simple') ) {
                $product_id = $product->get_id();
                $price = number_format( wc_get_price_including_tax( $product ), 2 );
            }
            $quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 1;
    
            ?>
            fbq('track', 'AddToCart', {
                value:    <?php echo $price; ?>,
                currency: '<?php echo $currency; ?>',
            });
            <?php
        }
        ?>
        </script>
        <noscript>
        <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=<?php echo $init_id; ?>&ev=PageView&noscript=1" />
        </noscript>
        <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme). It seems working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能