Skip to main content

WooCommerce Manual Integration

Written by David Rolenc
Updated over a week ago

If you are working with WordPress as your CMS and want to manually integrate tracking into your WooCommerce store without using our Plugin Integration method, you can follow the method below.

To do this safely, we will use the free Code Snippets plugin, which is the easiest way to add custom PHP code to WordPress without the risk of breaking your website.


1. What You Will Need

Before you begin, make sure you have the following ready:

  • Your Tenant ID (You can find your Tenant ID by following these steps).

  • Your Conversion Type (e.g., sale, lead).



2. Step-by-Step Instructions (using the Code Snippets Plugin)

  1. In your WordPress admin dashboard, go to Plugins > Add New Plugin.

  2. Search for the Code Snippets plugin, install it, and click Activate.

  3. In your left menu, navigate to Snippets > Add New.

  4. Give it a descriptive title, such as "Trackdesk WooCommerce Integration".

  5. Copy the Base Tracking Code provided below and paste it into the main code box.

  6. Make sure to select the option "Run snippet everywhere". This is important to ensure click tracking works across your entire site.

  7. Click the Save Changes and Activate button.


3. The Base Tracking Code

Copy this code and be sure to replace the uppercase placeholders with your actual Trackdesk details.

/* * TRACKDESK INTEGRATION: Click & Conversion Tracking for WooCommerce
*/

// 1. Click Tracking

add_action( 'wp_head', 'trackdesk_global_click_tracking' );

function trackdesk_global_click_tracking() {
?>
<script async src="//cdn.trackdesk.com/tracking.js"></script>
<script>
(function(t,d,k){(t[k]=t[k]||[]).push(d);t[d]=t[d]||t[k].f||function(){(t[d].q=t[d].q||[]).push(arguments)}})(window,"trackdesk","TrackdeskObject");

// REPLACE 'YOUR_TENANT_ID' with your actual Trackdesk Tenant ID
trackdesk("YOUR_TENANT_ID", "click");
</script>
<?php
}

// 2. Conversion Tracking

add_action( 'woocommerce_thankyou', 'trackdesk_conversion_event', 10, 1 );

function trackdesk_conversion_event( $order_id ) {
if ( ! $order_id ) return;
$order = wc_get_order( $order_id );
if ( ! $order ) return;

// Extract dynamic variables from WooCommerce
$td_total = $order->get_total() - $order->get_shipping_total();
$td_order_id = $order->get_id();

?>
<script>
// REPLACE 'YOUR_TENANT_ID' and 'YOUR_CONVERSION_TYPE'
trackdesk("YOUR_TENANT_ID", "conversion", {
"conversionType": "YOUR_CONVERSION_TYPE",
"externalId": "<?php echo esc_js( $td_order_id ); ?>",
"amount": {
"value": "<?php echo esc_js( $td_total ); ?>"
}
});
</script>
<?php
}

⚠️ Important: Remember to replace:

  • YOUR_TENANT_ID: In both places it appears.

  • YOUR_CONVERSION_TYPE: The conversion type you want (e.g., sale).


4. Coupon Tracking (Optional)

If you want to use coupons as part of your conversion attribution when a customer uses an affiliate's coupon code, replace the entire Part 2 (Conversion Tracking) block above with the code below.

This updated version will automatically extract the used coupons from the WooCommerce cart and send them to Trackdesk. You can read more about the complete Coupons feature here.

// 2. Conversion Tracking with Coupons

add_action( 'woocommerce_thankyou', 'trackdesk_conversion_event', 10, 1 );

function trackdesk_conversion_event( $order_id ) {
if ( ! $order_id ) return;
$order = wc_get_order( $order_id );
if ( ! $order ) return;

// Extract dynamic variables from WooCommerce
$td_total = $order->get_total() - $order->get_shipping_total();
$td_order_id = $order->get_id();

// WooCommerce returns an array of coupon codes used in the order
$td_coupons = $order->get_coupon_codes();

?>
<script>
// REPLACE the uppercase placeholders with your actual details
trackdesk("YOUR_TENANT_ID", "conversion", {
"conversionType": "YOUR_CONVERSION_TYPE",
"couponCodes": <?php echo json_encode( $td_coupons ); ?>,
"revenueOriginId": "YOUR_REVENUE_ORIGIN_ID",
"externalId": "<?php echo esc_js( $td_order_id ); ?>",
"amount": {
"value": "<?php echo esc_js( $td_total ); ?>"
}
});
</script>
<?php
}

⚠️ Important: Remember to replace:

  • YOUR_TENANT_ID: Your actual Tenant ID.

  • YOUR_CONVERSION_TYPE: The conversion type you want the coupon to be created with (e.g., sale).

  • YOUR_REVENUE_ORIGIN_ID: Your actual Revenue Origin ID. You can find your Revenue Origin ID by following these steps.


5. Testing Your Integration

Please remember to test the creation of a click and a conversion before starting your program. This way, you can verify that tracking has been set up correctly. You can create a test click and conversion by following the steps described here.

Did this answer your question?