Skip to main content

⚙️ WooCommerce (WordPress)

After this guide, you will have sale conversion tracking enabled for your WooCommerce WordPress store.

To make yourself familiar with Linkster tracking, it is recommended that you read through the introduction and overview to tracking methods first.

caution

The user tracking container must be installed first before you can start using this integration.

Sale conversion script

The following script provides full-featured sale conversion tracking.

This script has to be placed inside the functions.php file of your active WordPress theme. Make sure to replace YOUR_CAMPAIGN_ID with your campaign ID (the same one you are using for the user tracking container script).

functions.php
<?php
function linkster_conversion_tracking($order_id)
{
//getting order object
$order = wc_get_order($order_id);

$ordertoken = $order->get_order_number();
$items = $order->get_items();
?>

<script>
var campaign_id = YOUR_CAMPAIGN_ID;
var attribution = 1;
var cookieEmid = document.cookie.match(/emid\=([a-zA-Z0-9\-\_]+)/i);
var emid = window.localStorage['emid'] || (cookieEmid === null ? void 0 : cookieEmid[1]);

/* order variables */
var ordertoken = '<?php echo $ordertoken; ?>';
var triggerId = 1;
var currency = '<?php echo $order->get_currency(); ?>';
var voucherCode = '<?php echo implode(',', $order->get_coupon_codes()); ?>';
var basket = [];

/* basket item object */
/* foreach item in basket */
<?php foreach ( $items as $item ) : ?>
basket.push({
'campaign_id': campaign_id,
'attribution': attribution,
'emid': emid,
'trigger_id': triggerId,
'token': ordertoken,
'article_number': '<?php echo $item->get_product_id(); ?>',
'productname': '<?php echo $item->get_name(); ?>',
'category': '<?php echo implode(',', wp_list_pluck( get_the_terms($item->get_product_id(), 'product_cat') ,'name') ); ?>',
'amount': '<?php echo $item->get_quantity(); ?>',
'price': '<?php echo $order->get_item_total($item); ?>',
});
<?php endforeach; ?>
/* end foreach */

var trackingUrl = 'https://trck.linkster.co/trck/ebasket/?json='+encodeURIComponent(JSON.stringify(basket))+'&currency='+currency+'&vc='+encodeURIComponent(voucherCode);
var req = new XMLHttpRequest(); req.withCredentials = true; req.open("GET", trackingUrl); req.send();
</script>

<?php
}

add_action('woocommerce_thankyou', 'linkster_conversion_tracking');

?>