Track WooCommerce Sales In TikTok Without Plugin

Tracking WooCommerce sales on your TikTok Pixel is not hard, and you don’t need slow and insecure plugins. This code is minimal and will not add any overhead to the loading of WordPress. Use this instead of bloated plugins with ads and slow functionality.

The first thing you have to do is create a TikTok Business account and create your TikTok Pixel. After that, you will be able to find your TikTok Pixel ID (called Pixel Code – not to be confused with the actual complete pixel tracking code). See this explanation to learn how: https://ads.tiktok.com/help/article?aid=10021

You only need the TikTok Pixel ID, not the complete pixel code, for tracking sales. If you also want to track page views, you can follow the tutorial on the link above. In addition, you will need an access token for the Events API. You can learn how to get this here: https://ads.tiktok.com/marketing_api/docs?rid=p41a33fdhon&id=1727537566862337

/**
 * Track TikTok Purchases From WooCommerce
 */

function trsdm_track_tiktock_purchase($oid)
{
    $order = wc_get_order($oid);

    if (!$order) {
        return;
    }

    $email = hash("sha256", $order->get_billing_email());
    $phone = hash("sha256", $order->get_billing_phone());
    $uniqueId = hash("sha256", $order->get_billing_email() . "_" . $order->get_billing_phone());
    $ip = $order->get_customer_ip_address();
    $userAgent = $order->get_customer_user_agent();

    $datetime = new DateTime();
    $timestamp = $datetime->format('c');

    $contents = [];

    foreach ($order->get_items() as $item) {
        $product = $item->get_product();

        if (!$product) {
            continue;
        }

        $sku = $product->get_sku() ? $product->get_sku() : $product->get_id();
        $price = $order->get_item_total($item, true, true);
        $quantity = $item->get_quantity();

        $contents[] = [
            'price' => round($price, 2),
            'quantity' => (int) $quantity,
            'content_type' => 'product',
            'content_id' => $sku,
        ];
    }

    $orderTotal = round($order->get_total(), 2);

    $body = json_encode([
        'pixel_code' => 'YOUR PIXEL CODE', //PUT YOUR PIXEL CODE HERE
        'event' => 'CompletePayment',
        'event_id' => $oid,
        'timestamp' => $timestamp,
        'context' => [
            'page' => [
                'url' => wc_get_checkout_url(),
            ],
            'user' => [
                'external_id' => $uniqueId,
                'phone_number' => $phone,
                'email' => $email,
            ],
            'ip' => $ip,
            'user_agent' => $userAgent,
        ],
        'properties' => [
            'contents' => $contents,
            'currency' => get_woocommerce_currency(),
            'value' => $orderTotal,
        ],
    ]);

    wp_remote_post('https://business-api.tiktok.com/open_api/v1.2/pixel/track/', [
        'headers' => [
            'Access-Token' => 'YOUR ACCESS TOKEN', //PUT YOUR ACCESS TOKEN HERE
            'Content-Type' => 'application/json',
        ],
        'body' => $body,
        'timeout' => 2,
    ]);
}
add_action('woocommerce_payment_complete', 'trsdm_track_tiktock_purchase');Code language: PHP (php)

Code explained:

The code triggers when the customer completes payment for an order. We collect and encrypt the relevant customer information, as well as generate a unique ID for the customer. We also loop the order to get a list of all products and the order total. We use wp_remote_post to send this to TikTok.

Remember to input your own pixel code and access token where prompted in the code.

Where to put it:

The code goes in the functions.php file of your active (child) theme, or in a plugin.

Get these knowledge bombs before they are released

Plus exclusive content only for newsletter family members! No spam or yucky sales tactics. The bombs only drop occasionally for a clutter-free inbox.