Sub topic 15
Sending Events from a Server with the Measurement Protocol
When to use the Measurement Protocol
The Google Analytics 4 Measurement Protocol lets a server send events directly to a GA4 web data stream. Use it when an important action happens outside the browser, such as a payment confirmation, backend account change, or offline conversion.
This chapter assumes that you understand GA4 events and parameters, can read JSON, and have a GA4 property with a web data stream. You also need the stream's measurement ID and an API secret.
A server event contains:
- A
measurement_id, which identifies the data stream. - An
api_secret, which authenticates the request. - A
client_id, which connects the event to a browser or device. - An
eventsarray containing event names and parameters.
Build the request
The endpoint is https://www.google-analytics.com/mp/collect. Put the measurement ID and API secret in the query string. Send the event data as JSON in the request body.
This example sends the recommended sign_up event after a server confirms an email registration:
POST https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=API_SECRET
Content-Type: application/json
{
"client_id": "CLIENT_ID",
"events": [
{
"name": "sign_up",
"params": {
"method": "email"
}
}
]
}The sign_up name is recommended because GA4 already defines its meaning and its method parameter. Use a custom event only when no existing or recommended event describes the action.
The client_id is important for attribution and continuity. A browser can create a client ID and send it to your server with the request that causes the backend action. If the server sends an unrelated or newly generated ID, GA4 may treat the event as belonging to a different user or session.
| Field | Purpose | Example |
|---|---|---|
| `measurement_id` | Identifies the GA4 data stream | `G-XXXXXXXXXX` |
| `api_secret` | Authenticates the stream request | `API_SECRET` |
| `client_id` | Links the event to a browser or device | `1234567890.1700000000` |
| `name` | Identifies the event | `sign_up` |
| `params` | Adds event context | `method: email` |
Common server-side errors
A successful HTTP response does not prove that every event parameter is valid or useful in reports. Check the event in GA4 DebugView and inspect the request during development.
Common problems include:
- Sending a client ID that does not match the originating browser.
- Using an API secret from a different data stream.
- Sending an event name that violates GA4 naming rules.
- Expecting a custom parameter to appear in standard reports without registering an event-scoped custom dimension or metric.
- Sending duplicate events when both the browser and server report the same action.
For development, the Measurement Protocol validation endpoint can identify malformed payloads before production collection. Then send the corrected request to the normal /mp/collect endpoint.
Exercise
A browser sends a purchase event, and the server sends another purchase event for the same order. What should you check first?
Show answer
Check whether both requests represent the same transaction. Use one authoritative event, or design deduplication around a stable transaction identifier so the purchase is not counted twice.