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 {{Estimated profit}} 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 to{{ProfitMetrics - Profit}}{% endraw}. - 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 (they normally do this by default with a standard Google Analytics event stream format).
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 when testing Shopify checkout tracking
Step 1: Set a debug flag in your browser
In the browser you're using to test, open the developer console on your storefront and run the following command:
localStorage.setItem('debug', '1');
This stores a flag called debug with the value 1 in that browser only. It has no effect on any other visitor and won't be sent anywhere on its own — it just sits in your browser's storage until something reads it.
NOTE: This flag is specific to the browser and device you set it in. If you switch browsers or devices, or clear your browser storage, you'll need to set it again.
Step 2: Read the flag in your tag manager container
In your web tag manager container, create a Custom JavaScript variable to read the flag back out of storage. Name it localStorage getItem - debug and use this code:
function() {
try {
return localStorage.getItem('debug');
} catch(e) {
return undefined;
}
}
Wrapping the read in a try/catch means the variable safely returns undefined instead of causing an error for visitors whose browser blocks access to storage.
Next, create a second variable named dbg. Make this a Constant variable holding the literal text _dbg. This will be used as the parameter name in the next step.
Step 3: Forward the flag as an event parameter
In your Google Analytics purchase tag — the one that sends events on to your server-side container — add an event parameter using the two variables you just created:
| Field | Value |
|---|---|
| Name | The dbg variable (resolves to the literal text _dbg) |
| Value | The localStorage getItem - debug variable (resolves to 1 when your flag is set) |
NOTE: You need to set the parameter name using the dbg variable rather than typing _dbg directly, because Google Analytics doesn't allow parameter names starting with an underscore to be entered as free text.
Step 4: Gate your tags in server-side tracking
In your server-side container, add a trigger condition to the tags you're testing so they only fire when the _dbg event parameter equals 1. Any tag with this condition will now only trigger for traffic carrying your debug flag, leaving every other visitor's data untouched.
Testing and cleaning up
With localStorage.debug set to 1 in your browser, your checkout activity flows through to the gated server-side tags, letting you verify they're working correctly without mixing your test events into real customer data.
Once you're done testing, clear the flag by running the following in the same browser's developer console:
localStorage.removeItem('debug');
This stops the flag being forwarded, and your gated tags will no longer fire for this browser.
For anything not covered here, contact support@profitmetrics.io.