Ecommerce Tracking Setup
Track product views, cart actions, orders, and revenue with Ghost Metrics ecommerce tracking.
Overview
Ghost Metrics ecommerce tracking captures the full shopping journey — from product views through cart interactions to completed orders. It uses the Ghost Metrics JavaScript tracking API (_paq) to record ecommerce-specific actions alongside your standard analytics data.
Once configured, you’ll see:
- Product views — Which products visitors browse
- Cart activity — Items added, removed, and cart value over time
- Orders and revenue — Transaction totals, tax, shipping, and discounts
- Product performance — Which products drive the most revenue
Ecommerce data appears in the Ecommerce section of your Ghost Metrics dashboard, with dedicated reports for products, orders, and revenue trends.
Prerequisites
Before you begin, make sure you have:
- Ghost Metrics tracking code installed on your site (Tracking Code)
- Access to your website’s source code or theme templates
- A working ecommerce store (WooCommerce, Shopify, custom, etc.)
How Ecommerce Tracking Works
Ghost Metrics ecommerce tracking is built on four JavaScript calls that map to the shopping lifecycle:
setEcommerceView— Record a product or category page viewaddEcommerceItem— Add an item to the tracked carttrackEcommerceCartUpdate— Send the current cart state to Ghost MetricstrackEcommerceOrder— Record a completed order with revenue
These calls use the _paq tracking array, which is available on any page where the Ghost Metrics tracking code is installed. You can call them directly in your page templates, in inline scripts, or through tags configured in Ghost Metrics Tag Manager.
Setting Up Ecommerce Tracking
Step 1: Track Product Views
When a visitor views a product page, call setEcommerceView before trackPageView to associate the pageview with that product:
var _paq = window._paq = window._paq || [];
_paq.push([
'setEcommerceView',
'SKU-1234', // Product SKU (required)
'Wireless Blood Pressure Monitor', // Product name
'Medical Devices', // Product category
89.99 // Product price
]);
_paq.push(['trackPageView']);Place this in your product page template so it fires on every product view. The SKU is the primary identifier Ghost Metrics uses to track product performance across views, carts, and orders.
To track a category page (no specific product), pass the category without a SKU:
_paq.push(['setEcommerceView', false, false, 'Medical Devices']);
_paq.push(['trackPageView']);Step 2: Track Cart Updates
When a visitor adds an item to their cart or updates quantities, use addEcommerceItem to register each item, then trackEcommerceCartUpdate to send the current cart total:
var _paq = window._paq = window._paq || [];
// Add each item in the cart
_paq.push([
'addEcommerceItem',
'SKU-1234', // Product SKU (required)
'Wireless Blood Pressure Monitor', // Product name
'Medical Devices', // Product category
89.99, // Unit price
2 // Quantity
]);
_paq.push([
'addEcommerceItem',
'SKU-5678',
'Digital Thermometer',
'Medical Devices',
24.99,
1
]);
// Send the cart update with the total cart value
_paq.push(['trackEcommerceCartUpdate', 204.97]);Call addEcommerceItem for every item currently in the cart before calling trackEcommerceCartUpdate. Items not included in the push are treated as removed. This ensures Ghost Metrics always has an accurate snapshot of the cart.
Step 3: Track Orders
On the order confirmation page, use addEcommerceItem for each purchased item, then call trackEcommerceOrder with the transaction details:
var _paq = window._paq = window._paq || [];
// Add each item from the order
_paq.push([
'addEcommerceItem',
'SKU-1234',
'Wireless Blood Pressure Monitor',
'Medical Devices',
89.99,
2
]);
_paq.push([
'addEcommerceItem',
'SKU-5678',
'Digital Thermometer',
'Medical Devices',
24.99,
1
]);
// Record the order
_paq.push([
'trackEcommerceOrder',
'ORD-20260301-4821', // Order ID (required, must be unique)
204.97, // Grand total / revenue (required)
179.98, // Subtotal (before tax/shipping)
14.40, // Tax
10.59, // Shipping
false // Discount (false if none)
]);Each order ID must be unique. If the same order ID is sent twice, Ghost Metrics ignores the duplicate to prevent inflated revenue. Use your ecommerce platform’s order number or transaction ID.
Step 4: Verify Tracking
After adding the ecommerce calls to your site:
- Browse a product on your store
- Add it to the cart
- Complete a test order
- In Ghost Metrics, go to Visitors > Visits Log
- Find your visit — you should see ecommerce actions labeled as product views, cart updates, and orders
- Go to Ecommerce in the main navigation to see revenue and product reports
Using Ghost Metrics Tag Manager
Instead of adding JavaScript directly to your page templates, you can configure ecommerce tracking through Ghost Metrics Tag Manager. This is useful when you want to manage tracking centrally without modifying theme files.
Creating Ecommerce Tags
- Go to Administration > Tag Manager and select your container
- Click Tags > Create New Tag
- Select Custom HTML as the tag type
- Paste the relevant ecommerce tracking code (product view, cart update, or order)
- Assign a trigger (see below)
- Save and publish the container
Setting Up Triggers
Ghost Metrics Tag Manager supports several trigger types for ecommerce events:
- Pageview triggers with URL conditions — Fire on product pages by matching URL patterns (e.g., URL contains
/product/) - Custom event triggers — Fire when your site pushes a custom event to the data layer (e.g.,
addToCart,purchase) - DOM element triggers — Fire when specific elements exist on the page (e.g., an order confirmation element)
Example: Product view trigger
Create a Pageview trigger with a condition like “Page URL contains /product/” and attach it to a Custom HTML tag containing the setEcommerceView call.
Example: Order trigger
Create a Custom Event trigger matching a purchase event, and attach it to a tag containing the trackEcommerceOrder call. Your checkout page should push this event to the data layer after a successful order:
var _mtm = window._mtm = window._mtm || [];
_mtm.push({
'event': 'purchase',
'orderId': 'ORD-20260301-4821',
'orderTotal': 204.97
});Reading Data Layer Values in Tags
When using Tag Manager triggers with data layer events, you can create variables in Tag Manager to extract values like order ID and total. Reference these variables in your Custom HTML tags to dynamically populate the ecommerce tracking calls.
WooCommerce Implementation Example
WooCommerce is one of the most common ecommerce platforms. Here’s how to add Ghost Metrics ecommerce tracking to a WooCommerce store by adding code to your theme or a custom plugin.
Product Views
Add to your theme’s single-product.php template or via the woocommerce_after_single_product hook:
<?php
global $product;
if ( $product ) :
?>
<script>
var _paq = window._paq = window._paq || [];
_paq.push([
'setEcommerceView',
'<?php echo esc_js( $product->get_sku() ); ?>',
'<?php echo esc_js( $product->get_name() ); ?>',
'<?php
$categories = wp_get_post_terms( $product->get_id(), 'product_cat' );
echo esc_js( ! empty( $categories ) ? $categories[0]->name : '' );
?>',
<?php echo esc_js( $product->get_price() ); ?>
]);
_paq.push(['trackPageView']);
</script>
<?php endif; ?>Cart Updates
Add to your theme’s cart.php template or via the woocommerce_after_cart hook:
<script>
var _paq = window._paq = window._paq || [];
<?php foreach ( WC()->cart->get_cart() as $item ) :
$product = $item['data'];
$categories = wp_get_post_terms( $product->get_id(), 'product_cat' );
?>
_paq.push([
'addEcommerceItem',
'<?php echo esc_js( $product->get_sku() ); ?>',
'<?php echo esc_js( $product->get_name() ); ?>',
'<?php echo esc_js( ! empty( $categories ) ? $categories[0]->name : '' ); ?>',
<?php echo esc_js( $product->get_price() ); ?>,
<?php echo esc_js( $item['quantity'] ); ?>
]);
<?php endforeach; ?>
_paq.push(['trackEcommerceCartUpdate', <?php echo esc_js( WC()->cart->get_total( 'edit' ) ); ?>]);
</script>Order Confirmation
Add to your theme’s thankyou.php template or via the woocommerce_thankyou hook:
<?php
$order = wc_get_order( $order_id );
if ( $order ) :
?>
<script>
var _paq = window._paq = window._paq || [];
<?php foreach ( $order->get_items() as $item ) :
$product = $item->get_product();
$categories = wp_get_post_terms( $product->get_id(), 'product_cat' );
?>
_paq.push([
'addEcommerceItem',
'<?php echo esc_js( $product->get_sku() ); ?>',
'<?php echo esc_js( $item->get_name() ); ?>',
'<?php echo esc_js( ! empty( $categories ) ? $categories[0]->name : '' ); ?>',
<?php echo esc_js( $order->get_item_total( $item ) ); ?>,
<?php echo esc_js( $item->get_quantity() ); ?>
]);
<?php endforeach; ?>
_paq.push([
'trackEcommerceOrder',
'<?php echo esc_js( $order->get_order_number() ); ?>',
<?php echo esc_js( $order->get_total() ); ?>,
<?php echo esc_js( $order->get_subtotal() ); ?>,
<?php echo esc_js( $order->get_total_tax() ); ?>,
<?php echo esc_js( $order->get_shipping_total() ); ?>,
<?php echo esc_js( $order->get_discount_total() ? $order->get_discount_total() : 'false' ); ?>
]);
</script>
<?php endif; ?>JavaScript API Reference
| Method | Parameters | Description |
|---|---|---|
setEcommerceView | SKU, name, category, price | Record a product or category page view. Call before trackPageView. |
addEcommerceItem | SKU, name, category, price, quantity | Add an item to the tracked cart. Call for every item before a cart update or order. |
trackEcommerceCartUpdate | grandTotal | Send the current cart state. Items not re-added since the last call are treated as removed. |
trackEcommerceOrder | orderId, grandTotal, subTotal, tax, shipping, discount | Record a completed order. The order ID must be unique. |
All methods are called via _paq.push(['methodName', ...args]).
Best Practices
Test on Staging First
Set up and verify ecommerce tracking on a staging environment before deploying to production. This lets you confirm all calls are firing correctly without affecting your live analytics data.
Use Consistent SKUs
Ghost Metrics uses the product SKU as the primary identifier across all ecommerce reports. Make sure every product in your catalog has a unique SKU assigned and that the same SKU is passed in product views, cart updates, and orders.
Always Include Items Before Order Tracking
Call addEcommerceItem for every item in the order before calling trackEcommerceOrder. Without item data, Ghost Metrics records the order revenue but cannot break down performance by product.
Set Up Ecommerce Goals
Combine ecommerce tracking with Goals to measure conversion rates at each stage of the shopping funnel. For example, create goals for “Viewed Product,” “Added to Cart,” and “Completed Purchase.”
Prevent Duplicate Order Tracking
Order confirmation pages can be reloaded or revisited. Protect against duplicate trackEcommerceOrder calls by checking if the order has already been tracked:
if (!sessionStorage.getItem('tracked_order_' + orderId)) {
_paq.push(['trackEcommerceOrder', orderId, grandTotal, subTotal, tax, shipping, discount]);
sessionStorage.setItem('tracked_order_' + orderId, 'true');
}Monitor for Discrepancies
Compare Ghost Metrics order totals with your ecommerce platform’s reports weekly. Small differences are normal (bot traffic, JavaScript errors, visitors with JavaScript disabled), but large gaps may indicate a tracking issue.
Troubleshooting
Orders Not Appearing
- Open your browser’s developer console on the order confirmation page and verify
_paqcalls are executing - Check that the Ghost Metrics tracking code loads before the ecommerce calls — if the container script hasn’t loaded yet,
_paqcalls are queued but may be missed if there’s an error - In Ghost Metrics, go to Visitors > Visits Log and look for the visit — ecommerce actions appear as icons next to the visit entry
Revenue Showing as $0
- Ensure the grand total passed to
trackEcommerceOrderis a number, not a string with currency symbols (e.g.,49.99, not$49.99) - Check that the value is not
undefinedorNaN— log it to the console before pushing - If using a template language, verify the output is a raw number without HTML encoding
Duplicate Transactions
- Ghost Metrics automatically deduplicates by order ID, but if the confirmation page reloads before the first request completes, a race condition can occur
- Use the
sessionStorageguard shown in Best Practices above - Avoid placing order tracking on pages the customer can navigate back to
Missing Product Data in Order Reports
- Make sure
addEcommerceItemis called for each item beforetrackEcommerceOrder - Verify that SKUs in the order match the SKUs used in product view tracking — mismatched SKUs create orphaned product records
Cart Data Not Updating
- Remember that
trackEcommerceCartUpdatereplaces the entire cart state. You must calladdEcommerceItemfor every item currently in the cart, not just the newly added item - If the cart is empty, call
trackEcommerceCartUpdatewith0as the grand total to clear it
Next Steps
- WooCommerce Analytics — Premium automatic ecommerce tracking for WooCommerce
- Goals — Measure conversions at each step of the shopping funnel
- Events — Track custom interactions beyond standard ecommerce
- Custom Reports — Build reports tailored to your ecommerce KPIs