> For the complete documentation index, see [llms.txt](https://alhena.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alhena.gitbook.io/docs/developer-reference/website-sdk/examples/woocommerce-revenue-tracking.md).

# WooCommerce Revenue Tracking

Track cart and checkout events in WooCommerce for revenue attribution.

This example shows how to implement Alhena AI revenue tracking in WooCommerce, enabling you to see which AI conversations influence purchases.

## What You'll Learn

* Add the tracking snippet to your WooCommerce site
* Track add-to-cart events
* Track checkout completions on the thank-you page

## Use Case

When customers interact with your Alhena AI assistant before making a purchase, you want to attribute that revenue to the AI. This implementation captures:

1. Items added to cart (with product details)
2. Completed orders (with full line item data)

This data flows to your Alhena dashboard under **Analytics > Revenue Impact**.

## Prerequisites

* Alhena SDK installed on your WooCommerce site
* Access to your theme's `functions.php` or a custom plugin
* WooCommerce 3.0 or later

## Code Example

Add this code to your theme's `functions.php` or a custom plugin:

### 1. Add tracking snippet to header

```php
add_action('wp_head', function() {
    ?>
    <script>
    !(function (w) {
        w._alhenaEventQueue = w._alhenaEventQueue || [];
        w.trackAlhenaEvent = w.trackAlhenaEvent || function (method, data) {
            w._alhenaEventQueue.push({ method: method, args: data });
        };
    })(window);
    </script>
    <?php
});
```

### 2. Track add-to-cart events

```php
add_action('wp_footer', function() {
    ?>
    <script>
    jQuery(document).on('added_to_cart', function(event, fragments, cart_hash, button) {
        var productId = button.data('product_id');
        var productName = button.data('product_name') || 'Product';
        var price = parseFloat(button.data('price')) || 0;

        trackAlhenaEvent('sendCartEvent', {
            type: 'ITEM_ADDED',
            productId: String(productId),
            productName: productName,
            value: price,
            currency: '<?php echo get_woocommerce_currency(); ?>',
            quantity: 1
        });
    });
    </script>
    <?php
});
```

### 3. Track checkout on thank-you page

```php
add_action('woocommerce_thankyou', function($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;

    $line_items = [];
    foreach ($order->get_items() as $item) {
        $product = $item->get_product();
        $line_items[] = [
            'productId' => (string) $product->get_id(),
            'productName' => $item->get_name(),
            'quantity' => $item->get_quantity(),
            'value' => (float) $item->get_total(),
            'currency' => $order->get_currency()
        ];
    }
    ?>
    <script>
    trackAlhenaEvent('sendCheckoutEvent', {
        orderId: '<?php echo $order_id; ?>',
        value: <?php echo $order->get_total(); ?>,
        currency: '<?php echo $order->get_currency(); ?>',
        lineItems: <?php echo json_encode($line_items); ?>
    });
    </script>
    <?php
});
```

## How It Works

1. **Tracking snippet**: Initializes the event queue so tracking calls work even before the full SDK loads
2. **Add-to-cart**: Listens for WooCommerce's `added_to_cart` jQuery event and captures product details from the button's data attributes
3. **Thank-you page**: Hooks into `woocommerce_thankyou` to fire a checkout event with the complete order data

The events are matched to any AI conversations the customer had within the attribution window (24 hours by default).

## Customization

### Track cart removal

```php
add_action('wp_footer', function() {
    ?>
    <script>
    jQuery(document).on('removed_from_cart', function(event, fragments, cart_hash, button) {
        var productId = button.data('product_id');

        trackAlhenaEvent('sendCartEvent', {
            type: 'ITEM_REMOVED',
            productId: String(productId)
        });
    });
    </script>
    <?php
});
```

### Add custom product data

If your add-to-cart buttons don't have `data-product_name` or `data-price` attributes, you can extend the tracking:

```php
add_action('wp_footer', function() {
    ?>
    <script>
    jQuery(document).on('added_to_cart', function(event, fragments, cart_hash, button) {
        var productId = button.data('product_id');

        // Fetch product details via AJAX if needed
        jQuery.get('/wp-json/wc/v3/products/' + productId, function(product) {
            trackAlhenaEvent('sendCartEvent', {
                type: 'ITEM_ADDED',
                productId: String(productId),
                productName: product.name,
                value: parseFloat(product.price),
                currency: '<?php echo get_woocommerce_currency(); ?>',
                quantity: 1
            });
        });
    });
    </script>
    <?php
});
```

## Related

* [Revenue Attribution](/docs/developer-reference/website-sdk/cart-checkout-events/revenue-attribution.md) - How attribution works
* [Cart & Checkout Event API](/docs/developer-reference/website-sdk/cart-checkout-events.md) - JavaScript tracking API
* [HTTP APIs](/docs/developer-reference/website-sdk/cart-checkout-events/http-apis.md) - Server-side tracking endpoints
