Track Shopify Refunds with Mixpanel Insights
Track Shopify Refunds in Mixpanel
5 min setup
No coding required
Runs automatically
This template automatically triggers when a refund is issued in Shopify, calculates the total refund amount, and sends a detailed event to Mixpanel, including product information, refund reason, and customer data. By visualizing refund trends and segmenting by product or customer cohort in Mixpanel, you can identify frequently returned items, optimize inventory decisions, and track refund volume over time to reduce returns and improve your product strategy.
How it works
4 steps to track Shopify refunds in Mixpanel automatically
Refund Created
App connector: [Shopify](/content/apps/shopify/integrate "View Shopify integration page"/index.html)• Time to complete: 0 minutes (Auto-configured)
Why this matters: This trigger captures refunds the instant they’re issued, providing immediate visibility into return activity and enabling real-time dashboards that track refund volume, amounts, and patterns as they happen.
This step automatically activates whenever a refund is processed in your Shopify store, whether it’s a full or partial refund. No configuration is needed - MESA connects directly to your Shopify store’s refund events and captures essential details like refund amount, timestamp, and order information. The trigger passes this refund data to the next step for order retrieval and enrichment.
Retrieve Order
App connector: [Shopify](/content/apps/shopify/integrate "View Shopify integration page"/index.html)• Time to complete: 0 minutes (Auto-configured)
Why this matters: Fetches the complete order record to provide context for the refund, including customer details, order name, and full line item information that’s necessary for matching refunded items to their SKUs and product data.
This step automatically pulls the full order record associated with the refund using the order ID from the trigger. No configuration is required - the system retrieves comprehensive order data including customer details, billing information, and all line items from the original purchase. This enriched data gets passed to the custom code step for processing and formatting.
Calculate Refund Metrics
App connector: [Code](/content/apps/custom/integrate "View Code integration page"/index.html)• Time to complete: 0 minutes (Auto-configured)
Why this matters: Transforms raw refund and order data into analytics-ready metrics by calculating total refund amount, formatting product lists, matching SKUs, and summing quantities—work that would be difficult to accomplish with standard workflow steps.
This step runs custom JavaScript code that calculates total refund amounts, formats line item titles into readable lists, sums refunded quantities, and extracts SKUs for the refunded products. The code processes transaction amounts to determine the actual refund value and creates comma-separated lists of product information for easy analysis. All calculated values are formatted specifically for Mixpanel’s event structure and passed to the final tracking step.
const Mesa = require('vendor/Mesa.js');
/**
* A MESA Script exports a class with a script() method.
*/
module.exports = new (class {
/**
* MESA Script
*
* @param {object} prevResponse The response from the previous step
* @param {object} context Additional context about this task
*/
script = (prevResponse, context) => {
const vars = context.steps;
let response = {};
const refundPayload = vars.shopify;
const shopifyOrder = vars.shopify_1;
let refundAmount = 0;
refundPayload.transactions.forEach(transaction => {
refundAmount -= transaction.amount;
});
const refundedLineItemsTitles = refundPayload.refund_line_items.map(item => item.line_item.title).join(', ');
const refundedLineItemsTotalQuantity = refundPayload.refund_line_items.reduce((sum, item) => sum + item.quantity, 0);
const lineItemsById = {};
shopifyOrder.line_items.forEach(item => {
lineItemsById[item.id] = item;
});
const refundedLineItemsSkus = refundPayload.refund_line_items
.map(refundItem => {
const match = lineItemsById[refundItem.line_item_id];
return match ? match.sku : null;
})
.filter(Boolean)
.join(",");
response.total_refund_amount = refundAmount;
response.refunded_line_items_titles = refundedLineItemsTitles;
response.refunded_line_items_total_quantity = refundedLineItemsTotalQuantity;
response.refunded_line_items_skus = refundedLineItemsSkus;
Mesa.output.next(response);
};
})();
Send Refund Event to Mixpanel
App connector: [Mixpanel](/content/apps/mixpanel/integrate "View Mixpanel integration page"/index.html)• Time to complete: 2 minutes
Why this matters: Creates a rich refund event in Mixpanel with comprehensive properties that enable analysis of refund patterns by product, customer, amount, reason, and timing—insights critical for reducing returns and improving customer satisfaction.
You’ll need to configure your Mixpanel project ID in the “project_id” field to connect this workflow to your Mixpanel account. The step automatically formats all refund data into Mixpanel’s event structure, including order details, refund amounts, customer information, and product-specific data like SKUs and quantities. Each refund creates a distinct event in Mixpanel with the order name in the event title for easy identification.
Frequently asked questions
What if a refund doesn’t have a reason specified?
The Refund Reason property will be empty if no reason was entered when processing the refund. The event will still be created with all other properties populated. Consider adding a filter or default value in the custom code if you want to ensure every event has a reason.
Can I track partial refunds versus full refunds differently?
Yes, add a comparison in the custom code that checks if the total refund amount equals the order’s total price to determine full versus partial refunds, then add this as a property or create different event names for each type.
How are refund amounts calculated when there are multiple transactions?
The custom code loops through all transactions and sums the amounts. The code negates the amounts because Shopify stores refund transaction amounts as positive numbers, but they represent money leaving your account, so the code converts them to negative values for accurate refund total calculation.
Make this template your own!
Customize this workflow even further:
Add product-level refund details
Modify the custom code to create separate events for each refunded line item instead of one aggregated event, enabling product-specific refund rate analysis and identifying which products drive the most returns.
Include refund method information
Add properties to the Mixpanel event that capture the refund gateway, payment method, or whether it was a store credit versus original payment method refund, helping analyze refund processing patterns.
Send alerts for high-value refunds
Add a filter step before Mixpanel that checks the total refund amount and sends Slack notifications when refunds exceed a certain threshold, enabling immediate investigation of significant returns.
Track time-to-refund metrics
Modify the custom code to calculate the days between order creation and refund creation, adding this as a property to analyze how quickly refunds are processed.