WooCommerce Conversion Tracking Script for two Pixel

强颜欢笑 提交于 2019-12-31 06:56:06

问题


I want to promote my products by some affiliate networks.

Do only thing you have to do, is to go into the function.php file and add this script with the pixel. With this script the tracking of the amount value works fine. This script works only for one network and if you are the only vendor.

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
  $order = new WC_Order( $order_id );
  $total = $order->get_subtotal();
  $id = str_replace('#', '', $order->get_order_number());
  echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
}

My problem: I have multiple Vendors who are using my plattform for Product delivery / purchase processing.

I need to know how i can modify the function file in order to add a working second script for a 2nd pixel if a specific product has been selected and bought.

My it skills in woocommerce are limited, so i would like to understand how to modify the script without harming the (general) tracking.

  1. If somebody buys the "normal" Products than the 1st pixel above should fire.
  2. If somebody buys a specific product with the Product ID 2004 - than a 2nd different pixels needs to fire and ignore the first pixel.

Do i need to add a second function or modify the first one?

Thank you

Additional questions (Update 16.05.2017)

In the future I will probably have to install a third pixel. How would the structure be?

add_action('woocommerce_thankyou', 'wh_custom_tracking');

function wh_custom_tracking($order_id)
{
    $product_ids = [2004, 2000]; //<-- list of product_id(s) for which 2nd pixels should fire
    $checkSecond = FALSE;
    $product_ids = [2003, 2001]; //<-- list of product_id(s) for which 3nd pixels should fire
 $checkThird = FALSE;
    $order = wc_get_order($order_id);
    $total = $order->get_subtotal();
    $id = str_replace('#', '', $order->get_order_number());

    $items = $order->get_items();

    foreach ($items as $item)
    {
        $item_id = $item['product_id']; // <= Here is your product ID
        if (in_array($item_id, $product_ids))

        {
            $checkSecond = TRUE;
            break;
        }

 {
            $checkThird = TRUE;
            break;
        }
    }

    if ($checkSecond)
    {
        //add your 2nd pixel here 2nd pixel
    }
    else

    if ($checkThird)
    {
        //add your 3nd pixel here 2nd pixel
    }
    else
    {
        echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
    }
}

Is the same structure also valid for variation IDs?

In the affiliate software within the offer a "Target pixel" and the "final pixel" can be used.

Some products are "test products" and have a value of € 0.00. If the main pixel fires, then the affiliate receives no compensation, even if the customer afterwards purchases the product.

In this case, a kind of target pixel would have to be installed for the variation ID of a particular product. If the customer decides after the test month for the purchase, then the "right pixel" should fire.


回答1:


You can get the list of product ID(s) by Order ID, and then you can apply your conditioning and trigger different pixel.

Here is a sample code which should solve your query:

add_action('woocommerce_thankyou', 'wh_custom_tracking');

function wh_custom_tracking($order_id)
{
    $product_ids = [2004, 2000]; //<-- list of product_id(s) for which 2nd pixels should fire
    $checkSecond = FALSE;
    $order = wc_get_order($order_id);
    $total = $order->get_subtotal();
    $id = str_replace('#', '', $order->get_order_number());

    $items = $order->get_items();

    foreach ($items as $item)
    {
        $item_id = $item['product_id']; // <= Here is your product ID
        if (in_array($item_id, $product_ids))
        {
            $checkSecond = TRUE;
            break;
        }
    }

    if ($checkSecond)
    {
        //add your 2nd pixel here 2nd pixel
    }
    else
    {
        echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
    }
}

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.

Related Question: How to insert Google Merchant Review JS code in WooCommerce Order Complete page

Hope this helps!



来源:https://stackoverflow.com/questions/43988396/woocommerce-conversion-tracking-script-for-two-pixel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!