Install Microsoft Ads (Bing) conversion tracking for WooCommerce

This post will show you two ways of installing Microsoft Ads (Bing) tracking codes into Woocommerce. The manual way, which is adding a function and some hooks to your functions.php file (for those who want to learn and understand how it works) and the install plugin way for those who aren’t interested in how it works and simply want to install our plugin and have it done automatically.

The point of this guide is really to help people who have are selling products through WooCommerce and want to push the sales values back into Microsoft Ads.

Create the necessary tags and goals in Microsoft Ads

Step 1: Generate a Microsoft UET Tag ID from Microsoft Ads

The first thing you need to do in Microsoft Ads is to create a unique UET tag for your account/campaign. To do this, follow the below steps:

  • In Microsoft Ads, go to Tools > UET Tag

  • Click Create

  • Once you have your UET Tag – go to Step 2

Step 2: Create a conversion goal in Microsoft Ads to track purchases

  • In Microsoft Ads go to Tools >Conversion Goals

  • Click Create

  • What type of conversions do you want to track? = Website

  • Goal Category = Purchase

  • Goal Type = Event

  • Name = Whatever you like

  • Revenue = Conversion action may vary

  • Default Value = 0

  • Currency: Your Currency

  • Advanced Settings: leave as is

  • Yes, the UET tag was already installed on all website pages…

  • Install Event Tag = Manual Installation

  • Action > Equals to > purchase

  • Category > Equals to > sale

  • When do you want to track this event? = Track event on page load.

Install event tag

screenshot

Installing tracking code into WooCommerce – Manual Way

To add the tracking code into your WooCommerce store, we will add two functions into your functions.php file of your child theme. The first function’s purpose will be to install the modified tracking code which pushes the sale value back to your Microsoft Ads conversion goal. All of the below is entered into the functions.php file of your child theme.

1. Hook into WooCommerce thankyou page

First we need to hook into the WooCommerce thank you page, or the order-received end point. This is the page that congratulates the buyer for purchasing from your store, and includes the cart value they purchased. When this page appears for a customer, we want to capture the value and send it back to your Google Ads conversion goal.

add_action('woocommerce_thankyou', 'custom_tracking');

2. Add the first function

function custom_tracking($order_id)
{
//Grab the order
$order = wc_get_order($order_id);
//Get the entire order total
$total = $order->get_total();
//Select currency
$currency = 'AUD';
// bing tracking code:
?>

Modified value push bing tracking code starts here
(function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"#########"};o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")},n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function(){var s=this.readyState;s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)},i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})(window,document,"script","//bat.bing.com/bat.js","uetq");
window.uetq = window.uetq || [];
window.uetq.push('event', 'purchase', {'event_category': 'sale', 'event_value': '', 'revenue_value': '', 'currency': ''});

Modified value push bing tracking code ends here

<?php>

}

You may need to replace the currency code to your own if you are outside Australia, but most importantly you will have to replace the section in green where it says “{ti:” with your Microsoft Ads UET Tag ID. The code within the green areas need to start with script, and end with script as it is Javascript code.

3. Add the second function

function general_bing_tracking_code()
{
if (!is_wc_endpoint_url('order-received'))
{
?>
Standard Bing Tracking Code Starts Here
(function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"#########"};o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")},n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function(){var s=this.readyState;s&&s!=="loaded"&&s!=="complete"|| (f(),n.onload=n.onreadystatechange=null)},i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})(window,document,"script","//bat.bing.com/bat.js","uetq"); window.uetq = window.uetq || [];
Standard Bing Tracking Code Ends Here
<?php
}
}

Again, replace the “{ti:” with your UET Tag ID. The code within the green areas need to start with script, and end with script as it is Javascript code.

4. Hook into your page headers and run the second function.

add_action('wp_head', 'general_bing_tracking_code');

Full Code

/*BING TRACKING CODE*/

//Add custom tracking code to Woocommerce thank you page to grab order values
add_action( 'woocommerce_thankyou', 'custom_tracking' );
function custom_tracking($order_id)
{
//Grab the order
$order = wc_get_order($order_id);
//Get the entire order total
$total = $order->get_total();
//Select currency
$currency = 'AUD';
// bing tracking code:
?>
Modified value push bing tracking code starts here

<script>

(function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"########"};o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")},n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function(){var s=this.readyState;s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)},i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})(window,document,"script","//bat.bing.com/bat.js","uetq"); window.uetq = window.uetq || [];

window.uetq.push('event', 'purchase', {'event_category': 'sale', 'event_value': '', 'revenue_value': '', 'currency': ''});
</script>
Modified value push bing tracking code ends her

<?php
}

function general_bing_tracking_code()
{
if (!is_wc_endpoint_url('order-received'))
{
?>
Standard Bing Tracking Code Starts Here
<script>

(function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"########"};o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")},n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function(){var
s=this.readyState;s&&s!=="loaded"&&s!=="complete"|| (f(),n.onload=n.onreadystatechange=null)},i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})(window,document,"script","//bat.bing.com/bat.js","uetq");
window.uetq = window.uetq || [];
</script>

Standard Bing Tracking Code Ends Here

<?php
}
}
add_action('wp_head', 'general_bing_tracking_code');

Installing tracking code into WooCommerce – With Plugin

We’ve made the above code into an easy to install free WordPress plugin for you if you’re not interested in the manual way!

Download Free Plugin