Setting Up the ProfitMetrics Profit Variable in Server-Side GTM
The ProfitMetrics Profit variable calculates the gross profit of an order in real time and returns the value into your server-side GTM container. You can then use that profit value in any tag — for example, to send profit to a Google Campaign Manager Floodlight or a Google Analytics purchase event.
What You Need
Before configuring the variable, make sure you have the following ready.
| Requirement | Details |
|---|---|
| ProfitMetrics account | An active ProfitMetrics website with product cost data configured |
| Public ID | Found on the Websites page in ProfitMetrics, under the Public ID column |
| API key | Request one from support@profitmetrics.io |
| Server-side GTM | A working server container that receives a purchase event with order data |
| Fallback costs | Configured under Expenses > Fallback costs in ProfitMetrics |
To request your API key from the ProfitMetrics support team, please feel free to use the following template:
Hi ProfitMetrics,
Please create a server-side GTM variable API key for the website below:
Public ID:
URL: https://example.com
Installing the Template
The variable ships as a custom template file (.tpl) which you import into your server-side GTM workspace.
- Download the template file from this Google Drive link.
- In server-side GTM, go to Templates → Variable Templates → New.
- In the template editor, open the menu (three dots, top right) and choose Import. Select the downloaded
.tplfile. - Save the template and close the editor.
- Go to Variables → New → User-Defined Variable, then select ProfitMetrics - Profit from the custom variable types.
Profitmetrics data
This is the main configuration block. It connects the variable to your ProfitMetrics account and provides the order-level fields used in the profit calculation.
| Field | Description |
|---|---|
| Profitmetrics Public ID | Your website's Public ID from the Websites page in ProfitMetrics (Required) |
| Profitmetrics API key | The API key issued to your account (Required) |
| Shipping weight (grams) | Order shipping weight, in grams (Optional) |
| Shipping method | Shipping method name, e.g. "Express", "Standard" (Optional) |
| Payment method | Payment method name, e.g. "Credit card", "PayPal" (Optional) |
If Shipping weight, Shipping method, or Payment method are not available on a given order, ProfitMetrics uses the fallback values configured under Expenses > Fallback costs for that website. Leave these fields empty if you want the fallbacks to apply.
VAT information
These checkboxes describe whether the values in your purchase event already include VAT. The variable strips VAT before sending values to ProfitMetrics, so the settings need to match what your platform actually sends.
| Setting | What it means |
|---|---|
| Total value is tracked including VAT | The value field on your event already has VAT included |
| Shipping is tracked including VAT | The shipping field on your event already has VAT included |
| Product value is tracked including VAT | Product price values inside items already have VAT included |
| Vat rate (%) | Your VAT percentage as a number — for 25% VAT, enter 25 |
The Vat rate (%) field only appears when at least one of the three checkboxes above is enabled. If your VAT settings are wrong, ProfitMetrics will calculate incorrect profit. If you are unsure, check a sample order against your platform admin.
Event data the variable reads automatically
The variable reads the following fields directly from the purchase event in your server container. You do not need to map them manually unless you want to override one of them.
| Event field | Used as |
|---|---|
transaction_id |
Order ID |
value |
Total order value |
currency |
Currency code |
shipping |
Shipping cost |
coupon |
Voucher code |
user_data.email |
Customer email |
user_data.phone_number |
Customer phone |
user_data.address[0].country |
Shipping country |
user_data.address[0].postal_code |
Shipping zipcode |
items[].item_id |
Product SKU |
items[].quantity |
Product quantity |
items[].price |
Product price |
If transaction_id is missing from the event, the variable returns nothing and the calling tag does not receive a value.
Overwrite event data (optional)
Use this section if you need to replace any of the standard fields before they are sent to ProfitMetrics — for example, if your event uses a different identifier as the order ID, or if you want to hash an email before sending.
Each row has two columns: Field (a dropdown of supported field names) and Value (a literal string or a GTM variable reference). Values set here always take priority over the auto-read event data.
| Group | Available fields |
|---|---|
| Order | id, ts, currency, voucherCode |
| Customer | orderEmail, orderEmailMD5, orderEmailSHA256, customerPhone, customerPhoneMD5, customerPhoneSHA256 |
| Shipping | shippingMethod, shippingCountry, shippingZipcode, shippingWeight |
| Payment | paymentMethod |
| Pricing | priceShippingExVat, priceTotalExVat, priceTotalInclVat |
| Products | products |
Profit metrics Order overrides (optional)
Use this section to manually override individual cost fields or the calculated profit on a per-order basis. This is useful when you have known costs that ProfitMetrics cannot derive from the order itself.
| Field | Use case |
|---|---|
shippingCostExVat |
Override the calculated shipping cost |
paymentCostExVat |
Override the calculated payment processing cost |
extraCostExVat |
Add an additional one-off cost to the order |
grossProfitExVat |
Override the calculated gross profit value entirely |
Currency
By default, the variable uses the currency code from the purchase event. If you need to override it — for example, when your store reports values in one currency but you want ProfitMetrics to treat them as another — enter a three-letter currency code in the Output currency field (e.g. USD, EUR, GBP). Leave the field blank if the event's currency is correct.
Set up an Estimated profit fallback variable
The Profit variable returns a live value from ProfitMetrics. For tags where you want a backup value available — for example, during initial setup, or as an approximate value if the live calculation is unavailable — create an Estimated profit variable based on your store's average margin.
- In server-side GTM, go to Templates → Variable Templates and import the Deduct percentage template from the Community Template Gallery.
- Go to Variables → New → User-Defined Variable and select Deduct percentage. Name it Estimated profit.
- Set Input number to your order value variable.
- Set Percentage to your store's average margin. For a 35% margin, enter
35. - Leave Input includes percentage unchecked.
You can now reference anywhere a backup profit value is useful.
Create a value transformation
A server-side transformation runs before the affected tags fire and rewrites fields on the incoming event. This avoids hard-coding the variable reference into each conversion tag.
- In server-side GTM, go to Transformations → New → Augment Event.
- Name the transformation Gross profit value transformation.
- Under Parameters to augment, add a row with Name set to
valueand Value set to0. - Under Affected tags, select only the tags that should report profit as the conversion value — for example, a dedicated Google Ads Gross Profit conversion tag. Do not apply it to tags that should still receive revenue.
- Save the transformation.
Configure the destination tag
In the conversion tag that should use profit as its value (e.g. the Google Ads Gross Profit conversion tag):
- Leave Conversion value and Transaction ID blank.
- Make sure the tag is set to read these fields from the incoming event data automatically.
By the time the tag fires, the transformation has already replaced the event's value with the calculated profit, so the tag reports profit instead of revenue. The Profit variable resolves once per request, so multiple tags reading it in the same request receive the same value without triggering a second calculation.
The setup above assumes the standard event stream — purchase events with value, items, currency, and user_data sent in the standard GA4 format. Custom dataLayers or non-standard items arrays may require remapping via the Overwrite event data section in the Profit variable.
Troubleshooting
If the variable returns no value or an unexpected value, enable Log to console in the variable settings and run the request again in the server-side GTM Preview mode. The console output shows the data being sent and the response received.
| Symptom | Likely cause |
|---|---|
| Variable returns nothing | The event has no transaction_id, or the call to ProfitMetrics failed |
| Wrong profit value | VAT settings do not match the actual event data, or fallback costs are missing in ProfitMetrics |
| Authentication error in console | Wrong Profitmetrics Public ID or Profitmetrics API key |
| Same value across different orders in preview | Result cached for a repeat transaction_id within the same request — preview a new request with a different order |
| Profit is reasonable but missing shipping or payment cost | Fallback costs not configured under Expenses > Fallback costs |
| Profit calculated as if the order had no products | The items array is malformed — the variable expects each item to have item_id, quantity, and price. If those fields are missing or named differently (e.g. id or sku), products cannot be matched to their costs in ProfitMetrics |
For anything not covered here, contact support@profitmetrics.io.
Tip: isolating your own browser on Shopify checkouts
Shopify checkouts make it harder to verify changes without your debug events mixing into real traffic. Setting a localStorage flag in your own browser and forwarding it to server-side GTM as an event parameter lets you gate your tags so only your debug session triggers them.
- In the browser you're testing with, open the developer console on the storefront and run:
localStorage.setItem('debug', '1') - In your web GTM container, create a Custom JavaScript variable named localStorage getItem - debug with this code:
function() { try { return localStorage.getItem('debug'); } catch(e) { return undefined; } } - Create another GTM variable named dbg — a Constant variable holding the literal string
_dbgas the value. - In your Google Analytics purchase tag — the one sending events to your sGTM container — add an event parameter with Name
and Value. This is required as Google do not let you add_dbgas the name directly. - In server-side GTM, gate the tags you're testing on the
dbgevent parameter so they only fire when its value equals1.
While localStorage.debug is set to 1 in your browser, events flow through to your gated server-side tags. Clear it with localStorage.removeItem('debug') to stop forwarding.
For anything not covered here, contact support@profitmetrics.io.