This Server-Side GTM variable retrieves product margin data from
Google Firestore, calculates the total cart margin from event
items, and returns the result as a string.
Supports:
- Custom (non-Shopify) setups
- Shopify integrations
- Multiple calculation methods
- Automatic fallback when data is missing
- Reads
itemsfrom event data - Fetches product data from Firestore
- Calculates margin per item
- Applies fallback if needed
- Returns total cart margin (rounded to 2 decimals)
Path
<collectionId>/<productId>
Example document
{
"margin": 12.5,
"return_rate": 0.1
}Path
shopify/shops/shop_<SHOPIFY_ID>/collections/products/<item_id>-<variant_id>
Margin logic
price - unitCost
- Data Source:
OtherorShopify - Shopify ID (if Shopify selected)
- Collection ID (if not Shopify)
- GCP Project ID (otherwise uses
GOOGLE_CLOUD_PROJECT) - Custom item field names
- Fallback percentage
margin × quantity
(1 - return_rate) × margin × quantity
(margin - discount) × quantity
If a product is not found in Firestore:
fallbackPercent × price × quantity
Default value
0.5 (50%)
The value must be between 0 and 1.
Example event structure
{
"items": [
{
"item_id": "123",
"price": 100,
"quantity": 2,
"discount": 0
}
]
}Field names are configurable in the template.
- Returned as a string
- Rounded to 2 decimals
- Represents the total cart margin
Example
"42.37"
- Profit-based bidding
- Server-side ecommerce tracking
- Margin reporting
- Advanced attribution models