Ecommerce
Add Loogika to WooCommerce
WooCommerce runs on WordPress, so the steps for the Loogika tag are those of a plain WordPress site. Pageviews on shop pages are then recorded without more work. To record purchases and add-to-cart clicks, you add a few WooCommerce action hooks that send them as custom events.
Last updated .
How do you add the Loogika tag to WooCommerce?
Follow the WordPress guide and pick the functions.php method or the small plugin method. The event hooks on this page are PHP, and they belong in the same file as the tag.
function loogika_add_tracking_script() {
?>
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
<?php
}
add_action( 'wp_head', 'loogika_add_tracking_script' );
Replace YOUR_SITE_ID with the site ID from Site Settings in Loogika, under the Tracking Script tab. Put the code in the functions.php file of a child theme or in a plugin of your own, never in the parent theme.
How do WooCommerce events reach Loogika?
Every stage of a purchase triggers a PHP action hook in WooCommerce. Each example below uses one hook to print a short inline script that calls window.loogika.event().
The inline scripts wait for DOMContentLoaded before they send anything. Because the Loogika tag carries defer, the browser has not yet created window.loogika while it reads the page. Deferred scripts run before DOMContentLoaded, which means the function is ready when the event fires.
How do you track a purchase in WooCommerce?
Use the woocommerce_thankyou hook. WooCommerce fires it on the order received page and passes the order ID.
add_action( 'woocommerce_thankyou', 'loogika_track_purchase', 10, 1 );
function loogika_track_purchase( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$purchase = array(
'transaction_id' => $order->get_order_number(),
'value' => (float) $order->get_total(),
'tax' => (float) $order->get_total_tax(),
'shipping' => (float) $order->get_shipping_total(),
'currency' => $order->get_currency(),
);
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
window.loogika?.event('purchase', <?php echo wp_json_encode( $purchase ); ?>);
});
</script>
<?php
}
The event carries the order number, the total, tax, shipping and the currency. Loogika accepts one flat object of properties with text and number values, so the example sends order totals and no list of products.
How do you track add-to-cart clicks in WooCommerce?
You need two hooks, because WooCommerce adds products to the cart in two ways. The first hook covers the form on a single product page. The second listens for the added_to_cart jQuery event. WooCommerce fires that event when a visitor uses an AJAX button on a shop or category page.
// Single product page: track on form submit
add_action( 'woocommerce_after_add_to_cart_button', 'loogika_track_add_to_cart' );
function loogika_track_add_to_cart() {
global $product;
if ( ! $product instanceof WC_Product ) {
return;
}
$data = array(
'item_id' => $product->get_sku() ? $product->get_sku() : $product->get_id(),
'item_name' => $product->get_name(),
'price' => (float) $product->get_price(),
'currency' => get_woocommerce_currency(),
);
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
var form = document.querySelector('form.cart');
if (!form) return;
form.addEventListener('submit', function () {
var data = <?php echo wp_json_encode( $data ); ?>;
var qty = form.querySelector('input.qty');
data.quantity = qty && parseInt(qty.value, 10) > 0 ? parseInt(qty.value, 10) : 1;
window.loogika?.event('add_to_cart', data);
});
});
</script>
<?php
}
// AJAX add-to-cart buttons on shop and category pages
add_action( 'wp_footer', 'loogika_track_ajax_add_to_cart' );
function loogika_track_ajax_add_to_cart() {
if ( is_admin() ) {
return;
}
?>
<script>
jQuery(document.body).on('added_to_cart', function (event, fragments, cartHash, $button) {
if (!$button || !$button.length) return;
window.loogika?.event('add_to_cart', {
item_id: $button.data('product_id'),
quantity: $button.data('quantity') || 1,
source: 'ajax_add_to_cart'
});
});
</script>
<?php
}
The product ID and the quantity are all the AJAX handler gets, and it reads both from the data attributes of the button. If you also want the name and the price, give the button more attributes in your theme.
How do you track product views and the start of a checkout?
The same pattern works with two more hooks. Print an inline script from each hook, as in the purchase example.
| Hook | Where it runs | What to send |
|---|---|---|
woocommerce_after_single_product_summary | Product pages. Guard the code with is_product() | The global $product supplies item_id, item_name, price and currency for a view_item event |
woocommerce_before_checkout_form | The checkout page | A begin_checkout event. Take value from WC()->cart->get_total( 'edit' ) and read currency with get_woocommerce_currency() |
How do you check that the events arrive?
Place a test order and click each add-to-cart button your theme offers. The events show up under Events in the Loogika dashboard after a few seconds. With the events in place you can build a funnel from product view to purchase.
Why are WooCommerce events missing in Loogika?
| Cause | Fix |
|---|---|
| The event fires before the script has loaded | Keep the tag in wp_head and keep the DOMContentLoaded wrapper. A bare inline script calls the function too early, and the event is lost |
| The theme updates the cart without a page load | AJAX carts and mini carts never reach the PHP hooks. The added_to_cart listener is what catches those, so try every add-to-cart path on your shop |
| Another analytics plugin prints its own shop data | Plugins that print ecommerce data layers can conflict with the hooks. Test with a real order while both are active |