Sub topic 17
Sending Purchases and Refunds Server-Side with the Measurement Protocol
Assume you understand GA4 properties, events, JSON, and the basic JavaScript object model. This chapter uses the Measurement Protocol (MP) to send events from a server instead of a browser.
When to use the Measurement Protocol
The Measurement Protocol is an HTTP interface for sending GA4 events from your backend. It is useful when the server knows the final order status, such as after payment confirmation, or when a refund happens in an order system without a customer visiting your website.
Send the request to the GA4 collection endpoint. The query string identifies the data stream and authenticates the request:
https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=YOUR_API_SECRETKeep the API secret on your server. Never place it in browser JavaScript, a public repository, or a page response.
A server-side purchase should include the same core information as a browser purchase:
client_idor an appropriateuser_idto associate the event with a user.transaction_id, which uniquely identifies the order.currencyandvalue.- An
itemsarray containing the purchased products. - Separate
taxandshippingvalues when applicable.
{
"client_id": "123456.7654321",
"events": [
{
"name": "purchase",
"params": {
"transaction_id": "T_12345",
"currency": "USD",
"value": 49.98,
"tax": 4.00,
"shipping": 5.99,
"items": [
{
"item_id": "SKU_1001",
"item_name": "Trail Running Shoe",
"price": 24.99,
"quantity": 2
}
]
}
}
]
}Your order system should send this event only after the order reaches the payment state you consider successful. If the same transaction is sent more than once, revenue can be duplicated or require deduplication behavior. Use one stable transaction ID for all attempts to report that order.
Sending refunds
Send a refund event that references the original order with its transaction_id. For a partial refund, include the refunded item and quantity. For a full refund, include all refunded items and the refunded value.
{
"client_id": "123456.7654321",
"events": [
{
"name": "refund",
"params": {
"transaction_id": "T_12345",
"currency": "USD",
"value": 24.99,
"items": [
{
"item_id": "SKU_1001",
"item_name": "Trail Running Shoe",
"price": 24.99,
"quantity": 1
}
]
}
}
]
}The server must calculate refund values from trusted order data. Do not accept prices or quantities directly from an untrusted browser request.
Item-scoped custom dimensions
GA4 supports custom item parameters for product attributes that standard fields do not cover. For example, you might send membership_level or material inside each item:
{
"item_id": "SKU_1001",
"item_name": "Trail Running Shoe",
"price": 24.99,
"quantity": 2,
"material": "recycled mesh"
}Register material in the GA4 administration area as a custom dimension with item scope. An item-scoped dimension describes a product, not the whole event. It can then be used in item-level reports and explorations.
The parameter name in the event must match the registered parameter name exactly. Registration does not backfill older events, so send and validate the parameter before relying on reports. Keep product attributes stable and avoid sending personally identifiable information.
Exercise
Which identifier connects a server-side refund to the original purchase?
Show answer
The refund must use the original purchase's transaction_id.