How Cosmic AI calculates contribution margin
Cosmic AI calculates contribution margin as net revenue from Shopify minus four costs, in this order. Product cost comes off first, then shipping per order, then payment fees, then ad spend. It reports the figure before ads and after ads. If no costs are set, the answer is "not enough data", not an estimate.
How does Cosmic AI calculate contribution margin?
Cosmic AI calculates contribution margin by starting from a store's net revenue and subtracting four costs: product cost, shipping per order, payment fees and ad spend. The home page puts it in one line: margin is net revenue minus product cost, shipping, payment fees and ad spend.
My position is that revenue is the wrong number to run a marketing decision on. A contribution margin missing one of its four inputs is worse than no margin, because it looks finished and gets used.
Gross margin = Net revenue - Product cost
Contribution margin before ads = Gross margin - Shipping - Payment fees
Contribution margin after ads = Contribution margin before ads - Ad spend
I am Jake Bauman and I make Cosmic AI, a free, open-source CLI and a Mac app that check supported figures against their source. Hosted Cosmic Cloud is on the waitlist. Its growth agents judge their work by this number, so you should be able to check it with a calculator. This page gives every input, the order of subtraction, and one month worked line by line. The general method, for any store and any tool, is in the guide to the contribution margin formula.
What are the four cost inputs?
The four cost inputs are product cost, shipping per order, payment fees and ad spend. Net revenue is the starting figure and comes from the Shopify connection. The example column shows the settings on an example store. These are example figures, not customer results. The example store is far larger than most, so read the shares and ratios, not the totals.
| Input | What it is | Setting in the example |
|---|---|---|
| Net revenue | The store's net revenue, from Shopify | $16,610,912.12 over 30 days |
| Product cost | What the goods cost you | 42% of net revenue |
| Shipping | What you pay to ship one order | $7.50 per order |
| Payment fees | The card rate plus the fixed fee per order | 2.9% plus $0.30 per order |
| Ad spend | What the ad platform charged for the same days | Meta spend, $4,044,339 |
Three of these have a public reference you can hold your own numbers against. Shopify's finance reports documentation defines net sales as gross sales minus discounts and sales reversals, without shipping charges or taxes. Shopify's help page on cost per item says that field holds what a product or variant costs you. The 2.9% plus 30 cents in the example matches the online card rate on the entry plan of the Shopify pricing page and the domestic card rate on the Stripe pricing page.
Setup with Work with Jake includes your product costs from Shopify or a file.
In what order are the costs subtracted?
The costs come off in a fixed order: product cost, then shipping and payment fees, then ad spend. The order gives three stopping points, and each one answers a different question.
- Gross margin is net revenue less product cost. It tells you whether the goods are priced above what they cost.
- Contribution margin before ads also removes shipping and payment fees. It tells you what an order is worth once it has shipped and the card fee is paid.
- Contribution margin after ads also removes ad spend. It is the figure the agents judge by, and the only one of the three that falls when you overspend on ads.
There is a good reason to take ads off last. If ad spend came off first, you could not see what an order earns before you paid to win it, and that middle figure is the one a budget decision needs.
The calculation on one month of example data
The table uses 30 days of example figures, not customer results: 227,671 orders. The three totals in the last column come from the calculation itself. The cost lines in the middle column are my own arithmetic from the four inputs.
| Step | What comes off | My arithmetic | What is left |
|---|---|---|---|
| Start | Nothing yet | Net revenue from Shopify | $16,610,912.12 |
| 1 | Product cost | 42% x $16,610,912.12 = $6,976,583.09 | Gross margin, $9,634,329.01 |
| 2 | Shipping | 227,671 x $7.50 = $1,707,532.50 | |
| 3 | Payment fees | 2.9% x $16,610,912.12, plus 227,671 x $0.30 = $550,017.75 | Contribution margin before ads, $7,376,778.80 |
| 4 | Ad spend | Meta, same 30 days = $4,044,339.00 | Contribution margin after ads, $3,332,439.80 |
The margin after ads is 20.06% of net revenue. Product cost took 42.00%, shipping 10.28%, payment fees 3.31% and ads 24.35%.
Now the hand check. Subtract my four cost lines from net revenue and you get $3,332,439.78. The calculation shows $3,332,439.80 for the same example. The two figures are two cents apart on $16.6 million, and the gross margin line carries the same two cents. That is close enough for me to call the arithmetic the same. If your own hand check is off by thousands, a setting is wrong, and the table tells you which line to look at.
Does the number of days change the answer?
The number of days changes the answer only when revenue and ad spend cover different days. Here are 30 days and 90 days of the same example figures.
| Window | Net revenue | Meta ad spend | Margin after ads | Margin % |
|---|---|---|---|---|
| Last 30 days | $16,610,912.12 | $4,044,339.00 | $3,332,439.80 | 20.06% |
| Last 90 days | $37,285,338.38 | $8,986,606.16 | $7,649,916.70 | 20.52% |
The two percentages sit within half a point of each other because both windows hold a full set of ad spend. The example store has only 90 days of Meta spend. Ask for a longer window and you would subtract 90 days of ads from many more days of revenue, and the margin would look far better than it is. So before you trust any margin, from any tool, check that every cost covers the same days as the revenue.
One more limit of the example. Only Meta has ad spend in the example store, so "after ads" here means after Meta ads.
What happens when a cost is missing?
When no costs are set, Cosmic AI answers "not enough data" and gives no margin. Here is an example of that answer, for a store with no costs entered:
We can't give a contribution margin after ads for the last 30 days yet. No costs are set for this store, so the margin is "not enough data."
The alternative is to fill the gap with an average, and I think that is worse than no answer. A margin built on a guessed product cost looks exactly like a margin built on your invoices. You cannot tell them apart on a screen, and you would set ad budgets from either one.
I can show only that one case. I have no worked example of a store with some costs set and others missing, so I will not describe it here. The article on why an AI should say not enough data covers the wider rule.
What is checked before the margin is calculated?
Supported figures are checked against their source before an agent quotes them. The margin calculation starts from net revenue, and a wrong start spoils every line below it. In the example figures, the order count in the warehouse matches the source: 3,000,000 orders against 3,000,000.
Two softer checks cover 90 days of the same example figures. Klaviyo recorded 493,470 placed orders against 500,987, which is within 3%. Google Analytics 4 recorded 465,913 purchases against 500,987, which is within 15%. Tools count differently, so the check is a stated tolerance and not an exact match. The guide to ecommerce data reconciliation explains why those gaps exist.
The Work with Jake agreement has one guarantee: if your first agent job is not running on checked data within 30 days of kickoff, the setup fee comes back, as long as you gave access to the agreed sources within 5 business days and joined the weekly sessions.
How do I check the number myself?
- Pick a window, such as the last 30 days, and write down net sales and the order count from Shopify for those days.
- Multiply or sum your product cost for the same orders, and subtract it. You now have gross margin.
- Multiply the order count by your shipping cost per order. Work out payment fees from your card rate and fixed fee. Subtract both.
- Subtract ad spend for the same days. Divide what is left by net sales.
If your figure and the product's differ, compare line by line in the order above. The first line that disagrees is the setting to fix. For the same sum on a single order, see contribution margin per order. The numbers are also available in a terminal through the command line tool, and the product page shows how the agents use them.
How do Shopify brands identify products that sell well but lose money after ad costs?
They run the same subtraction one product at a time and look for a negative after ads. The store-wide margin can be healthy while one bestseller is below zero, because the average hides it.
Here is the arithmetic with made-up figures, not example data and not a customer. A product sells 200 units at $50. It costs $22 to buy, $7.50 to ship, and the card fee is 2.9% plus $0.30, which is $1.75. The ads that bring in each sale cost $20.
| Line per unit | Amount |
|---|---|
| Price | $50.00 |
| Product cost | -$22.00 |
| Shipping | -$7.50 |
| Payment fee | -$1.75 |
| Before ads | $18.75 |
| Ads per sale | -$20.00 |
| After ads | -$1.25 |
Across 200 units that is $3,750 before ads, $4,000 of ads and a loss of $250. The product ranks first by units and revenue, and it still costs the brand money on every sale. The figure to watch is the before ads $18.75: it is the most a sale of this product can spend on ads and break even.
Two inputs make this harder than the store-wide sum. Product cost has to be set for each product, not as one percentage. And ad spend has to be matched to products, usually by campaign, which is the platform's own grouping and not a measurement of who bought what. The example data on this page has no per product split, so I do not show one. Treat a product-level result as a lead to test, not a verdict.
Frequently asked questions
How does Cosmic AI calculate contribution margin?
Cosmic AI calculates contribution margin as net revenue from Shopify minus product cost, shipping per order, payment fees and ad spend, subtracted in that order. It reports gross margin, contribution margin before ads and contribution margin after ads. In 30 days of example figures, not a customer result, $16,610,912.12 of net revenue left $3,332,439.80, or 20.06%.
How are contribution margins calculated?
Contribution margin is net revenue minus the costs that rise and fall with orders. For a Shopify store those costs are product cost, shipping, payment fees and ad spend. Divide the result by net revenue to get the percentage.
What does Cosmic AI say when costs are not set?
Cosmic AI says "not enough data" when no costs are set for a store. It does not estimate a margin from an average or a benchmark. Once product cost, shipping per order, payment fees and ad spend are in place, the margin is calculated.
How can I connect Shopify ad spend, product costs, returns, and fees to understand product profitability?
Put them on the same days and subtract in a fixed order. Start with Shopify net sales, which already has discounts and returns removed. Subtract product cost, then shipping, then payment fees, then ad spend. The worked table above does this for a whole store. A per product answer needs each cost split by product, which the example data here does not have.