BLOG


Adding order total to third party tracking scripts on Woofunnels One Click Upsell

by Lance Meman on 19 February, 2021

WFOCU only support a handful of third party tracking scripts you can set up on their settings page. So adding other scripts not available there can be quite tricky. We will use Outbrain in this example.

Insert the base Outbrain Pixel in the <head></head> section of each page of your site.

<!-- Outbrain -->
<script data-obct type="text/javascript">
  // DO NOT MODIFY THIS CODE
  !function(_window, _document) {
    var OB_ADV_ID='XXXXXXXXXXXXXXXXX';
    if (_window.obApi) {var toArray = function(object) {return Object.prototype.toString.call(object) === '[object Array]' ? object : [object];};_window.obApi.marketerId = toArray(_window.obApi.marketerId).concat(toArray(OB_ADV_ID));return;}
    var api = _window.obApi = function() {api.dispatch ? api.dispatch.apply(api, arguments) : api.queue.push(arguments);};api.version = '1.1';api.loaded = true;api.marketerId = OB_ADV_ID;api.queue = [];var tag = _document.createElement('script');tag.async = true;tag.src = '//amplify.outbrain.com/cp/obtp.js';tag.type = 'text/javascript';var script = _document.getElementsByTagName('script')[0];script.parentNode.insertBefore(tag, script);}(window, document);

obApi('track', 'PAGE_VIEW');
</script>
<!-- Outbrain End -->

Next to track the Purchase event data usually we do it on the thank you page of woocommerce. But in our case we are upselling items to the customers before redirecting them to the thank you page.

This additional step can mess up our data since there is a possibility that the customer will abandon the website before reaching the thank you page.

That is why we need to send the purchase event every time the customer accepts an upsell/downsell offer.

So we are going to use the wfocu_custom_purchase_tracking hook in order to achieve this. This hook is used by wfocu to fire the purchase events of those found on the wfocu settings like facebook and google ads. This will fire on the next page after an offer is accepted either it be another upsell/downsell page or the thank you page.

With that out of the way lets proceed with adding the codes.

Note: Create a backup before editing your functions.php.

Insert the following code to your functions.php:

add_action( 'wfocu_custom_purchase_tracking', 'lm_custom_tracking_scripts' );
function lm_custom_tracking_scripts($general_data) {
    $init_order = $general_data;
    $init_total = $init_order['total'];  //Order Total
    $init_qty = $init_order['num_qty'];  //Quantity
    $init_orderID = strip_tags($init_order['transaction_id']);  //Order ID
    $init_id = $init_order['products'][0]['id'];  //Product ID
    
    echo "<script type=\"application/javascript\">
    setTimeout(function(){
        
    //Outbrain Event
    obApi('track', 'Purchased Tea', {
            orderValue: '$init_total', //dynamic order value from previous transaction *(Initial order or Upsell)
            currency: 'USD',
            orderId: '$init_orderID' //Order ID
        })
    
    }, 2000);
    </script>";

There you have it. Just Edit the event code with the data you need like order total, order Id or product Id.