How to Track Shopify Purchases in Matomo Using Shopify Pixels
Matomo is a powerful, GDPR-compliant analytics platform. Shopify store owners often want to use it for ecommerce tracking, but Shopify does not provide a native integration. The good news is that Shopify’s Customer Events (Pixels) feature makes it possible to send purchase data into Matomo.
This guide walks you through the entire process, step by step, with code you can copy and paste. By the end, you will have complete ecommerce order tracking from Shopify into Matomo.
Step 1: Open Shopify Pixel Settings
- Log in to your Shopify admin.
- Go to this URL (replace
your-storewith your actual store name): https://admin.shopify.com/store/your-store/settings/customer_events/pixels/ - Click Add custom pixel.
Step 2: Add the Matomo Container Code
In the Code field, paste the following JavaScript and replace https://analytics.example.com/js/container_XYZ.js with your MTM container URL:
var _mtm = window._mtm = window._mtm || [];
_mtm.push({
'mtm.startTime': (new Date().getTime()),
'event': 'mtm.Start'
});
(function() {
var d = document,
g = d.createElement('script'),
s = d.getElementsByTagName('script')[0];
g.async = true;
g.src = 'https://analytics.example.com/js/container_XYZ.js';
s.parentNode.insertBefore(g, s);
})();
analytics.subscribe('checkout_completed', (event) => {
try {
const checkout = event.data.checkout;
window.customShopifyData = {
orderId: checkout.order?.id,
grandTotal: checkout.totalPrice?.amount || 0,
subtotal: checkout.subtotalPrice?.amount || 0,
tax: checkout.totalTax?.amount || 0,
shipping: checkout.shippingLine?.price?.amount || 0,
discount: checkout.totalDiscounts?.amount || 0,
lineItems: checkout.lineItems.map(item => ({
sku: item.variant?.sku || item.variant?.id,
productName: item.title,
category: item.product?.type || "",
price: item.price?.amount || 0,
quantity: item.quantity
}))
};
_mtm.push({
'event': 'shopify_purchase'
});
} catch (e) {
console.error("Matomo Shopify data layer error:", e);
}
});
What this code does:
- Loads your Matomo Tag Manager container.
- Subscribes to Shopify’s
checkout_completedevent. - Collects order details (totals, tax, shipping, discounts, and line items).
- Pushes a custom event called
shopify_purchaseinto the Matomo data layer.
Step 3: Configure Customer Privacy
When creating the pixel, you will also see Customer privacy options. Since Matomo can be run in GDPR-compliant mode, choose these settings:
- Permission: Not required (so the pixel always runs).
- Data sale: Data collected does not qualify as data sale.
This ensures that Matomo runs without requiring consent for basic ecommerce tracking and remains compliant with GDPR.
Step 4: Save Your Pixel
Save your new custom pixel. Shopify will now inject this script on your store’s checkout completion page, sending order data into Matomo.
Step 5: Create a Custom Event Trigger in Matomo
Next, log into your Matomo Tag Manager to capture that custom shopify_purchase event.
- In Matomo Tag Manager, go to Triggers.
- Click Create new trigger.
- Choose Custom Event as the type.
- Name it Shopify Purchase Event.
- In the Event name field, enter:
shopify_purchase - Save the trigger.
Step 6: Create a Custom HTML Tag in Matomo
Now you need to translate the order data into Matomo’s ecommerce tracking format.
- Go to Tags in Matomo.
- Click Create new tag.
- Select Custom HTML as the type.
- Name the tag Shopify Ecommerce Tracking.
- Paste in this code:
<script>
if (window.customShopifyData) {
const data = window.customShopifyData;
// Ensure the Matomo tracker is ready
window._paq = window._paq || [];
// Add each line item
if (data.lineItems && data.lineItems.length > 0) {
data.lineItems.forEach(item => {
_paq.push(['addEcommerceItem',
item.sku,
item.productName,
item.category,
item.price,
item.quantity
]);
});
}
// Track the final ecommerce order
_paq.push(['trackEcommerceOrder',
data.orderId,
data.grandTotal,
data.subtotal,
data.tax,
data.shipping,
data.discount
]);
// Clean up so the data is not reused on refresh
delete window.customShopifyData;
}
</script>
- Under Triggering, choose the trigger you created earlier (Shopify Purchase Event).
- Save and publish your container.
Step 7: Test the Integration
- Place a test order in your Shopify store.
- Open your browser’s developer console and look for the
shopify_purchaseevent being pushed into_mtm. - In Matomo, go to Ecommerce reports.
- You should see the order ID, revenue, tax, shipping, discounts, and line items.
Troubleshooting
Even with everything set up, there are a few common issues that can prevent orders from being tracked:
1. 400 Errors in Matomo Requests
If you see a 400 Bad Request in the network tab for matomo.php, check:
- That all values (order ID, totals, line items) are properly defined.
- That strings (like product names or categories) do not contain special characters that need encoding.
2. Cloudflare Caching
If your Matomo server is behind Cloudflare, make sure caching rules are not interfering with script delivery.
- Go to Cloudflare Rules and add a Cache Rule or Page Rule to Bypass Cache for:
https://analytics.yourdomain.com/js/* https://analytics.yourdomain.com/matomo.php* - This ensures your Matomo container and tracking requests are never cached or blocked.
3. Duplicate Orders Being Tracked
If you notice duplicate orders, make sure that delete window.customShopifyData; remains in your custom HTML tag. Without this, refreshing the confirmation page may resend the purchase.
4. Pixel Not Running at Checkout
If no event fires at all, double-check:
- The pixel is set to Not required under permission.
- The domain hosting Matomo is accessible from your storefront.
- The container ID (
container_XYZ.js) matches your actual Matomo container.
Conclusion
With this setup, Shopify order data flows directly into Matomo:
- Shopify Pixels send completed checkout data.
- Matomo Tag Manager listens for the
shopify_purchaseevent. - A custom HTML tag formats and submits the data using Matomo’s ecommerce tracking API.
You now have a GDPR-compliant, self-hosted analytics solution that provides the same purchase insights as third-party trackers, while keeping full control of your customers’ data.
Looking for Expert WordPress or Drupal development?
I am a freelance website developer and designer based in the UK. I work as a remote Drupal developer, WordPress developer and Front-end developer for a variety of startups, charities and international businesses in Bristol, Bath, London and Europe. You can learn more about me by visiting my resume page.
Get in touch for a free quote on your next project and if you want to connect me with then check out my LinkedIn profile.