Woocommerce
Samantha avatar
Written by Samantha
Updated over a week ago

1. Install SmartPixel

In wp-content/themes/<YOUR-THEME>/header.php add the following code inside of the <head> tag:

<script>
    window['friendbuy'] = window['friendbuy'] || [];
    window['friendbuy'].push(['site', 'site-xxxxxx']);
<?php
$customer = wp_get_current_user();
if (!empty($customer->ID)) { ?>
window['friendbuy'].push(['track', 'customer', {
   id: '<?php echo $customer->ID; ?>',
   email: '<?php echo $customer->user_email; ?>'
}]);
<?php } ?>
(function (f, r, n, d, b, y) {
    b = f.createElement(r), y = f.getElementsByTagName(r)[0];b.async = 1;b.src = n;y.parentNode.insertBefore(b, y);
    })(document, 'script', '//djnf6e5yyirys.cloudfront.net/js/friendbuy.min.js');
</script>

Add the widget script and tag as you would on any other site.

2. Install Conversion Tracker

In wp-content/themes/<YOUR-THEME>/functions.php add the following code to the end of the file:

<?php
add_action('woocommerce_thankyou', 'friendbuy_conversion_tracker');

/**
 * Generates the conversion tracking part of the Friendbuy SmartPixel. This will
 * ensure that the purchase is tracked in your Friendbuy account so that we can
 * accurately track conversions, issue any configured rewards, etc.
 *
 * @param mixed $order_id The ID of the order, as generated by WooCommerce.
 */
function friendbuy_conversion_tracker($order_id) {
    $order = new WC_Order($order_id);
    $format_product = function ($item) use ($order) {
        $product = $order->get_product_from_item($item);
        return array(
            'sku' => $product->get_sku(),
            'price' => $order->get_line_subtotal($item),
            'quantity' => $item['qty']
        );
    };
    $is_first_order = function ($order) {
        $customer_orders = get_posts(array(
            'numberposts' => -1,
            'meta_key'    => '_billing_email',
            'meta_value'  => $order->billing_email,
            'post_type'   => wc_get_order_types(),
            'post_status' => array_keys(wc_get_order_statuses())
        ));
        return count($customer_orders) === 1;
    };
    $order_data = json_encode(array(
        'id' => $order->get_order_number(),
        'amount' => $order->get_total(),
        'email' => $order->billing_email,
        'new_customer' => $is_first_order($order)
    ));
    $product_data = json_encode(array_map($format_product, $order->get_items()));

    echo <<<SMARTPIXEL
    <script>
        window['friendbuy'] = window['friendbuy'] || [];
        window['friendbuy'].push(['track', 'order', $order_data]);
        window['friendbuy'].push(['track', 'products', $product_data]);
    </script>
SMARTPIXEL;
}

3. Install Widgets on Your Site

Use Friendbuy to install and configure widgets on your site.

Did this answer your question?